Commit ac8f720d authored by Darwin's avatar Darwin
Browse files

cobafind:

parents e4be1102 6092b816
compile.on.save=true
<<<<<<< HEAD
<<<<<<< HEAD
user.properties.file=C:\\Users\\Stephen\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
<<<<<<< HEAD
......@@ -28,3 +29,6 @@ user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.pro
=======
user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.properties
>>>>>>> 5c393c17c0f1ef1dd5ee2823322f7f8ff51533ad
=======
user.properties.file=C:\\Users\\Stephen\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
>>>>>>> 6092b816dd3fdb47e423d8ff58331eee92413992
......@@ -188,12 +188,11 @@ public class TCPClient {
return obj;
}
public JSONObject findOfferJSON(String token, int id_item) {
public JSONObject findOfferJSON(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();
......@@ -267,7 +266,7 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
}
catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
......@@ -275,8 +274,10 @@ public class TCPClient {
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -292,15 +293,17 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver register exception");
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -316,15 +319,17 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver login exception");
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -340,15 +345,17 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver inventory exception");
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -365,7 +372,7 @@ public class TCPClient {
try {
System.out.println("mix mantap");
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
System.out.println("harusnya udah nambah");
} catch (IOException ex) {
System.out.println("error woi mix nya");
......@@ -374,8 +381,10 @@ public class TCPClient {
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -391,15 +400,17 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver map exception");
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -415,15 +426,17 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver offer exception");
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -439,15 +452,17 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver tradebox exception");
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -463,15 +478,17 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver move exception");
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -487,7 +504,7 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver field exception");
......@@ -495,8 +512,10 @@ public class TCPClient {
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -512,15 +531,17 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver tradebox exception");
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -536,21 +557,23 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver send accept exception");
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
}
public JSONObject accept(String token,String offer_token) throws Exception
public JSONObject accept(String token,String offer_token) throws Exception
{
openConnection();
System.out.println("Accept");
......@@ -560,7 +583,7 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver accept exception");
......@@ -568,32 +591,36 @@ public class TCPClient {
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
}
public JSONObject findoffer(String token, int id_item) throws Exception
public JSONObject findoffer(int id_item) throws Exception
{
openConnection();
System.out.println("Find Offer");
String toSend = findOfferJSON(token, id_item).toString();
String toSend = findOfferJSON(id_item).toString();
//throw ioexception
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
} 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();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -609,7 +636,7 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver fetch item exception");
......@@ -617,8 +644,10 @@ public class TCPClient {
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -634,7 +663,7 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver canceloffer exception");
......@@ -642,8 +671,10 @@ public class TCPClient {
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -665,7 +696,7 @@ public class TCPClient {
// System.out.print("Password : "); password = inFromUser.readLine();
// String toSend = registerJSON(username, password).toString();
//
// outToServer.writeBytes(toSend + '\n');
// outToServer.write(toSend.getBytes("UTF-8"));
//
// String response = inFromServer.readLine();
// JSONObject responseJSON = new JSONObject(response);
......@@ -686,7 +717,7 @@ public class TCPClient {
// System.out.print("Password : "); password = inFromUser.readLine();
// String toSend = loginJSON(username, password).toString();
//
// outToServer.writeBytes(toSend + '\n');
// outToServer.write(toSend.getBytes("UTF-8"));
//
// String response = inFromServer.readLine();
// JSONObject responseJSON = new JSONObject(response);
......@@ -711,7 +742,7 @@ public class TCPClient {
// if(command.equalsIgnoreCase("inventory")) {
// String toSend = inventoryJSON(token).toString();
//
// outToServer.writeBytes(toSend + '\n');
// outToServer.write(toSend.getBytes("UTF-8"));
//
// String response = inFromServer.readLine();
// JSONObject responseJSON = new JSONObject(response);
......@@ -738,7 +769,7 @@ public class TCPClient {
// System.out.print("Item 2 ID : "); int id2 = Integer.parseInt(inFromUser.readLine());
// String toSend = mixitemJSON(token, id1, id2).toString();
//
// outToServer.writeBytes(toSend + '\n');
// outToServer.write(toSend.getBytes("UTF-8"));
//
// String response = inFromServer.readLine();
// JSONObject responseJSON = new JSONObject(response);
......@@ -765,11 +796,17 @@ public class TCPClient {
response = client.map();
System.out.println(response.toString());
response = client.offer(client.token, 0, 1, 7, 3);
response = client.offer(client.token, 2, 5, 7, 3);
System.out.println(response.toString());
response = client.offer(client.token, 2, 1, 7, 3);
System.out.println(response.toString());
response = client.sendFind(client.token, 2);
System.out.println(response.toString());
// response = client.findoffer(2);
// System.out.println(response.toString());
}
}
......@@ -12,6 +12,7 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Vector;
import jdk.nashorn.internal.parser.JSONParser;
import org.json.JSONArray;
import org.json.JSONException;
......@@ -330,7 +331,7 @@ public class MySQLAccess {
int difference = resultSet.getInt(resultSet2.getString("item_name").toLowerCase().replace(' ', '_')) - n1;
if(difference >= 0) {
preparedStatement = connect.prepareStatement("INSERT INTO offer "
+ "VALUES(NULL, ?, ?, ?, ?, ?, ?, ?)");
+ "VALUES(NULL, ?, ?, ?, ?, ?, '', 0, ?, ?)");
preparedStatement.setInt(1, userID);
preparedStatement.setInt(2, offered_item);
preparedStatement.setInt(3, n1);
......@@ -399,27 +400,30 @@ public class MySQLAccess {
public JSONObject findOffer(int id_item) throws JSONException {
open();
JSONObject response = new JSONObject();
JSONArray offers = new JSONArray();
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");
while(resultSet.next()) {
int user_id = resultSet.getInt("id_user");
if(user_id != 0)
{
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"));
offers.put(array);
}
}
response.put("status", "ok");
response.put("offers",offers);
} catch (SQLException e) {
response.put("status", "error");
e.printStackTrace();
......@@ -429,7 +433,7 @@ public class MySQLAccess {
return response;
}
public JSONObject accept(int userID, String offer_token) throws JSONException
public JSONObject sendAccept(int userID, String offer_token) throws JSONException
{
open();
JSONObject response = new JSONObject();
......@@ -522,6 +526,45 @@ public class MySQLAccess {
return response;
}
public JSONObject accept(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)
{
preparedStatement = connect.prepareStatement("UPDATE offer SET available = 0" + " WHERE "
+ "offer_token=\"" + offer_token + "\";");
preparedStatement.executeUpdate();
response.put("status", "ok");
}
else //offer tidak tersedia
{
response.put("status", "fail");
response.put("description", "offer is not available");
}
} else {
response.put("status", "fail");
response.put("description", "offer is not available");
}
}
catch (SQLException e) {
response.put("status", "error");
e.printStackTrace();
}
close();
return response;
}
public JSONObject fetchitem(int userID, String offer_token) throws JSONException
{
open();
......@@ -571,6 +614,38 @@ public class MySQLAccess {
return response;
}
public JSONObject sendfind(int id_item) throws JSONException{
open();
JSONObject response = new JSONObject();
JSONArray offers = new JSONArray();
try {
preparedStatement = connect.prepareStatement("SELECT * FROM offer WHERE "
+ "offered_item=" + id_item + " AND "
+ "id_user=0;");
resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
JSONArray offer = new JSONArray();
offer.put(resultSet.getInt("offered_item"));
offer.put(resultSet.getInt("n1"));
offer.put(resultSet.getInt("demanded_item"));
offer.put(resultSet.getInt("n2"));
offer.put(resultSet.getBoolean("available"));
offer.put(resultSet.getString("offer_token"));
offers.put(offer);
}
response.put("status", "ok");
response.put("offers", offers);
} catch (SQLException e) {
response.put("status", "error");
e.printStackTrace();
}
close();
return response;
}
public JSONObject canceloffer(int userID, String offer_token) throws JSONException
{
open();
......@@ -620,6 +695,38 @@ public class MySQLAccess {
return response;
}
public void insertOffer(JSONObject object, Vector<Host> offerHost) throws JSONException{
open();
JSONArray offers =