Commit 919e3191 authored by melvinfonda's avatar melvinfonda
Browse files

tambah cancel offer

parent 52eb16cb
......@@ -3,5 +3,5 @@ compile.on.save=true
<<<<<<< HEAD
user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.properties
=======
user.properties.file=C:\\Users\\susanti_2\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.properties
>>>>>>> 43e41c8c08f84039568ca701a584f38f9911e548
......@@ -147,6 +147,20 @@ public class TCPClient {
return obj;
}
public JSONObject cancelofferJSON(String token, String offer_token) {
JSONObject obj = new JSONObject();
try {
obj.put("method", "canceloffer");
obj.put("token", token);
obj.put("offer_token", offer_token);
} catch(Exception e) {
e.printStackTrace();
}
return obj;
}
public JSONObject register(String username, String password) throws Exception
{
System.out.println("Register");
......@@ -320,7 +334,30 @@ public class TCPClient {
outToServer.writeBytes(toSend + '\n');
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver move exception");
throw new Exception("outtoserver field 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");
String toSend = cancelofferJSON(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 canceloffer exception");
}
//ambil balasan server
......@@ -452,6 +489,9 @@ public class TCPClient {
response = client.offer(client.token, 0, 1, 7, 3);
System.out.println(response.toString());
response = client.canceloffer(client.token, "a");
System.out.println(response.toString());
}
......
......@@ -7,5 +7,5 @@ javadoc.preview=true
user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.properties
user.properties.file=C:\\Users\\susanti_2\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.properties
......@@ -353,6 +353,40 @@ public class MySQLAccess {
return response;
}
public JSONObject canceloffer(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 + ";");
System.out.println(preparedStatement.toString());
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 void createInventory(String username) {
open();
try {
......
......@@ -127,6 +127,16 @@ public class TCPServer {
}
}
public JSONObject canceloffer(JSONObject clientObject) throws JSONException {
if(clientObject.getString("token").equalsIgnoreCase(token)) {
return sql.canceloffer(userID, clientObject.getString("offer_token"));
} else {
JSONObject responseJSON = new JSONObject();
responseJSON.put("status", "error");
return responseJSON;
}
}
public static void main(String[] args) throws Exception {
int port = 6789;
TCPServer server = new TCPServer(port);
......@@ -188,6 +198,11 @@ public class TCPServer {
System.out.println(responseJSON.toString());
outToClient.writeBytes(responseJSON.toString() + "\n");
}
else if(method.equalsIgnoreCase("canceloffer")) {
JSONObject responseJSON = server.canceloffer(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