Commit 5c393c17 authored by Stephen's avatar Stephen
Browse files

Added sendfind

parents d5600142 01516b2f
compile.on.save=true
user.properties.file=C:\\Users\\Stephen\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
<<<<<<< HEAD
=======
<<<<<<< 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
=======
<<<<<<< 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
>>>>>>> f5cc2615451f416ad94e5b85c35aa84513a123d3
>>>>>>> 9b3bab9e9eefddca873933beff1cff411b869149
>>>>>>> 4425b3f14619792ceebdcdb264cb7c61d1d9d38c
=======
>>>>>>> 7d12ee92a34d06c0f223c1804ac375528b1b069d
user.properties.file=C:\\Users\\Stephen\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
\ No newline at end of file
......@@ -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
</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();
......@@ -609,11 +648,11 @@ public class TCPClient {
closeConnection();
return responseJSON;
}
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());
......
#Tue, 21 Apr 2015 17:07:39 +0700
#Wed, 22 Apr 2015 16:28:23 +0700
C\:\\Users\\Stephen\\Documents\\NetBeansProjects\\TubesSister\\src\\server=
compile.on.save=true
<<<<<<< HEAD
<<<<<<< HEAD
=======
user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.properties
=======
>>>>>>> 9b3bab9e9eefddca873933beff1cff411b869149
>>>>>>> 4425b3f14619792ceebdcdb264cb7c61d1d9d38c
do.depend=false
do.jar=true
javac.debug=true
javadoc.preview=true
<<<<<<< HEAD
user.properties.file=C:\\Users\\Stephen\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
=======
<<<<<<< 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
=======
<<<<<<< HEAD
<<<<<<< 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
>>>>>>> f5cc2615451f416ad94e5b85c35aa84513a123d3
=======
user.properties.file=C:\\Users\\Stephen\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
>>>>>>> 7d12ee92a34d06c0f223c1804ac375528b1b069d
>>>>>>> 9b3bab9e9eefddca873933beff1cff411b869149
>>>>>>> 4425b3f14619792ceebdcdb264cb7c61d1d9d38c
......@@ -3,25 +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/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>
=======
<<<<<<< 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
>>>>>>> 7c35159221e38ba344dd84636b19fa3f0c29651a
</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();
......
......@@ -32,7 +32,7 @@ public class TCPServer implements Runnable {
serverPort = 6789;
serverClientSocket = new ServerSocket(serverPort);
servers = new ArrayList<Host>();
thisIP = "192.168.1.106";
thisIP = "167.205.71.56";
sql = new MySQLAccess();
}
......@@ -192,6 +192,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(clientObject.getInt("item"));
......@@ -416,6 +426,14 @@ public class TCPServer implements Runnable {
JSONObject responseJSON = server.sendfind(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");
} else if(method.equalsIgnoreCase("serverStatus")) {
JSONObject responseJSON = server.serverstatus(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