Commit ac1a8519 authored by Susanti Gojali's avatar Susanti Gojali
Browse files

fixed conflict

parents df709e5a 6092b816
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
......@@ -2282,15 +2282,13 @@ public class MainPage extends javax.swing.JFrame {
showMessage("Map Error. Try again later..");
}
} catch (Exception ex) {
showMessage("Server map does not respond. Please try again later..");
showMessage("Server does not respond. Please try again later..");
System.out.println("Map Exception");
Logger.getLogger(MainPage.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void generateTradebox(){
System.out.println("masuk generate tradebox");
try {
JSONObject response = client.tradebox();
if(response.getString("status").equals("ok")) {
......@@ -2304,23 +2302,17 @@ public class MainPage extends javax.swing.JFrame {
{
ImageIcon newPlayerIcon = getImage(offer.getJSONArray(i).getInt(0), 100, 100);
tradeboxModel.setValueAt(newPlayerIcon, i, 0);
tradeboxModel.setValueAt(offer.getJSONArray(i).get(1), i, 1);
newPlayerIcon = getImage(offer.getJSONArray(i).getInt(2), 100, 100);
tradeboxModel.setValueAt(newPlayerIcon, i, 2);
tradeboxModel.setValueAt(offer.getJSONArray(i).get(3), i, 3);
if(offer.getJSONArray(i).get(4).toString().equals("true")) //avalaible
{
tradeboxModel.setValueAt("Cancel", i, 4);
//
if(offer.getJSONArray(i).get(4).toString().equals("true")){ //avalaible
tradeboxModel.setValueAt("Cancel", i, 4);
}else{
tradeboxModel.setValueAt("Fetch", i, 4);
}
}
tradeboxTable.setModel(tradeboxModel);
tradeboxTable.setRowHeight(100);
......@@ -2347,12 +2339,11 @@ public class MainPage extends javax.swing.JFrame {
};
ButtonColumn buttonColumn = new ButtonColumn(tradeboxTable, delete, 4);
buttonColumn.setMnemonic(KeyEvent.VK_D);
} else {
showMessage("Tradebox Error. Try again later..");
}
} catch (Exception ex) {
showMessage("Server tradebox does not respond. Please try again later..");
showMessage("Server does not respond. Please try again later..");
System.out.println("Tradebox Exception");
Logger.getLogger(MainPage.class.getName()).log(Level.SEVERE, null, ex);
}
......@@ -2384,13 +2375,14 @@ public class MainPage extends javax.swing.JFrame {
}
private void fetchItem(String offerToken)
{
try {
JSONObject response = client.fetchItem(offerToken);
if(response.getString("status").equals("ok")){
showMessage("Success");
showMessage("Success!");
generateTradebox();
}else if(response.getString("status").equals("fail")){
showMessage(response.getString("description"));
}else{
......@@ -2403,12 +2395,15 @@ public class MainPage extends javax.swing.JFrame {
}
}
private void cancelOffer(String offerToken)
{
try {
JSONObject response = client.cancelOffer(offerToken);
if(response.getString("status").equals("ok")){
showMessage("Cancel offer success!");
generateTradebox();
}else if(response.getString("status").equals("fail")){
showMessage(response.getString("description"));
}else{
......@@ -2416,7 +2411,7 @@ public class MainPage extends javax.swing.JFrame {
}
} catch (Exception ex) {
showMessage("Server does not respond. Please try again later..");
System.out.println("Fetch Item Exception");
System.out.println("Cancel Offer Exception");
Logger.getLogger(MainPage.class.getName()).log(Level.SEVERE, null, ex);
}
}
......
This diff is collapsed.
#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
<<<<<<< HEAD
=======
user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.properties
......@@ -38,3 +38,6 @@ user.properties.file=C:\\Users\\susanti_2\\AppData\\Roaming\\NetBeans\\8.0.2\\bu
>>>>>>> 9b3bab9e9eefddca873933beff1cff411b869149
>>>>>>> 4425b3f14619792ceebdcdb264cb7c61d1d9d38c
=======
user.properties.file=C:\\Users\\Stephen\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
>>>>>>> 6092b816dd3fdb47e423d8ff58331eee92413992
......@@ -3,7 +3,13 @@
<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>
=======
<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>
>>>>>>> 6092b816dd3fdb47e423d8ff58331eee92413992
</group>
</open-files>
</project-private>
......@@ -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);
......@@ -396,6 +397,174 @@ public class MySQLAccess {
return response;
}
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();
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();
}
close();
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();
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();
......@@ -445,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();
......@@ -494,6 +695,38 @@ public class MySQLAccess {
return response;
}
public void insertOffer(JSONObject object, Vector<Host> offerHost) throws JSONException{
open();
JSONArray offers = object.getJSONArray("offers");
try {
// Hapus dari basis data semua offer milik server lain
preparedStatement = connect.prepareStatement("DELETE FROM offer WHERE "
+ "id_user=0;");
preparedStatement.executeUpdate();
// Tambahkan ke basis data semua offer baru dari server lain
if(offers.length()>0) {
for(int i=0; i<offers.length(); i++) {
JSONArray offer = (JSONArray) offers.get(i);
preparedStatement = connect.prepareStatement("INSERT INTO offer "
+ "VALUES(NULL, 0, ?, ?, ?, ?, ?, ?, 1, ?)");
preparedStatement.setInt(1, offer.getInt(0));
preparedStatement.setInt(2, offer.getInt(1));
preparedStatement.setInt(3, offer.getInt(2));
preparedStatement.setInt(4, offer.getInt(3));
preparedStatement.setString(5, offerHost.get(i).ipAddress);
preparedStatement.setInt(6, offerHost.get(i).port);
preparedStatement.setString(7, offer.getString(5));
preparedStatement.executeUpdate();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
close();
}
public void createInventory(String username) {
open();
try {
......@@ -531,4 +764,110 @@ public class MySQLAccess {
close();
return id;
}
public void deleteOffer(String offer_token) {
open();
try {
preparedStatement = connect.prepareStatement("DELETE FROM offer WHERE "
+ "offer_token=\"" + offer_token + "\";");
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
close();
}
public void commitAcceptOffer(int userID, String offer_token) {
open();
try {
preparedStatement = connect.prepareStatement("SELECT * FROM offer WHERE "
+ "offer_token=\"" + offer_token + "\";");
resultSet = preparedStatement.executeQuery();
if(resultSet.first()) {
String offered_item = "", demanded_item = "";
// Ambil nama item yang di offer
preparedStatement = connect.prepareStatement("SELECT * FROM items WHERE "
+ "id_item=\"" + resultSet.getInt("offered_item") + "\";");
ResultSet resultSet2 = preparedStatement.executeQuery();
if(resultSet2.first()) {
offered_item = resultSet2.getString("item_name");
}
// Ambil nama item yang di demand
preparedStatement = connect.prepareStatement("SELECT * FROM items WHERE "
+ "id_item=\"" + resultSet.getInt("demanded_item") + "\";");
resultSet2 = preparedStatement.executeQuery();
if(resultSet2.first()) {
demanded_item = resultSet2.getString("item_name");
}
// Kurangin inventory user dengan yg demanded, dan tambah dengan yg offered
preparedStatement = connect.prepareStatement("UPDATE inventory SET "
+ offered_item + "=" + offered_item + "+" + resultSet.getInt("n1") + ", "
+ demanded_item + "=" + demanded_item + "-" + resultSet.getInt("n2") + " WHERE "
+ "id_user=" + userID + ";");
preparedStatement.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
}
close();
}
public boolean isItemEnough(int userID, String offer_token) {
open();
boolean enough = true;
try {
preparedStatement = connect.prepareStatement("SELECT * FROM offer WHERE "
+ "offer_token=\"" + offer_token + "\";");
resultSet = preparedStatement.executeQuery();
if(resultSet.first()) {
int demanded_count = resultSet.getInt("n2");
preparedStatement = connect.prepareStatement("SELECT * FROM items WHERE "
+ "id_item=" + resultSet.getInt("demanded_item") + ";");
resultSet = preparedStatement.executeQuery();
if(resultSet.first()) {
String demanded_item = resultSet.getString("item_name").toLowerCase().replace(' ', '_');
preparedStatement = connect.prepareStatement("SELECT * FROM inventory WHERE "
+ "id_user=" + userID + ";");
resultSet = preparedStatement.executeQuery();
if(resultSet.first()) {
if(resultSet.getInt(demanded_item) < demanded_count) {
enough = false;
}
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
close();
return enough;
}
public Host getHostByOfferToken(String offer_token)
{
open();
Host host = null;
try {
preparedStatement = connect.prepareStatement("SELECT * FROM offer WHERE "
+ "offer_token=\"" + offer_token + "\";");
resultSet = preparedStatement.executeQuery();
if(resultSet.first()) {
host.ipAddress = resultSet.getString("ip");
host.port = resultSet.getInt("port");
}
} catch (SQLException e) {
e.printStackTrace();
}
close();
return host;
}
}
This diff is collapsed.
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