Commit 51d3fd1a authored by Stephen's avatar Stephen
Browse files

Merged

parents 4f932493 f5cc2615
compile.on.save=true
<<<<<<< HEAD
user.properties.file=C:\\Users\\Stephen\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
=======
<<<<<<< HEAD
<<<<<<< HEAD
user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.properties
=======
<<<<<<< HEAD
=======
>>>>>>> 2e2ad538abfe623e38cc24be02c48f5ad2ee0768
user.properties.file=C:\\Users\\susanti_2\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
>>>>>>> 43e41c8c08f84039568ca701a584f38f9911e548
=======
user.properties.file=C:\\Users\\Stephen\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
>>>>>>> 9b3bab9e9eefddca873933beff1cff411b869149
>>>>>>> 4425b3f14619792ceebdcdb264cb7c61d1d9d38c
......@@ -188,6 +188,20 @@ public class TCPClient {
return obj;
}
public JSONObject fetchitemJSON(String token, String offer_token) {
JSONObject obj = new JSONObject();
try {
obj.put("method", "fetchitem");
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 fetchitem(String token, String offer_token) throws Exception
{
System.out.println("Fetch item");
String toSend = fetchitemJSON(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 fetch item 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());
// }
//
//
//
}
#Mon, 20 Apr 2015 19:37:20 +0700
#Tue, 21 Apr 2015 17:07:39 +0700
C\:\\Users\\Stephen\\Documents\\NetBeansProjects\\TubesSister\\src\\server=
......@@ -3,9 +3,15 @@
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group>
<<<<<<< HEAD
<file>file:/C:/Users/Stephen/Documents/NetBeansProjects/TubesSister/src/server/src/tcpserver/TCPServer.java</file>
<file>file:/C:/Users/Stephen/Documents/NetBeansProjects/TubesSister/src/server/src/tcpserver/Host.java</file>
<file>file:/C:/Users/Stephen/Documents/NetBeansProjects/TubesSister/src/server/src/tcpserver/MySQLAccess.java</file>
=======
<file>file:/D:/IF3230-Tugas-Besar-Sister-2015/src/server/map.txt</file>
<file>file:/D:/IF3230-Tugas-Besar-Sister-2015/src/server/src/tcpserver/MySQLAccess.java</file>
<file>file:/D:/IF3230-Tugas-Besar-Sister-2015/src/server/src/tcpserver/TCPServer.java</file>
>>>>>>> f5cc2615451f416ad94e5b85c35aa84513a123d3
</group>
</open-files>
</project-private>
......@@ -396,6 +396,55 @@ public class MySQLAccess {
return response;
}
public JSONObject fetchitem(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();
......@@ -411,8 +460,23 @@ public class MySQLAccess {
//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();
//masukkan kembali data offer ke inventory
int offered_item = resultSet.getInt("offered_item");
int N_offered = resultSet.getInt("n1");
preparedStatement = connect.prepareStatement("SELECT item_name FROM items WHERE id_item =" + offered_item + ";");
resultSet = preparedStatement.executeQuery();
if(resultSet.first())
{
preparedStatement = connect.prepareStatement("UPDATE inventory SET "
+ resultSet.getString("item_name").toLowerCase() + "="
+ resultSet.getString("item_name").toLowerCase() + "+" + N_offered
+ " WHERE id_user=" + userID + ";");
preparedStatement.executeUpdate();
}
response.put("status", "ok");
}
......
......@@ -188,6 +188,16 @@ public class TCPServer implements Runnable {
return responseJSON;
}
}
public JSONObject fetchitem(JSONObject clientObject) throws JSONException {
if(clientObject.getString("token").equalsIgnoreCase(token)) {
return sql.fetchitem(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("fetchitem")) {
JSONObject responseJSON = server.fetchitem(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