Commit c8b6477f authored by adwisatya's avatar adwisatya
Browse files

Find offers, belum di tes

parent c312a4a7
......@@ -174,7 +174,7 @@ public class Client extends Thread{
case "sendfind":
cmdSendFind(utility.JSONtoString("token",cmd), Integer.parseInt(utility.JSONtoString("item",cmd)));
break;
/* 12 belum */
/* 12 - belum di tes*/
case "findoffer":
cmdFindOffer(new Integer(utility.JSONtoString("itemid",cmd)));
break;
......@@ -407,23 +407,13 @@ public class Client extends Thread{
}
/* Sukses */
public void cmdFindOffer(int itemId) throws IOException, NoSuchAlgorithmException, JSONException{
String response = "";
String request= "";
Socket miniSocket = new Socket(URL,dbPort);
JSONObject obj = new JSONObject();
obj.put("method", "findoffer");
obj.put("token", token);
obj.put("item", new Integer(itemId));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(miniSocket.getOutputStream())));
out.println(obj);
out.println();
out.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(miniSocket.getInputStream()));
response = org.apache.commons.io.IOUtils.toString(in);
in.close();
try {
obj = database.getOfferList(itemId);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
sendToClient(obj.toString());
}
/* Sukses */
public String cmdSendAccept(String token, String offer_token) throws IOException, NoSuchAlgorithmException, JSONException{
......
......@@ -251,6 +251,37 @@ public class Database {
}
return objToSent;
}
public JSONObject getOfferList(int itemid) throws ClassNotFoundException, JSONException{
Connection conn = null;
Statement statement = null;
ResultSet rs;
JSONObject objToSentFinal = new JSONObject();
JSONObject objToSent = new JSONObject();
JSONArray objArr = new JSONArray();
try{
Class.forName(driver);
conn = DriverManager.getConnection(url+dbName,username,password);
statement = conn.createStatement();
rs = statement.executeQuery("SELECT `offered_item`,`n1`,`demanded_item`,`n2` from offers WHERE offered_item="+itemid);
while(rs.next()){
objToSent.put("", rs.getInt("offered_item"));
objToSent.put("", rs.getInt("n1"));
objToSent.put("", rs.getInt("demanded_item"));
objToSent.put("", rs.getString("n2"));
objArr.put(objToSent);
}
System.out.println(objArr.toString());
statement.close();
conn.close();
objToSentFinal.put("status", "ok");
objToSentFinal.put("offers", objArr);
}catch(SQLException e){
objToSentFinal.put("status", "error");
e.printStackTrace();
}
return objToSentFinal;
}
public JSONObject mixItems(String token, String itemid1, String itemid2) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException, NoSuchAlgorithmException{
Connection conn = null;
Statement statement = null;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment