Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client #4

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
42d7c82
Ummm... not good.
vahovahovah Jul 14, 2020
aedf97d
some advances.
vahovahovah Jul 18, 2020
9f37ea4
login and logout added.
vahovahovah Jul 19, 2020
258b754
primary payment method added
TahaJahani Jul 22, 2020
57213c2
completed Transactions
TahaJahani Jul 22, 2020
2f27e74
bugs fixed
TahaJahani Jul 22, 2020
4cf1c58
client completed I guess :? <3
vahovahovah Jul 23, 2020
e2514c7
Merge remote-tracking branch 'origin/client' into client
vahovahovah Jul 23, 2020
17c25ee
add Supporter & Chat classes
mahdavifar2002 Jul 23, 2020
f8940d4
Merge remote-tracking branch 'origin/client' into client
vahovahovah Jul 24, 2020
f9a2c30
bug fixes
vahovahovah Jul 24, 2020
55b4fa9
gues p2p should work now.
vahovahovah Jul 24, 2020
2e93181
add file field to products
vahovahovah Jul 24, 2020
7ac6f60
ammm...
vahovahovah Jul 24, 2020
3c90472
eh :(
vahovahovah Jul 25, 2020
e162819
deposit and withdraw credit added
TahaJahani Jul 25, 2020
b0cff4f
eh :(
vahovahovah Jul 26, 2020
e79c788
eh :(
vahovahovah Jul 26, 2020
1a255a8
deletes
vahovahovah Jul 26, 2020
f805dbc
:p
vahovahovah Jul 26, 2020
717bbdb
Merge remote-tracking branch 'origin/client' into client
TahaJahani Jul 26, 2020
d653a9e
eh :(
vahovahovah Jul 26, 2020
fb6aecf
change port to 8080
mahdavifar2002 Jul 26, 2020
c55461c
recover deleted chat classes
mahdavifar2002 Jul 26, 2020
b2d64dc
debug logout
mahdavifar2002 Jul 26, 2020
aa73a02
bugs fixed
TahaJahani Jul 26, 2020
bfdc183
Merge remote-tracking branch 'origin/client' into client
TahaJahani Jul 26, 2020
18d3889
resolve chat page
mahdavifar2002 Jul 26, 2020
d78892b
Merge remote-tracking branch 'origin/client' into client
mahdavifar2002 Jul 26, 2020
6be37b1
fix logout
mahdavifar2002 Jul 26, 2020
275ddb2
fix bug in possible users
mahdavifar2002 Jul 26, 2020
15223fd
Final Commit?
TahaJahani Jul 27, 2020
a370e66
Final Commit?
TahaJahani Jul 27, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
98110349 Arman Babaei

98101363 M.Taha Jahani-Nezhad

98106072 Ali Mahdavifar

[Server repo](https://github.com/Arman17Babaei/ProjectServer)
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@
<artifactId>jfoenix</artifactId>
<version>9.0.8</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.2</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>
<java.version>1.9</java.version>
Expand Down
71 changes: 53 additions & 18 deletions src/main/java/controller/CustomerController.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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() {
Expand All @@ -30,7 +35,7 @@ private String viewPersonalInfo() {
"Phone number: " + customerLoggedOn.getPhoneNumber() + "\n";
}

public void changePersonalInfo(HashMap<String, String> infoToSet) {
public void changePersonalInfo(HashMap<String, String> infoToSet) throws Exception {
super.changePersonalInfo(infoToSet);
}

Expand All @@ -40,7 +45,7 @@ public void addAddress(HashMap<String, String> 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();
Expand All @@ -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<Product, Integer> customerCart = customerLoggedOn.getCart();
Expand Down Expand Up @@ -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<Product, Integer> 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) {
Expand All @@ -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);
Expand All @@ -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<Product, Integer> 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);
Expand All @@ -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());
Expand Down
Loading