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

modify tradebox and client

parents 1e97ef5f f5cc2615
......@@ -13,5 +13,11 @@ src/client/nbbuild/
src/client/nbdist/
src/client/nbactions.xml
src/client/nb-configuration.xml
<<<<<<< HEAD
src/client/build/classes/tcpclient/LoginPage.class
src/client/build/classes/tcpclient/TCPClient.class
.nb-gradle/
/src/client/dist/
\ No newline at end of file
=======
.nb-gradle/
/src/client/dist/
>>>>>>> bc4556fff5c99bc4b898c7dd8720e7cf066d81a5
compile.on.save=true
<<<<<<< HEAD
user.properties.file=C:\\Users\\Stephen\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
=======
<<<<<<< HEAD
<<<<<<< HEAD
user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.properties
=======
......@@ -11,5 +15,10 @@ user.properties.file=C:\\Users\\susanti_2\\AppData\\Roaming\\NetBeans\\8.0.2\\bu
>>>>>>> 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
This diff is collapsed.
This diff is collapsed.
......@@ -20,8 +20,8 @@ public class MapTableModel extends DefaultTableModel{
dimX=x;
dimY=y;
data=new Object [x][y];
for(int i=0; i<y ;i++ ){
for(int j=0;j<x;j++)
for(int i=0; i<x ;i++ ){
for(int j=0;j<y;j++)
{
data[i][j] = "";
}
......@@ -30,12 +30,12 @@ public class MapTableModel extends DefaultTableModel{
@Override
public int getRowCount() {
return dimY;
return dimX;
}
@Override
public int getColumnCount() {
return dimX;
return dimY;
}
@Override
......@@ -44,7 +44,7 @@ public class MapTableModel extends DefaultTableModel{
}
public void setValueAt(Object value, int row, int col) {
data[row-1][col-1] = value;
data[row][col] = value;
fireTableCellUpdated(row, col);
}
......
......@@ -14,12 +14,26 @@ public class TCPClient {
public String token;
public int x;
public int y;
private String ipAddress;
private int port;
public TCPClient(String ipAddress, int port) throws IOException
public TCPClient(String _ipAddress, int _port) throws IOException
{
ipAddress=_ipAddress;
port=_port;
//clientSocket= new Socket(ipAddress, port);
}
private void openConnection() throws IOException
{
clientSocket= new Socket(ipAddress, port);
}
private void closeConnection() throws IOException
{
clientSocket.close();
}
public void printItemList(List inventory) {
System.out.print("Honey " + inventory.get(0) + ", ");
System.out.print("Herbs " + inventory.get(1) + ", ");
......@@ -147,7 +161,8 @@ public class TCPClient {
return obj;
}
private Object tradeboxJSON(String token) {
public JSONObject tradeboxJSON(String token) {
JSONObject obj = new JSONObject();
try {
......@@ -156,10 +171,11 @@ public class TCPClient {
} catch(Exception e) {
e.printStackTrace();
}
return obj;
}
}
private Object sendAcceptJSON(String offerToken) {
private JSONObject sendAcceptJSON(String offerToken) {
JSONObject obj = new JSONObject();
try {
......@@ -186,8 +202,61 @@ public class TCPClient {
return obj;
}
public JSONObject fetchitemJSON(String token, String offer_token) {
JSONObject obj = new JSONObject();
try {
obj.put("method", "fetchitem");
obj.put("token", token);
obj.put("offer_token", offer_token);
} catch(Exception e) {
e.printStackTrace();
}
return obj;
}
public JSONObject sendFindJSON(String token,int FindId) {
JSONObject obj = new JSONObject();
try {
obj.put("method","sendfind");
obj.put("token",token);
obj.put("item", FindId);
}
catch (Exception e) {
e.printStackTrace();
}
return obj;
}
public JSONObject sendFind(String token,int FindId) throws Exception {
openConnection();
System.out.println("sendFind");
String toSend = sendFindJSON(token,FindId).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 login 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 register(String username, String password) throws Exception
{
openConnection();
System.out.println("Register");
String toSend = registerJSON(username, password).toString();
......@@ -205,12 +274,13 @@ public class TCPClient {
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
}
public JSONObject login(String username, String password) throws Exception
{
openConnection();
System.out.println("Login");
String toSend = loginJSON(username, password).toString();
......@@ -228,12 +298,13 @@ public class TCPClient {
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
}
public JSONObject inventory(String token) throws Exception
{
openConnection();
System.out.println("Inventory");
String toSend = inventoryJSON(token).toString();
......@@ -251,21 +322,25 @@ public class TCPClient {
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
}
public JSONObject mixItem(String token, int item1, int item2) throws Exception
{
openConnection();
System.out.println("Mix Item");
String toSend = mixItemJSON(token, item1, item2).toString();
//throw ioexception
DataOutputStream outToServer;
try {
System.out.println("mix mantap");
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
System.out.println("harusnya udah nambah");
} catch (IOException ex) {
System.out.println("error woi mix nya");
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver mix item exception");
}
......@@ -274,12 +349,13 @@ public class TCPClient {
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
}
public JSONObject map() throws Exception
{
openConnection();
System.out.println("Map");
String toSend = mapJSON(token).toString();
......@@ -297,12 +373,13 @@ public class TCPClient {
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
}
public JSONObject offer(String token, int offered_item, int n1, int demanded_item, int n2) throws Exception
{
openConnection();
System.out.println("Offer");
String toSend = offerJSON(token, offered_item, n1, demanded_item, n2).toString();
......@@ -320,12 +397,37 @@ public class TCPClient {
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
}
public JSONObject tradebox(String token) throws Exception
{
openConnection();
System.out.println("Tradebox");
String toSend = tradeboxJSON(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 tradebox 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 move(int x, int y) throws Exception
{
openConnection();
System.out.println("Move");
String toSend = moveJSON(token, x, y).toString();
......@@ -343,12 +445,13 @@ public class TCPClient {
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
}
public JSONObject field() throws Exception
{
openConnection();
System.out.println("Field");
String toSend = fieldJSON(token).toString();
......@@ -367,12 +470,13 @@ public class TCPClient {
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
}
public JSONObject tradebox() throws Exception
{
openConnection();
System.out.println("Tradebox");
String toSend = tradeboxJSON(token).toString();
......@@ -390,12 +494,13 @@ public class TCPClient {
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
}
public JSONObject sendAccept(String offerToken) throws Exception
{
openConnection();
System.out.println("Send Accept");
String toSend = sendAcceptJSON(offerToken).toString();
......@@ -413,12 +518,38 @@ public class TCPClient {
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
}
public JSONObject fetchitem(String token, String offer_token) throws Exception
{
openConnection();
System.out.println("Fetch item");
String toSend = fetchitemJSON(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 fetch item 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 canceloffer(String token, String offer_token) throws Exception
{
openConnection();
System.out.println("Cancel Offer");
String toSend = cancelofferJSON(token, offer_token).toString();
......@@ -437,14 +568,14 @@ public class TCPClient {
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
JSONObject responseJSON = new JSONObject(response);
closeConnection();
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());
......@@ -545,28 +676,31 @@ public class TCPClient {
// System.out.println();
// } 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());
response = client.field();
System.out.println(response.toString());
response = client.offer(client.token, 0, 1, 7, 3);
System.out.println(response.toString());
response = client.canceloffer(client.token, "a");
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());
//
// response = client.canceloffer(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());
// }
//
//
//
}
package tcpclient;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
public class TradeboxTableModel extends AbstractTableModel {
private final String[] header={"Offered Item", "Number Offered", "Demanded Item", "Number Demanded", "Act"};
private final Class[] m_colTypes = { ImageIcon.class, Integer.class, ImageIcon.class, Integer.class, JButton.class };
private Object[][] data;
private int dimX;
private int dimY;
public TradeboxTableModel() {
dimX=0;
dimY=0;
}
public TradeboxTableModel(int x, int y) {
dimX=x;
dimY=y;
data=new Object [x][y];
for(int i=0; i<x ;i++ ){
for(int j=0;j<y;j++)
{
data[i][j] = "";
}
}
}
@Override
public int getRowCount() {
return dimX;
}
@Override
public int getColumnCount() {
return dimY;
}
@Override
public String getColumnName(int col) {
return header[col];
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return data[rowIndex][columnIndex];
}
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
@Override
public Class<?> getColumnClass(int column) {
switch (column) {
case 0:
return ImageIcon.class;
case 1:
return String.class;
case 2:
return ImageIcon.class;
case 3:
return String.class;
case 4:
return JButton.class;
default:
return Object.class;
}
}
}
<<<<<<< HEAD
#Sun, 19 Apr 2015 14:13:30 +0700
C\:\\Users\\susanti_2\\Documents\\ITB\\IF\\netbeans\\IF3230-Tugas-Besar-Sister-2015\\src\\server=
=======
#Sun, 19 Apr 2015 16:30:46 +0700
#Mon, 20 Apr 2015 19:37:20 +0700
C\:\\Users\\Stephen\\Documents\\NetBeansProjects\\TubesSister\\src\\server=
>>>>>>> 9b3bab9e9eefddca873933beff1cff411b869149
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
......@@ -19,6 +26,11 @@ 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
>>>>>>> f5cc2615451f416ad94e5b85c35aa84513a123d3
>>>>>>> 9b3bab9e9eefddca873933beff1cff411b869149
>>>>>>> 4425b3f14619792ceebdcdb264cb7c61d1d9d38c
......@@ -24,7 +24,8 @@ public class MySQLAccess {
private Statement statement;
private PreparedStatement preparedStatement;
private ResultSet resultSet;