Merging 2 classes together

I would like to merge my Vote4Cash class with my commands class.

My vote4cash class will reward the player for voting when they type the command check. Therefor, I need the vote4cash process to be built into my commands class. I've been trying to do this for a while, it's the most important part of my vote4cash system. If you know how I could merge them together or make them work together please tell me how. I tried using imports, it didn't work out. I didn't code the commands class, someone else did, which makes it harder for me to add the vote4gold process or make them work together.

My Vote4Cash class:

import java.sql.*;
import java.net.*;

public class Vote4Cash {

  public static void main(String args[]) {

  Connection con = null;
  Statement st = null;
  Statement stmt = null;
  ResultSet auth = null;
  ResultSet given = null;

    String url = "jdbc:mysql://localhost:3306/";
    String db = "vote4gold";
    String driver = "com.mysql.jdbc.Driver";
    String user = "root";
    String pass = "";

  try {
  Class.forName(driver);
  con = DriverManager.getConnection(url + db, user, pass);
  con.setAutoCommit(false);
  st = con.createStatement();
  stmt = con.createStatement();

  //String give = "SELECT `given` FROM `has_voted` WHERE `ip` LIKE '+thisIp.getHostAddress()'";
        InetAddress thisIp =InetAddress.getLocalHost();
String give = "SELECT `given` FROM `has_voted` " + 
              "WHERE `ip` = '" + thisIp.getHostAddress() + "'";
  given = st.executeQuery(give);

while (given.next()) {
    if (given.getInt("given") > 0) {
        System.out.println("You've already recieved a reward for the last time you voted, but thanks again for voting.");
    } else {
        System.out.println("Thanks for voting! You've been rewarded 25m gold! Vote again tomorrow!");
        String sql = "SELECT has_voted (given) Replace('0', '0', '1')";
        int rows = stmt.executeUpdate("UPDATE has_voted SET given = 1 WHERE given = 0");
        System.out.println("The given reward column has been set to 1 for the ip address:");
       System.out.println("IP:"+thisIp.getHostAddress());
    }
  }
  } catch (Exception e) {
  System.out.println(e);
  }
  }
}

My commands class (Wont fit on here): http://pastebin.com/GXFLfMX4

Thanks! If you need more details or don't understand something please tell me so I can help you help me. :)

Answers


To start, I would NOT try to 'merge' the code. That should never be the solution when you receive code from others. You should first try to understand how it was meant to be used.

To that end...

For starters, the Commands class is declared to be in: package server.model.players.packets;

So you will need to import that package. Further, the methods within the Commands class take a Client as a parameter. Therefore, in order to use this class, you would need to be able to instantiate an instance of Client.

Which Client is that? Well, looking at the import statements in Commands, it appears that it would be server.model.players.Client. You would need access to that class and to be able to create an instance.

Were you given the source for Commands or a compile set of classes or a jar? If the former, you will need to have access to all of the other classes that Commands imports in order to compile it.

I suspect that, looking at the verbosity of the Commands class, and your desire to treat it like a black box (but still merge it with your class ?!? (which really is just a wrapper for a main method)), you are really just looking for a quick solution. I would start a lot smaller. Resist the urge to merge. Try to write a Commands class of your own, with a couple simple methods taking classes of your design. You will end up with a process that you understand and a better experience gained.


Need Your Help

NSTimer and accelerometer in iPhone/iPad

iphone ipad accelerometer nstimer

I have a requirement in my app which requires me to display some message to the user if there is no activity happening for about 3 hours on the app/ipad.