diff --git a/README.md b/README.md
index 8826177..8d78fec 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,7 @@
98110349 Arman Babaei
+
98101363 M.Taha Jahani-Nezhad
+
98106072 Ali Mahdavifar
+
+[Server repo](https://github.com/Arman17Babaei/ProjectServer)
diff --git a/pom.xml b/pom.xml
index c1313e1..9b92048 100644
--- a/pom.xml
+++ b/pom.xml
@@ -76,6 +76,17 @@
jfoenix
9.0.8
+
+ io.jsonwebtoken
+ jjwt
+ 0.2
+
+
+ javax
+ javaee-api
+ 6.0
+ provided
+
1.9
diff --git a/src/main/java/controller/CustomerController.java b/src/main/java/controller/CustomerController.java
index f088c51..42970fa 100644
--- a/src/main/java/controller/CustomerController.java
+++ b/src/main/java/controller/CustomerController.java
@@ -1,12 +1,17 @@
package controller;
+import com.google.gson.JsonObject;
import model.*;
import model.exception.DefaultUser;
+import java.net.HttpURLConnection;
+import java.net.URL;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
+import static controller.Database.getJsonObjectFromReader;
+
public class CustomerController extends UserController {
private Customer customerLoggedOn;
@@ -16,7 +21,7 @@ public Customer getCustomerLoggedOn() {
public CustomerController(Customer user, ProductController productController) {
super(user, productController);
- this.customerLoggedOn = (Customer) user;
+ this.customerLoggedOn = user;
}
public static Customer newDefaultUser() {
@@ -30,7 +35,7 @@ private String viewPersonalInfo() {
"Phone number: " + customerLoggedOn.getPhoneNumber() + "\n";
}
- public void changePersonalInfo(HashMap infoToSet) {
+ public void changePersonalInfo(HashMap infoToSet) throws Exception {
super.changePersonalInfo(infoToSet);
}
@@ -40,7 +45,7 @@ public void addAddress(HashMap information) {
public String getPaymentCheck() {
try {
- long totalPrice = purchase();
+ long totalPrice = purchase("e-wallet");
return "Succesfully purchased\n" +
"Total price: " + totalPrice + "\n" +
"Your current credit: " + customerLoggedOn.getCredit();
@@ -49,7 +54,7 @@ public String getPaymentCheck() {
}
}
- public String viewCartProducts() throws Exception {
+ public String viewCartProducts() {
StringBuilder stringToReturn = new StringBuilder();
stringToReturn.append("Product ID\tProduct name\tUnit price\tNumber\n");
HashMap customerCart = customerLoggedOn.getCart();
@@ -101,16 +106,25 @@ public void decreaseNumberOfProduct(String productId) {
removeFromCart(productId);
}
- public long getTotalPrice() throws Exception {
+ public long getTotalPrice() {
long totalPrice = 0;
- for (Product product : customerLoggedOn.getCart().keySet()) {
- totalPrice += product.getPrice() * customerLoggedOn.getCart().get(product);
+ HashMap cart = customerLoggedOn.getCart();
+ for (Product product : cart.keySet()) {
+ totalPrice += cart.get(product) * product.getPrice();
}
if (customerLoggedOn.getDiscountUsed() != null)
totalPrice *= (double) (100 - customerLoggedOn.getDiscountUsed().getDiscountPercent()) / 100;
return totalPrice;
}
+ public long getProductPrice (Product product, int num) {
+ long totalPrice = 0;
+ totalPrice += product.getPrice() * num;
+ if (customerLoggedOn.getDiscountUsed() != null)
+ totalPrice *= (double) (100 - customerLoggedOn.getDiscountUsed().getDiscountPercent()) / 100;
+ return totalPrice;
+ }
+
public boolean validateDiscountCode(String code) {
Discount discount = Database.getDiscountByCode(code);
if (discount != null) {
@@ -131,12 +145,15 @@ public void removeDiscountCode () {
customerLoggedOn.undoUseDiscount();
}
- public long purchase() throws Exception {
+ public long purchase(String method) throws Exception {
long totalPrice = getTotalPrice();
- customerLoggedOn.payCredit(totalPrice);
+ if (method.equalsIgnoreCase("e-wallet") && customerLoggedOn.getCredit() < totalPrice) {
+ throw new Exception("Not enough credit");
+ }
+ payEachSeller(method);
+ //creating log
PurchaseLog newLog = createPurchaseLog();
createSellLogForAllProducts();
- payEachSeller();
customerLoggedOn.deleteDiscount();
Database.add(newLog);
customerLoggedOn.addToPurchaseHistory(newLog);
@@ -146,15 +163,33 @@ public long purchase() throws Exception {
return totalPrice;
}
- private void payEachSeller() {
- for (Product product : customerLoggedOn.getCart().keySet()) {
- Seller seller = product.getSellers().get(0);
- seller.addToCredit(product.getPrice());
- Database.update(seller, seller.getId());
+ private void payEachSeller(String method) throws Exception {
+ HashMap cart = customerLoggedOn.getCart();
+ for (Product product : cart.keySet()) {
+ String destId = product.getSellers().get(0).getId();
+ String reply;
+ if (method.equalsIgnoreCase("e-wallet"))
+ reply = sendCreditPaymentRequest(getProductPrice(product, cart.get(product)), destId);
+ else
+ reply = sendBankPaymentRequest(getProductPrice(product, cart.get(product)), destId);
+ System.out.println(product.getName() + ": " + reply);
}
}
- public void createSellLogForAllProducts() {
+ private String sendBankPaymentRequest(long productPrice, String destId) throws Exception {
+ String url = Database.getServerUrl() + "paySellerByBankAccount" + "?username=" + userLoggedOn.getUsername()
+ + "&password=" + userLoggedOn.getPassword() + "&money=" + productPrice + "&sourceId="
+ + userLoggedOn.getBankAccountId() + "&destId=" + destId;
+ return sendRequestToServer(url);
+ }
+
+ private String sendCreditPaymentRequest(long productPrice, String destId) throws Exception {
+ String url = Database.getServerUrl() + "paySellerCredit" + "?sourceId=" + userLoggedOn.getId()
+ + "&money=" + productPrice + "&destId=" + destId;
+ return sendRequestToServer(url);
+ }
+
+ public void createSellLogForAllProducts() throws Exception {
for (Product product : customerLoggedOn.getCart().keySet()) {
SellLog log = product.createSellLog();
log.setCustomer(customerLoggedOn);
@@ -163,13 +198,13 @@ public void createSellLogForAllProducts() {
}
}
- public void addCustomerToProducts() throws Exception {
+ public void addCustomerToProducts() {
for (Product product : this.customerLoggedOn.getCart().keySet()) {
product.addBuyer(this.customerLoggedOn);
}
}
- private PurchaseLog createPurchaseLog() throws Exception {
+ private PurchaseLog createPurchaseLog() {
PurchaseLog log = new PurchaseLog();
log.setDate(LocalDateTime.now());
log.setAmountPaid(getTotalPrice());
diff --git a/src/main/java/controller/Database.java b/src/main/java/controller/Database.java
index e53539c..1d1d6d3 100644
--- a/src/main/java/controller/Database.java
+++ b/src/main/java/controller/Database.java
@@ -6,70 +6,82 @@
import com.google.gson.JsonObject;
import model.*;
-import java.io.File;
-import java.io.FileReader;
-import java.io.FileWriter;
-import java.io.IOException;
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
public class Database {
- private static final ArrayList allUsers = new ArrayList<>();
- private static final ArrayList allProducts = new ArrayList<>();
- private static final ArrayList allRequests = new ArrayList<>();
- private static final ArrayList allDiscountCodes = new ArrayList<>();
- private static final ArrayList allCategories = new ArrayList<>();
- private static final ArrayList allComments = new ArrayList<>();
- private static final ArrayList allProperties = new ArrayList<>();
- private static final ArrayList allScores = new ArrayList<>();
- private static final ArrayList allPurchaseLogs = new ArrayList<>();
- private static final ArrayList allSellLogs = new ArrayList<>();
- private static final ArrayList allOffs = new ArrayList<>();
- private static final ArrayList allProductAds = new ArrayList<>();
- private static final ArrayList allPossibleManagers = new ArrayList<>();
-
- public static void loadAllData() {
+ private static final String USER_AGENT = "Mozilla/5.0";
+ private static final String serverUrl = "http://localhost:8080/";
+ private static final NetworkArray allUsers = new NetworkArray<>(User.class);
+ private static final NetworkArray allProducts = new NetworkArray<>(Product.class);
+ private static final NetworkArray allRequests = new NetworkArray<>(Request.class);
+ private static final NetworkArray allDiscountCodes = new NetworkArray<>(Discount.class);
+ private static final NetworkArray allCategories = new NetworkArray<>(Category.class);
+ private static final NetworkArray allComments = new NetworkArray<>(Comment.class);
+ private static final NetworkArray allChats = new NetworkArray<>(Chat.class);
+ private static final NetworkArray allProperties = new NetworkArray<>(Property.class);
+ private static final NetworkArray allScores = new NetworkArray<>(Score.class);
+ private static final NetworkArray allPurchaseLogs = new NetworkArray<>(PurchaseLog.class);
+ private static final NetworkArray allSellLogs = new NetworkArray<>(SellLog.class);
+ private static final NetworkArray allOffs = new NetworkArray<>(Off.class);
+ private static final NetworkArray allProductAds = new NetworkArray<>(Product.class);
+ private static final NetworkArray allPossibleManagers = new NetworkArray<>(PossibleManager.class);
+ private static final NetworkArray allPossibleSupporters = new NetworkArray<>(PossibleSupporter.class);
+
+ private static String token = "random-customer";
+
+ public static void loadAllData() throws Exception {
makeDirectories();
loadLists();
}
private static void makeDirectories() {
makeDirectory(Manager.class);
+ makeDirectory(Supporter.class);
makeDirectory(Seller.class);
makeDirectory(Customer.class);
makeDirectory(Product.class);
- makeDirectory(JsonObject.class);
+ makeDirectory(Request.class);
makeDirectory(Discount.class);
makeDirectory(Category.class);
makeDirectory(Comment.class);
+ makeDirectory(Chat.class);
makeDirectory(Property.class);
makeDirectory(Score.class);
makeDirectory(PurchaseLog.class);
makeDirectory(SellLog.class);
makeDirectory(Off.class);
makeDirectory("ProductAd");
- makeDirectory(String.class);
+ makeDirectory(PossibleManager.class);
+ makeDirectory("PossibleSupporter");
}
- private static void loadLists() {
+ private static void loadLists() throws Exception {
loadList(allUsers, Manager.class);
+ loadList(allUsers, Supporter.class);
loadList(allUsers, Seller.class);
loadList(allUsers, Customer.class);
loadList(allProducts, Product.class);
- loadList(allRequests, JsonObject.class);
+ loadList(allRequests, Request.class);
loadList(allDiscountCodes, Discount.class);
loadList(allCategories, Category.class);
loadList(allComments, Comment.class);
+ loadList(allChats, Chat.class);
loadList(allProperties, Property.class);
loadList(allScores, Score.class);
loadList(allPurchaseLogs, PurchaseLog.class);
loadList(allSellLogs, SellLog.class);
loadList(allOffs, Off.class);
loadList(allProductAds, Product.class, "ProductAd");
- loadList(allPossibleManagers, String.class);
+ loadList(allPossibleManagers, PossibleManager.class);
+ loadList(allPossibleSupporters, PossibleSupporter.class);
}
private static String getPath(String folderName) {
- return "Database\\" + folderName + "\\";
+ return "Database\\" + folderName + "\\";
}
private static String getPath(Object object) {
@@ -84,12 +96,13 @@ private static void makeDirectory(String folderName) {
new File(getPath(folderName)).mkdirs();
}
- private static void loadList(ArrayList list, Class extends T> cls) {
+ private static void loadList(ArrayList list, Class extends T> cls) throws Exception {
loadList(list, cls, cls.getSimpleName());
}
- private static void loadList(ArrayList list, Class extends T> cls, String folderName) {
- for (final File fileEntry : new File(getPath(folderName)).listFiles()) {
+ private static void loadList(ArrayList list, Class extends T> cls, String folderName) throws Exception {
+ ((NetworkArray) list).load();
+/* for (final File fileEntry : new File(getPath(folderName)).listFiles()) {
try {
FileReader fileReader = new FileReader(fileEntry);
Object object = new Gson().fromJson(fileReader, cls);
@@ -98,15 +111,135 @@ private static void loadList(ArrayList list, Class extends T> cls, Stri
} catch (Exception e) {
e.printStackTrace();
}
+ }*/
+ }
+
+ public static Object getObjectFromNetwork(String objectId, Class