Commit 02c85311 authored by Stephen's avatar Stephen
Browse files

Fixed some methods

parent 6092b816
......@@ -2347,7 +2347,6 @@ public class MainPage extends javax.swing.JFrame {
}
}
private ImageIcon getImage(int id, int width, int height)
{
String image="";
......
......@@ -433,99 +433,6 @@ public class MySQLAccess {
return response;
}
public JSONObject sendAccept(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 accept(String offer_token) throws JSONException
{
open();
......@@ -563,9 +470,8 @@ public class MySQLAccess {
close();
return response;
}
public JSONObject fetchitem(int userID, String offer_token) throws JSONException
public JSONObject fetchitem(int userID, String offer_token) throws JSONException
{
open();
JSONObject response = new JSONObject();
......@@ -592,19 +498,17 @@ public class MySQLAccess {
{
preparedStatement = connect.prepareStatement("UPDATE inventory SET "
+ resultSet.getString("item_name").toLowerCase() + "="
+ resultSet.getString("item_name").toLowerCase() + "+" + N_demanded
+ resultSet.getString("item_name").toLowerCase().replace(' ', '_') + "="
+ resultSet.getString("item_name").toLowerCase().replace(' ', '_') + "+" + 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");
......@@ -671,21 +575,18 @@ public class MySQLAccess {
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();
preparedStatement = connect.prepareStatement("UPDATE inventory SET "
+ resultSet.getString("item_name").toLowerCase().replace(' ', '_') + "="
+ resultSet.getString("item_name").toLowerCase().replace(' ', '_') + "+" + N_offered
+ " 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");
......
......@@ -311,50 +311,57 @@ public class TCPServer implements Runnable {
findOfferTime = dateFormat.format(date);
}
System.out.println("Current Time : " + currTime);
System.out.println("Last Find Time: " + findOfferTime);
if(firsttime || moreThan5Minutes(currTime, findOfferTime)) {
JSONObject responseJSON = new JSONObject();
responseJSON.put("status", "ok");
JSONArray offers = new JSONArray();
Vector offerHost = new Vector<Host>();
for(Host server : servers) {
System.out.println(server.ipAddress + " " + server.port);
if(server.ipAddress.equalsIgnoreCase("167.205.32.46") && server.port==8000) {
// do nothing
} else if(server.ipAddress.equalsIgnoreCase(thisIP) && server.port==serverPort) {
// do nothing
} else if(server.ipAddress.equalsIgnoreCase("167.205.32.46")){ // Masih cuma pake kakaknya punya
System.out.println("Searching offer from : " + server.ipAddress + ":" + server.port);
JSONObject tempRequest = new JSONObject();
tempRequest.put("method", "findoffer");
tempRequest.put("item", clientObject.getInt("item"));
Socket tempsocket = new Socket(server.ipAddress, server.port);
tempsocket.setSoTimeout(5000);
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(tempsocket.getOutputStream());
outToServer.write(tempRequest.toString().getBytes("UTF-8"));
System.out.println(tempRequest.toString());
} catch (IOException ex) {
ex.printStackTrace();
}
Socket tempsocket = new Socket(server.ipAddress, server.port);
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(tempsocket.getOutputStream());
outToServer.write(tempRequest.toString().getBytes("UTF-8"));
System.out.println(tempRequest.toString());
} catch (IOException ex) {
ex.printStackTrace();
}
DataInputStream inFromServer = new DataInputStream(tempsocket.getInputStream());
DataInputStream inFromServer = new DataInputStream(tempsocket.getInputStream());
byte b[] = new byte[4096];
inFromServer.read(b);
String response = new String(b, "UTF-8");
byte b[] = new byte[4096];
inFromServer.read(b);
String response = new String(b, "UTF-8");
System.out.println(response);
JSONObject tempResponse = new JSONObject(response);
if(tempResponse.getString("status").equalsIgnoreCase("ok")) {
if(tempResponse.getJSONArray("offers").length()>0) {
for(int i=0; i<tempResponse.getJSONArray("offers").length(); i++) {
offers.put(tempResponse.getJSONArray("offers").get(i));
offerHost.add(new Host(server.ipAddress, server.port));
System.out.println(response);
JSONObject tempResponse = new JSONObject(response);
if(tempResponse.getString("status").equalsIgnoreCase("ok")) {
if(tempResponse.getJSONArray("offers").length()>0) {
for(int i=0; i<tempResponse.getJSONArray("offers").length(); i++) {
offers.put(tempResponse.getJSONArray("offers").get(i));
offerHost.add(new Host(server.ipAddress, server.port));
}
}
}
}
tempsocket.close();
tempsocket.close();
} catch(Exception e) {
System.out.println("Cannot connect to " + server.ipAddress + ":" + server.port);
}
}
}
......
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