Commit 0acaf2b3 authored by Rafi Ramadhan's avatar Rafi Ramadhan
Browse files

update tradebox

parent 761076f6
......@@ -11,6 +11,8 @@ import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
......@@ -125,6 +127,10 @@ public class DBHandler {
}
}
public void removefromInventory() {
}
public int getItem(String username, int item) throws SQLException {
int amount = 0;
ResultSet result = statement.executeQuery("select count(item) as amount from inventory where username='"+username+"' and item="+item);
......@@ -143,7 +149,44 @@ public class DBHandler {
return item;
}
public void removefromInventory() {
public void putOffer(String username, Offer offer) {
try {
statement.executeUpdate("insert into tradebox (offer_token, username, item_offered, num_offered, item_demanded, num_demanded, availability)"
+ "values ('" + offer.getToken() + "', '" + username + "', " + offer.getOffered() + ", " + offer.getOffered_num() + ", " + offer.getDemanded() + ", " + offer.getDemanded_num() + ", " + offer.isAvailability() + ")");
} catch (SQLException ex) {
Logger.getLogger(DBHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
public List<Offer> getOffer(String username) throws SQLException {
Offer offer = new Offer();
Offer temp = new Offer();
List<Offer> list = new ArrayList<>();
ResultSet result = statement.executeQuery("select * from tradebox where username='"+username+"'");
while(result.next()) {
offer.setToken(result.getString("offer_token"));
offer.setOffered(result.getLong("item_offered"));
offer.setOffered_num(result.getLong("num_offered"));
offer.setDemanded(result.getLong("item_demanded"));
offer.setDemanded_num(result.getLong("num_demanded"));
offer.setAvailability(result.getBoolean("availability"));
temp = offer.Copy(offer);
list.add(temp);
}
return list;
}
public List<Offer> copyList(List<Offer> target) {
List<Offer> result = new ArrayList<>();
for (int i=0; i<target.size(); i++) {
result.add(target.get(i));
}
return result;
}
}
......@@ -23,6 +23,7 @@ import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
......@@ -101,6 +102,18 @@ public class MethodHandler {
}
public String randString(int len) {
String stream = "abcdefghijklmnopqrstuvwxyz0123456789";
Random rd = new Random();
StringBuilder sb = new StringBuilder(len);
for(int i=0; i<len; i++) {
sb.append(stream.charAt(rd.nextInt(stream.length())));
}
return sb.toString();
}
public String Signup(String input) throws SQLException, ParseException {
DBHandler db = new DBHandler();
......@@ -329,33 +342,80 @@ public class MethodHandler {
return JSONValue.toJSONString(response);
}
public String Offer(String input) throws ParseException {
public boolean checkInventory(int item, int num, String token) throws SQLException {
DBHandler db = new DBHandler();
boolean exist = false;
String username = session.get(searchToken(token)).getUsername();
int amount = db.getItem(username, item);
if(num<=amount) {
exist = true;
}
System.out.println(exist);
return exist;
}
public String Offer(String input) throws ParseException, SQLException, NoSuchAlgorithmException {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
response = parse.offerJSON(true);
DBHandler db = new DBHandler();
Offer trade = new Offer();
String token = parse.parseJSON(input, "token");
String username = session.get(searchToken(token)).getUsername();
String offer_token = genToken(randString(4));
Long item_offered = parse.parseIntJSON(input, "offered_item");
Long num_offered = parse.parseIntJSON(input, "n1");
Long item_demanded = parse.parseIntJSON(input, "demanded_item");
Long num_demanded = parse.parseIntJSON(input, "n2");
if(checkInventory(item_offered.intValue(), num_offered.intValue(), token)){
trade.setToken(offer_token); trade.setOffered(item_offered); trade.setOffered_num(num_offered);
trade.setDemanded(item_demanded); trade.setDemanded_num(num_demanded); trade.setAvailability(true);
db.putOffer(username, trade);
response = parse.offerJSON(true);
//System.out.println();
}
else {
response = parse.offerJSON(false);
}
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
public String TradeBox(String input) throws ParseException {
public String TradeBox(String input) throws ParseException, SQLException {
List<Object> offer = new ArrayList<>();
List<Object> temp = new ArrayList<>();
List<Object> offers = new ArrayList<>();
List<Object> result = new ArrayList<>();
List<Offer> offers = new ArrayList<>();
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
DBHandler db = new DBHandler();
String token = parse.parseJSON(input, "token");
String username = session.get(searchToken(token)).getUsername();
offer.add(5); offer.add(3); offer.add(6);
offer.add(1); offer.add(false); offer.add("we23klf9vv4");
temp = copyList(offer);
offer.clear();
offers.add(temp);
//offers.add(temp);
response = parse.tradeboxJSON(offers);
offers = db.getOffer(username);
for(int i=0; i<offers.size(); i++) {
offer.add(offers.get(i).getToken());
offer.add(offers.get(i).getOffered());
offer.add(offers.get(i).getOffered_num());
offer.add(offers.get(i).getDemanded());
offer.add(offers.get(i).getDemanded_num());
offer.add(offers.get(i).isAvailability());
temp = copyList(offer);
offer.clear();
result.add(temp);
}
response = parse.tradeboxJSON(result);
System.out.println(offers.size());
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
......
......@@ -12,42 +12,42 @@ package grandquestserver;
*/
public class Offer {
public int offered;
public int offered_num;
public int demanded;
public int demanded_num;
public Long offered;
public Long offered_num;
public Long demanded;
public Long demanded_num;
public boolean availability;
public String token;
public int getOffered() {
public Long getOffered() {
return offered;
}
public void setOffered(int offered) {
public void setOffered(Long offered) {
this.offered = offered;
}
public int getOffered_num() {
public Long getOffered_num() {
return offered_num;
}
public void setOffered_num(int offered_num) {
public void setOffered_num(Long offered_num) {
this.offered_num = offered_num;
}
public int getDemanded() {
public Long getDemanded() {
return demanded;
}
public void setDemanded(int demanded) {
public void setDemanded(Long demanded) {
this.demanded = demanded;
}
public int getDemanded_num() {
public Long getDemanded_num() {
return demanded_num;
}
public void setDemanded_num(int demanded_num) {
public void setDemanded_num(Long demanded_num) {
this.demanded_num = demanded_num;
}
......@@ -68,4 +68,21 @@ public class Offer {
}
public Offer Copy(Offer offer) {
Offer copy = new Offer();
copy.setToken(offer.getToken());
copy.setOffered(offer.getOffered());
copy.setOffered_num(offer.getOffered_num());
copy.setDemanded(offer.getDemanded());
copy.setDemanded_num(offer.getDemanded_num());
copy.setAvailability(offer.isAvailability());
return copy;
}
public void printOffer() {
}
}
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