Skip to content

Commit

Permalink
NetWork -buy process
Browse files Browse the repository at this point in the history
  • Loading branch information
MJavadHzr committed Jul 27, 2020
1 parent e577ef9 commit 75b0074
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 26 deletions.
27 changes: 21 additions & 6 deletions src/main/java/Controller/CustomerManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package Controller;

import Model.Account.Account;
import Model.Account.Customer;
import Model.Account.Salesman;
import Model.Cart.Cart;
import Model.Log.BuyLog;
import Model.Off.OffCode;
Expand Down Expand Up @@ -153,13 +155,26 @@ public void setOffCode(String username, String offCodeID) {
Server.setAnswer("Final Price:" + "\n" + customer.getCart().getTotalPrice(offCodeID));
}

public void buy(String username, String offCodeID) {
Customer customer = (Customer) Storage.getAccountWithUsername(username);
assert customer != null;
if (customer.getCart().buy(offCodeID)) {
Server.setAnswer("successful, your purchase completed");
public void buy(Customer customer, String offCodeID, String[] infoLine) {
StringBuilder result = new StringBuilder("ERRORS:");
Cart cart = new Cart(customer.getUsername());
for (String s : infoLine) {
if (s.startsWith("buy")) continue;
String salesmanID = s.split("\\+")[0];
String productID = s.split("\\+")[1];
int count = Integer.parseInt(s.split("\\+")[2]);
if (!cart.addProductToCart(productID, salesmanID, count)) {
result.append("\n").append("there isn't enough number of product [" + productID + "] in [" + salesmanID + "]'s stock!");
}
}
if (result.toString().startsWith("ERRORS:\nthere")) {
Server.setAnswer(result.toString());
return;
}
if (cart.buy(offCodeID)) {
Server.setAnswer("successful, your purchase completed\n" + cart.toString());
} else {
Server.setAnswer("error, you don't have enough credit to purchase");
Server.setAnswer("ERROR, you don't have enough credit to purchase");
}
}
}
11 changes: 8 additions & 3 deletions src/main/java/Controller/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import Model.Account.Role;
import Model.Account.Salesman;
import Model.Auction.Auction;
import Model.Cart.Cart;
import Model.Category.Category;
import Model.Confirmation;
import Model.Log.Log;
Expand Down Expand Up @@ -688,7 +689,7 @@ private void hasUserBought(String command) {
String[] info = command.split("\\+");
String username = info[1];
String productID = info[2];

//setAnswer("yes");
if (Log.hasCustomerBoughtProduct(username, productID)) {
setAnswer("yes");
} else {
Expand Down Expand Up @@ -796,15 +797,19 @@ private void canUserOffCode(String command) {
}

private void buy(String command) {
for (String s : command.split("\n")) {
String[] infoLine = command.split("\n");
Customer customer = (Customer) (Storage.getAccountWithUsername(infoLine[0].split("\\+")[1]));
String offCodeID = infoLine[0].split("\\+")[2];
customerManager.buy(customer, offCodeID, infoLine);
/*for (String s : infoLine) {
if (s.startsWith("buy")) continue;
Product product = Storage.getProductById(s.split("\\+")[1]);
product.setRemainderForSalesman(product.getRemainderForSalesman(s.split("\\+")[0]) - Integer.parseInt(s.split("\\+")[2]), s.split("\\+")[0]);
Account account = Storage.getAccountWithUsername(command.split("\\+")[1]);
Account account1 = Storage.getAccountWithUsername(s.split("\\+")[0]);
((Salesman) account1).setCredit(account.getCredit() + product.getPriceBySalesmanID(s.split("\\+")[0]) * Integer.parseInt(s.split("\\+")[2]));
((Customer) account).setCredit(account.getCredit() - product.getPriceBySalesmanID(s.split("\\+")[0]) * Integer.parseInt(s.split("\\+")[2]));
}
}*/
}

private void getMoney(String command) {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/GUI/Cart/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,4 @@ public void deleteFromCart(ActionEvent actionEvent) throws IOException, ParseExc
}
}
}

}
10 changes: 7 additions & 3 deletions src/main/java/GUI/Cart/Pay.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@ public class Pay {
private String token;

public void buyFromCredit() throws ParseException, IOException {
StringBuilder products = new StringBuilder("buy+" + MenuHandler.getUsername() + "\n");
StringBuilder products = new StringBuilder("buy+" + MenuHandler.getUsername() + "+" + Controller.controller.usedOffCode + "\n");
for (Cart cart : Controller.controller.list) {
products.append(cart.getSalesman()).append("+").append(cart.getProductId()).append("+").append(cart.getCount()).append("\n");
}
MenuHandler.getConnector().clientToServer(products.toString());
if (MenuHandler.getConnector().serverToClient().equals("Buy Successful")) {
Alert alert = new Alert(Alert.AlertType.INFORMATION, "Buy Successful", ButtonType.OK);
String response = MenuHandler.getConnector().serverToClient();
if (response.startsWith("successful")) {
Alert alert = new Alert(Alert.AlertType.INFORMATION, response, ButtonType.OK);
alert.showAndWait();
} else {
Alert alert = new Alert(Alert.AlertType.ERROR, response, ButtonType.OK);
alert.showAndWait();
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/Model/Account/Customer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package Model.Account;

import Controller.CreditController;
import Model.Cart.Cart;
import Model.Off.Off;
import Model.Off.OffCode;
Expand All @@ -20,7 +21,6 @@ public Customer(String username, String password, String firstName, String secon
String role, int credit) {
super(username, password, firstName, secondName, Email, telephone, role);
this.credit = credit;
this.credit = credit;
customerOffCodes = new HashMap<>();
}

Expand All @@ -45,13 +45,18 @@ public void setCredit(int credit) {
}

public boolean isCreditEnoughAccordingToCartWithOffCode(String offCode) {
return (credit > cart.getTotalPrice(offCode));
int minimumCredit = CreditController.creditController.getMinimumCredit();
return (credit - minimumCredit > cart.getTotalPrice(offCode));
}

public Cart getCart() {
return cart;
}

public void setCart(Cart cart) {
this.cart = cart;
}

// public static boolean isCreditEnoughAccordingToCart() {return false;}

public static boolean isThereCustomerWithUsername(String username) {
Expand Down
42 changes: 31 additions & 11 deletions src/main/java/Model/Cart/Cart.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package Model.Cart;

import Controller.CreditController;
import Model.Account.Customer;
import Model.Account.Salesman;
import Model.Log.BuyLog;
import Model.Off.OffCode;
import Model.Off.Sale;
Expand All @@ -23,6 +25,8 @@ public class Cart implements Serializable {

public Cart(String username) {
this.username = username;
Customer customer = (Customer) Storage.getAccountWithUsername(username);
customer.setCart(this);
cartID = RandomString.createID("cartID");
Storage.allCarts.add(this);
}
Expand All @@ -41,13 +45,31 @@ public boolean buy(String offCode) {
new BuyLog(this, offCode);
customer.setCredit(customer.getCredit() - getTotalPrice(offCode));
customer.useOffCode(offCode);
doBuyProductThings();
sendMoneyToStore(offCode);
clearCart();
return true;
} else {
return false;
}
}

private void doBuyProductThings() {
int wagePercentage = CreditController.creditController.getWagePercentage();
for (Triplet<String, String, Integer> item : allItems) {
Product product = Storage.getProductById(item.getValue0());
product.decreaseProductRemaining(item.getValue1(), item.getValue2());
int productCost = Sale.getPriceAfterSale(item.getValue0(), item.getValue1());
Salesman salesman = (Salesman) Storage.getAccountWithUsername(item.getValue1());
salesman.setCredit(salesman.getCredit() + productCost * wagePercentage / 100);
}
}

private void sendMoneyToStore(String offCodeID) {
int totalPrice = getTotalPrice(offCodeID);
//...
}

//updated with Triplet
public void clearCart() {
for (Triplet<String, String, Integer> item : allItems) {
Expand Down Expand Up @@ -75,19 +97,17 @@ public static Cart getCartBasedOnUsername(String username) {


//updated with Triplet
public boolean addProductToCart(String productID, String salesmanID, String cartID) {
Cart cart = getCartWithID(cartID);
assert cart != null;
public boolean addProductToCart(String productID, String salesmanID, int count) {
Triplet<String, String, Integer> item = getItem(productID, salesmanID);
if (item != null) {
if (!Storage.getProductById(productID).isAvailableBySalesmanWithUsername(salesmanID, getItemCount(productID, salesmanID) + 1)) {
if (!Storage.getProductById(productID).isAvailableBySalesmanWithUsername(salesmanID, getItemCount(productID, salesmanID) + count)) {
return false;
}
allItems.remove(item);
allItems.add(item.setAt2(item.getValue2() + 1));//Triplet is immutable :|
return true;
}
if (Storage.getProductById(productID).isAvailableBySalesmanWithUsername(salesmanID, 1)) {
if (Storage.getProductById(productID).isAvailableBySalesmanWithUsername(salesmanID, count)) {
allItems.add(new Triplet<>(productID, salesmanID, 1));
return true;
}
Expand Down Expand Up @@ -188,19 +208,19 @@ private int getTotalPrice() {
private String toStringSingleItem(Triplet<String, String, Integer> item) {
Product product = Storage.getProductById(item.getValue0());
assert product != null;
String result = "Product: " + product.getName() + "\n";
result += "Salesman: " + item.getValue1() + "\n";
result += "Count: " + item.getValue2() + "\n";
result += "Price Per Unit: " + product.getPriceBySalesmanID(item.getValue1()) + "\n";
result += "Price after sale Per Unit: " + Sale.getPriceAfterSale(item.getValue0(), item.getValue1()) + "\n";
String result = "| Product: " + product.getName() + " |---| ";
result += "Salesman: " + item.getValue1() + " |---| ";
result += "Count: " + item.getValue2() + " |---| ";
result += "Price Per Unit: " + product.getPriceBySalesmanID(item.getValue1()) + " |---| ";
result += "Price after sale Per Unit: " + Sale.getPriceAfterSale(item.getValue0(), item.getValue1()) + " |";
return result;
}

//updated with Triplet
public String toString() {
StringBuilder result = new StringBuilder("Here are all of your products in cart:");
for (Triplet<String, String, Integer> item : allItems) {
result.append("\n").append(toStringSingleItem(item)).append("\n---------------------------------");
result.append("\n").append(toStringSingleItem(item)).append("\n-----------------------------------------");
}
result.append("Total price WithOut Using OffCode: ").append(this.getTotalPrice());
return result.toString();
Expand Down

0 comments on commit 75b0074

Please sign in to comment.