Commit f9d10a5f authored by melvinfonda's avatar melvinfonda
Browse files

accept done

parent 7c351592
......@@ -18,7 +18,7 @@ user.properties.file=C:\\Users\\susanti_2\\AppData\\Roaming\\NetBeans\\8.0.2\\bu
<<<<<<< HEAD
user.properties.file=C:\\Users\\susanti_2\\AppData\\Roaming\\NetBeans\\8.0.2\\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
>>>>>>> f5cc2615451f416ad94e5b85c35aa84513a123d3
>>>>>>> 9b3bab9e9eefddca873933beff1cff411b869149
>>>>>>> 4425b3f14619792ceebdcdb264cb7c61d1d9d38c
......
......@@ -3,15 +3,8 @@
<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/susanti_2/Documents/ITB/IF/netbeans/IF3230-Tugas-Besar-Sister-2015/src/client/src/tcpclient/MapTableModel.java</file>
<file>file:/C:/Users/susanti_2/Documents/ITB/IF/netbeans/IF3230-Tugas-Besar-Sister-2015/src/client/src/tcpclient/MainPage.java</file>
<file>file:/C:/Users/susanti_2/Documents/ITB/IF/netbeans/IF3230-Tugas-Besar-Sister-2015/src/client/src/tcpclient/TCPClient.java</file>
=======
<file>file:/C:/Users/Stephen/Documents/NetBeansProjects/TubesSister/src/client/src/tcpclient/MainPage.java</file>
<file>file:/C:/Users/Stephen/Documents/NetBeansProjects/TubesSister/src/client/src/tcpclient/TCPClient.java</file>
<file>file:/C:/Users/Stephen/Documents/NetBeansProjects/TubesSister/src/client/src/tcpclient/LoginPage.java</file>
>>>>>>> 7d12ee92a34d06c0f223c1804ac375528b1b069d
<file>file:/D:/IF3230-Tugas-Besar-Sister-2015/src/client/src/tcpclient/TCPClient.java</file>
<file>file:/D:/IF3230-Tugas-Besar-Sister-2015/src/client/src/tcpclient/MainPage.java</file>
</group>
</open-files>
</project-private>
......@@ -202,6 +202,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 cancelofferJSON(String token, String offer_token) {
JSONObject obj = new JSONObject();
......@@ -536,6 +550,31 @@ public class TCPClient {
return responseJSON;
}
public JSONObject accept(String token,String offer_token) throws Exception
{
openConnection();
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);
closeConnection();
return responseJSON;
}
public JSONObject findoffer(String token, int id_item) throws Exception
{
openConnection();
......@@ -610,10 +649,10 @@ public class TCPClient {
return responseJSON;
}
// public static void main(String argv[]) throws Exception
// {
// TCPClient client = new TCPClient("localhost", 6789);
//
public static void main(String argv[]) throws Exception
{
TCPClient client = new TCPClient("localhost", 6789);
// do {
// BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
// DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
......@@ -715,16 +754,16 @@ public class TCPClient {
// } while(true);
// } while(!command.equalsIgnoreCase("exit"));
//
// JSONObject response = client.login("ayam", "ayam");
// if(response.getString("status").equals("ok")) {
// client.token = response.getString("token");
// client.x=response.getInt("x");
// client.y=response.getInt("y");
// }
// System.out.println(response.toString());
//
// response = client.map();
// System.out.println(response.toString());
JSONObject response = client.login("ayam", "ayam");
if(response.getString("status").equals("ok")) {
client.token = response.getString("token");
client.x=response.getInt("x");
client.y=response.getInt("y");
}
System.out.println(response.toString());
response = client.map();
System.out.println(response.toString());
//
// response = client.offer(client.token, 0, 1, 7, 3);
// System.out.println(response.toString());
......@@ -732,12 +771,12 @@ public class TCPClient {
// response = client.findoffer(client.token, 2);
// System.out.println(response.toString());
//
// response = client.accept(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());
// }
}
//
//
//
......
......@@ -33,7 +33,7 @@ user.properties.file=C:\\Users\\susanti_2\\AppData\\Roaming\\NetBeans\\8.0.2\\bu
user.properties.file=C:\\Users\\susanti_2\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
>>>>>>> f5cc2615451f416ad94e5b85c35aa84513a123d3
=======
user.properties.file=C:\\Users\\Stephen\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.properties
>>>>>>> 7d12ee92a34d06c0f223c1804ac375528b1b069d
>>>>>>> 9b3bab9e9eefddca873933beff1cff411b869149
>>>>>>> 4425b3f14619792ceebdcdb264cb7c61d1d9d38c
......
......@@ -3,19 +3,9 @@
<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/susanti_2/Documents/ITB/IF/netbeans/IF3230-Tugas-Besar-Sister-2015/src/server/src/tcpserver/TCPServer.java</file>
=======
<<<<<<< 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
>>>>>>> 7d12ee92a34d06c0f223c1804ac375528b1b069d
</group>
</open-files>
</project-private>
......@@ -429,6 +429,99 @@ public class MySQLAccess {
return response;
}
public JSONObject accept(int userID, String offer_token) throws JSONException
{
open();
JSONObject response = new JSONObject();
try {
//cek offer dengan offer token yang sesuai
preparedStatement = connect.prepareStatement("SELECT * FROM offer WHERE "
+ "offer_token=\"" + offer_token + "\";");
resultSet = preparedStatement.executeQuery();
if(resultSet.first()) {
boolean available = resultSet.getBoolean("available");
//cek apakah offer tersedia
if(available){
//int id_offer = resultSet.getInt("id_offer");
//simpan variabel offer
int offered_item = resultSet.getInt("offered_item");
int N_offered = resultSet.getInt("n1");
int demanded_item = resultSet.getInt("demanded_item");
int N_demanded = resultSet.getInt("n2");
//mencari item sesuai dengan item yang diinginkan oleh penawar item
preparedStatement = connect.prepareStatement("SELECT item_name FROM items WHERE id_item =" + demanded_item + ";");
resultSet = preparedStatement.executeQuery();
if(resultSet.first())
{
String itemDemandedName = resultSet.getString("item_name").toLowerCase();
preparedStatement = connect.prepareStatement("SELECT * FROM inventory WHERE id_user="
+ userID + ";");
resultSet = preparedStatement.executeQuery();
if(resultSet.first())
{
int N_item = resultSet.getInt(itemDemandedName);
//cek kondisi item yang diinginkan penawar apakah cukup di inventory
if(N_item >= N_demanded ){
preparedStatement = connect.prepareStatement("UPDATE inventory SET "
+ itemDemandedName + "="
+ itemDemandedName + "-" + N_demanded
+ " WHERE id_user=" + userID + ";");
preparedStatement.executeUpdate();
//menambahkan item yang ditawarkan oleh penawar ke dalam inventory user
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();
}
//hapus data offer yang akan diterima
//menjadi tidak available
preparedStatement = connect.prepareStatement("UPDATE offer SET available = 0" + " WHERE "
+ "offer_token=\"" + offer_token + "\";");
preparedStatement.executeUpdate();
response.put("status", "ok");
}
else // jumlah item di inventory < dari jumlah yang diinginkan penawar
{
//menjadi available kembali karena fail
preparedStatement = connect.prepareStatement("UPDATE offer SET available = 1" + " WHERE "
+ "offer_token=\"" + offer_token + "\";");
preparedStatement.executeUpdate();
response.put("status", "fail");
response.put("description", "Can not accept, item in inventory not enough");
}
}
}
}
else //not available
{
response.put("status", "fail");
response.put("description", "Can not accept, offer not available");
}
}
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 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 findoffer(JSONObject clientObject) throws JSONException {
if(clientObject.getString("token").equalsIgnoreCase(token)) {
return sql.findOffer(userID, clientObject.getInt("item"));
......@@ -371,6 +381,11 @@ public class TCPServer implements Runnable {
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