Commit d597c5f4 authored by melvinfonda's avatar melvinfonda
Browse files

accept offer done

parent 032f2dc2
......@@ -188,6 +188,20 @@ public class TCPClient {
return obj;
}
public JSONObject acceptJSON(String token, String offer_token) {
JSONObject obj = new JSONObject();
try {
obj.put("method", "accept");
obj.put("token", token);
obj.put("offer_token", offer_token);
} catch(Exception e) {
e.printStackTrace();
}
return obj;
}
public JSONObject sendFindJSON(String token,int FindId) {
JSONObject obj = new JSONObject();
try {
......@@ -482,6 +496,30 @@ public class TCPClient {
return responseJSON;
}
public JSONObject accept(String token, String offer_token) throws Exception
{
System.out.println("Accept");
String toSend = acceptJSON(token, offer_token).toString();
//throw ioexception
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver accept exception");
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
JSONObject responseJSON = new JSONObject(response);
return responseJSON;
}
public JSONObject canceloffer(String token, String offer_token) throws Exception
{
System.out.println("Cancel Offer");
......@@ -625,13 +663,16 @@ public class TCPClient {
// response = client.offer(client.token, 0, 1, 7, 3);
// System.out.println(response.toString());
//
// response = client.offer(client.token, 1, 1, 7, 3);
// response = client.canceloffer(client.token, "a");
// System.out.println(response.toString());
//
// response = client.accept(client.token, "a");
// System.out.println(response.toString());
//
// response = client.tradebox(client.token);
// System.out.println(response.toString());
// }
//
//
//
}
......@@ -396,6 +396,55 @@ public class MySQLAccess {
return response;
}
public JSONObject accept(int userID, String offer_token) throws JSONException
{
open();
JSONObject response = new JSONObject();
try {
preparedStatement = connect.prepareStatement("SELECT * FROM offer WHERE "
+ "offer_token=\"" + offer_token + "\";");
resultSet = preparedStatement.executeQuery();
if(resultSet.first()) {
int id_offer = resultSet.getInt("id_offer");
//hapus data offer yang akan di cancel
preparedStatement = connect.prepareStatement("DELETE FROM offer WHERE "
+ "id_offer=" + id_offer + ";");
preparedStatement.executeUpdate();
//masukkan demanded item ke inventory
int demanded_item = resultSet.getInt("demanded_item");
int N_demanded = resultSet.getInt("n2");
preparedStatement = connect.prepareStatement("SELECT item_name FROM items WHERE id_item =" + demanded_item + ";");
resultSet = preparedStatement.executeQuery();
if(resultSet.first())
{
preparedStatement = connect.prepareStatement("UPDATE inventory SET "
+ resultSet.getString("item_name").toLowerCase() + "="
+ resultSet.getString("item_name").toLowerCase() + "+" + N_demanded
+ " WHERE id_user=" + userID + ";");
preparedStatement.executeUpdate();
}
response.put("status", "ok");
}
else
{
response.put("status", "fail");
response.put("description", "Offer not found");
}
} catch (SQLException e) {
response.put("status", "error");
e.printStackTrace();
}
close();
return response;
}
public JSONObject canceloffer(int userID, String offer_token) throws JSONException
{
open();
......
......@@ -188,6 +188,16 @@ public class TCPServer implements Runnable {
return responseJSON;
}
}
public JSONObject accept(JSONObject clientObject) throws JSONException {
if(clientObject.getString("token").equalsIgnoreCase(token)) {
return sql.accept(userID, clientObject.getString("offer_token"));
} else {
JSONObject responseJSON = new JSONObject();
responseJSON.put("status", "error");
return responseJSON;
}
}
public JSONObject tradebox(JSONObject clientObject) throws JSONException {
if(clientObject.getString("token").equalsIgnoreCase(token)) {
......@@ -341,6 +351,10 @@ public class TCPServer implements Runnable {
JSONObject responseJSON = server.tradebox(clientObject);
System.out.println(responseJSON.toString());
outToClient.writeBytes(responseJSON.toString() + "\n");
} else if(method.equalsIgnoreCase("accept")) {
JSONObject responseJSON = server.accept(clientObject);
System.out.println(responseJSON.toString());
outToClient.writeBytes(responseJSON.toString() + "\n");
}
}
}
......
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