Commit 7c351592 authored by melvinfonda's avatar melvinfonda
Browse files

Find offer kelar

parent d38bec03
......@@ -188,6 +188,20 @@ public class TCPClient {
return obj;
}
public JSONObject findOfferJSON(String token, int id_item) {
JSONObject obj = new JSONObject();
try {
obj.put("method", "findoffer");
obj.put("token", token);
obj.put("item", id_item);
} catch(Exception e) {
e.printStackTrace();
}
return obj;
}
public JSONObject cancelofferJSON(String token, String offer_token) {
JSONObject obj = new JSONObject();
......@@ -522,6 +536,30 @@ public class TCPClient {
return responseJSON;
}
public JSONObject findoffer(String token, int id_item) throws Exception
{
openConnection();
System.out.println("Find Offer");
String toSend = findOfferJSON(token, id_item).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 find offer exception");
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
}
public JSONObject fetchitem(String token, String offer_token) throws Exception
{
openConnection();
......@@ -691,7 +729,7 @@ public class TCPClient {
// response = client.offer(client.token, 0, 1, 7, 3);
// System.out.println(response.toString());
//
// response = client.canceloffer(client.token, "a");
// response = client.findoffer(client.token, 2);
// System.out.println(response.toString());
//
// response = client.accept(client.token, "a");
......
......@@ -396,6 +396,39 @@ public class MySQLAccess {
return response;
}
public JSONObject findOffer(int userID,int id_item) throws JSONException {
open();
JSONObject response = new JSONObject();
try {
preparedStatement = connect.prepareStatement("SELECT * FROM offer WHERE "
+ "offered_item=" + id_item + " AND available = 1;");
resultSet = preparedStatement.executeQuery();
if(resultSet.first()) {
response.put("status", "ok");
JSONArray array = new JSONArray();
array.put(resultSet.getInt("offered_item"));
array.put(resultSet.getInt("n1"));
array.put(resultSet.getInt("demanded_item"));
array.put(resultSet.getInt("n2"));
array.put(resultSet.getBoolean("available"));
array.put(resultSet.getString("offer_token"));
response.put("offers", array);
}
else
{
response.put("status", "error");
}
} catch (SQLException e) {
response.put("status", "error");
e.printStackTrace();
}
close();
return response;
}
public JSONObject fetchitem(int userID, String offer_token) throws JSONException
{
open();
......
......@@ -189,6 +189,16 @@ public class TCPServer implements Runnable {
}
}
public JSONObject findoffer(JSONObject clientObject) throws JSONException {
if(clientObject.getString("token").equalsIgnoreCase(token)) {
return sql.findOffer(userID, clientObject.getInt("item"));
} else {
JSONObject responseJSON = new JSONObject();
responseJSON.put("status", "error");
return responseJSON;
}
}
public JSONObject fetchitem(JSONObject clientObject) throws JSONException {
if(clientObject.getString("token").equalsIgnoreCase(token)) {
return sql.fetchitem(userID, clientObject.getString("offer_token"));
......@@ -356,6 +366,11 @@ public class TCPServer implements Runnable {
System.out.println(responseJSON.toString());
outToClient.writeBytes(responseJSON.toString() + "\n");
}
else if(method.equalsIgnoreCase("findoffer")) {
JSONObject responseJSON = server.findoffer(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