Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	.idea/workspace.xml
#	Data/Accounts/Buyers.json
#	Data/Accounts/Sellers.json
#	Data/DisAndOff/Discounts.json
#	Data/DisAndOff/Offs.json
#	Data/IDs/Id.json
  • Loading branch information
parhampch committed Jul 24, 2020
2 parents b5496cd + e5f2a59 commit d3cf958
Show file tree
Hide file tree
Showing 18 changed files with 224 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Data/DisAndOff/Discounts.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[]
[{"discountId":4,"maxValue":100,"percent":20,"startDate":"Oct 20, 2010, 10:10:00 AM","endDate":"Oct 20, 2030, 10:10:00 AM","numberOfTimes":2}]
2 changes: 1 addition & 1 deletion Data/DisAndOff/Offs.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[]
[{"offId":3,"maxValue":10,"percent":100,"startDate":"Oct 10, 2010, 10:10:00 AM","endDate":"Nov 11, 2030, 11:11:00 AM","productIds":[7],"status":"Accepted","sellerUsername":"Seller"},{"offId":4,"maxValue":100,"percent":20,"startDate":"Oct 20, 2010, 10:10:00 AM","endDate":"Oct 20, 2030, 10:10:00 AM","productIds":[7],"status":"Accepted","sellerUsername":"Seller"}]
2 changes: 1 addition & 1 deletion Data/IDs/Id.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[29,27,5,5,11]
[30,27,5,5,11]
2 changes: 1 addition & 1 deletion Data/Products/Products.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"productId":9,"status":"Greate","name":"TextFile","available":true,"number":1,"description":"Goood","averageScore":0.0,"categoryName":"cleaning","price":10,"sellerUsername":"Seller","scores":[],"comments":[],"offId":-1,"doesHasFile":true,"address":"Data/test.txt"},{"productId":10,"status":"greate","name":"shampoo","available":true,"number":23,"description":"good","averageScore":0.0,"categoryName":"cleaning","price":10,"sellerUsername":"Seller","scores":[],"comments":[],"offId":-1,"doesHasFile":false,"address":"DoesNotHaveFile"}]
[{"productId":9,"status":"Greate","name":"TextFile","available":true,"number":1,"description":"Goood","averageScore":0.0,"categoryName":"cleaning","price":10,"sellerUsername":"Seller","scores":[],"comments":[],"offId":-1,"doesHasFile":true,"address":"Data/test.txt"},{"productId":10,"status":"greate","name":"shampoo","available":true,"number":21,"description":"good","averageScore":0.0,"categoryName":"cleaning","price":10,"sellerUsername":"Seller","scores":[],"comments":[],"offId":-1,"doesHasFile":false,"address":"DoesNotHaveFile"}]
18 changes: 13 additions & 5 deletions src/main/java/Controller/AdminManager.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package Controller;

import Model.Account.Account;
import Model.Account.AdminAccount;
import Model.Account.BuyerAccount;
import Model.Account.SellerAccount;
import Model.Account.*;
import Model.Product.Category;
import Model.Product.DiscountAndOff.Discount;
import Model.Product.DiscountAndOff.Off;
Expand All @@ -15,7 +12,7 @@
import java.util.ArrayList;

public class AdminManager {
private static int commission = 5 ;
private static int commission = 10 ;
private static int minimumValue = 100;

public static int getCommission() {
Expand Down Expand Up @@ -67,6 +64,17 @@ public String addNewAdminAccount(String username, String firstName, String lastN
return "New admin account registered.";
}

public String addNewSupporterAccount(String username, String firstName, String lastName, String email, String phoneNumber, String password, int credit) {
if (Database.getAccountByUsername(username) != null)
return "Exist account with this username.";
try {
Database.addAllAccounts(new SupporterAccount(username, firstName, lastName, password, email, phoneNumber, credit));
} catch (Exception e) {
e.printStackTrace();
}
return "New Supporter account registered.";
}

public String showRequestByiId(int id){
Request request = Database.getRequestById(id);
if (request == null)
Expand Down
51 changes: 48 additions & 3 deletions src/main/java/Controller/BuyerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,60 @@ public String bankPay(Account account , int discountId , String address , String
if (discountId!=-1 && !((BuyerAccount) account).canUseDiscount(discountId))
return "You can't use this discount";
else {
String res = bankBuy(discountId,account, address);
String res = bankBuy(discountId , account, address , bankUsername , bankPassword , bankID);
if (res.equals("done successfully"))
return "product bought successfully";
return res;
}
}

private String bankBuy(int discountId, Account account, String address) {
return "";
private String bankBuy(int discountId, Account account, String address , String bankUsername , String bankPassword , String bankId) {
BuyerAccount buyerAccount = (BuyerAccount) account;
long cost = buyerAccount.getCart().getCost();

if (buyerAccount.canUseDiscount(discountId)) {
buyerAccount.useDiscount(discountId);
if (discountId != -1) {
Discount discount = Database.getDiscountById(discountId);
cost = cost - Math.min((int) cost * discount.getPercent() / 100, discount.getMaxValue());
}
}
String res = WorkWithBank.withdraw(Math.toIntExact(cost), bankUsername , bankPassword, bankId);
if (!res.equals("done successfully"))
return res;
if (discountId!=-1 && buyerAccount.canUseDiscount(discountId)) {
buyerAccount.useDiscount(discountId);
}
payToSellerWithBank(discountId, account, address);
buyerAccount.setCart(new Cart());
return res;
}

public void payToSellerWithBank(int discountId, Account account, String addressOfBuyer) {
BuyerAccount buyerAccount = (BuyerAccount) account;
int discountValue = 0;
if (buyerAccount.canUseDiscount(discountId))
discountValue = Database.getDiscountById(discountId).getPercent();
if (discountId == -1)
discountValue = 0;
for (Integer productId : buyerAccount.getCart().getProductsID()) {
Product product = Database.getProductByID(productId);
SellerAccount sellerAccount = (SellerAccount) Database.getAccountByUsername(product.getSellerUsername());
int amount = sellerAccount.getCredit();
sellerAccount.setCredit(sellerAccount.getCredit() + product.getPrice() * buyerAccount.getCart().getMuchOfProductID(productId));
int maxValue = product.getPrice();
int offValue = 0;
if (product.doesHaveOff()) {
maxValue = product.getOff().getMaxValue();
offValue = product.getPrice() * product.getOff().getPercent() / 100;
sellerAccount.setCredit(sellerAccount.getCredit() - Math.min(offValue, maxValue) * buyerAccount.getCart().getMuchOfProductID(productId));
}
amount = sellerAccount.getCredit() - amount;
Account adminAccount = Database.getAccountByUsername("Admin");
adminAccount.setCredit(adminAccount.getCredit() + (amount * AdminManager.getCommission())/100);
sellerAccount.setCredit(sellerAccount.getCredit() - (amount * AdminManager.getCommission())/100);
Log.addLog(buyerAccount.getUsername(), product.getSellerUsername(), product.getPrice(), productId, Math.min(offValue, maxValue), discountValue * product.getPrice() / 100, addressOfBuyer, product.doesHasFile());
}
}

public void joinAuction(Auction auction, Account account){
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/Controller/ClientThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ else if (input.startsWith("AddAdmin"))
details = input.split(" ");
output = adminManager.addNewAdminAccount(details[1], details[2], details[3], details[4], details[5], details[6], Integer.parseInt(details[7]));
}
else if (input.startsWith("AddSupporter")) {
output = adminManager.addNewSupporterAccount(split[1], split[2], split[3], split[4], split[5], split[6], Integer.parseInt(split[7]));
}
else if (input.startsWith("ViewUsername"))
{
Account temp = Database.getAccountByUsername(input.split(" ")[1]);
Expand Down Expand Up @@ -430,6 +433,9 @@ else if (input.startsWith("ReceivedBuyLog"))
{
Objects.requireNonNull(Database.getBuyLogByID(Integer.parseInt(input.split(" ")[1]))).setDeliveryStatus("Received");
}
else if (input.startsWith("getAllSupporters")){
output = Database.getUsers();
}
else if (input.startsWith("addMessage")) {
chatManager.addMessage(Integer.parseInt(split[1]), split[2], account);
} else if (input.startsWith("getChatById")) {
Expand Down Expand Up @@ -468,7 +474,7 @@ else if (input.startsWith("SetMostPriceOfAuction"))
}
else if (input.startsWith("BankPay ")){
details = input.split(" ");
buyerManager.bankPay(account , Integer.parseInt(details[1]), details[2] , details[3], details[4] ,details[5]);
output = buyerManager.bankPay(account , Integer.parseInt(details[1]), details[2] , details[3], details[4] ,details[5]);
}
else if (input.startsWith("Exit"))
{
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/Controller/Database.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package Controller;

import Model.Account.Account;
import Model.Account.AdminAccount;
import Model.Account.BuyerAccount;
import Model.Account.SellerAccount;
import Model.Account.*;
import Model.Log.BuyLog;
import Model.Messaging.Chat;
import Model.Product.Auction;
Expand Down Expand Up @@ -96,6 +93,15 @@ public static Auction getAuctionByID(int auctionId){
}


public static String getUsers() {
String res = "";
for (Account account : allAccounts) {
if (account instanceof BuyerAccount || account instanceof SupporterAccount)
res += account.getUsername() + "\\n";
}
return res;
}

public static Request getRequestById(int id) {
for (Request request : allRequest) {
if (request.getId() == id)
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/Controller/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public class Server {
public static void main(String[] args) {
Server server = new Server();
Database.initialize();
/*try {
try {
WorkWithBank.ConnectToBankServer();
} catch (IOException e) {
e.printStackTrace();
}*/
}
server.waitForClient();
}

Expand Down
12 changes: 11 additions & 1 deletion src/main/java/Controller/WorkWithBank.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.net.Socket;

public class WorkWithBank {
public static final int PORT = 2222;
public static final int PORT = 2020;
public static final String IP = "127.0.0.1";

private static DataOutputStream outputStream;
Expand Down Expand Up @@ -60,4 +60,14 @@ public static String decreaseCredit(int much, String bankUsername, String bankPa
}
return res ;
}

public static String withdraw(int much , String bankUsername, String bankPassword, String bankId) {
String token = get_token(bankUsername , bankPassword);
String res = sendMessage("create_receipt " + token + " withdraw " + much + " " + bankId + " " + -1 + " buying");
if (res.matches("[0-9]+")){
res = sendMessage("pay " + res) ;
}
return res ;

}
}
89 changes: 87 additions & 2 deletions src/main/java/View/Menu/AdminMenus/AdminMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ public void show() {
manageAddAdmin.getStyleClass().add("dark-blue");
manageAddAdmin.setMaxWidth(Double.MAX_VALUE);

Button manageAddSupporter = new Button("Add Supporter");
manageAddSupporter.setOnAction(e -> handleAddSupporter());
manageAddSupporter.getStyleClass().add("dark-blue");
manageAddSupporter.setMaxWidth(Double.MAX_VALUE);


GridPane.setConstraints(editInfoButton, 0, 0);
GridPane.setConstraints(manageUser, 0, 1);
Expand All @@ -116,6 +121,7 @@ public void show() {
GridPane.setConstraints(manageAddAdmin, 0, 7);
GridPane.setConstraints(manageLogs, 0, 8);
GridPane.setConstraints(logout, 0, 9);
GridPane.setConstraints(manageAddSupporter,0,10);
GridPane.setHalignment(manageLogs, HPos.CENTER);
GridPane.setHalignment(editInfoButton, HPos.CENTER);
GridPane.setHalignment(manageUser, HPos.CENTER);
Expand All @@ -126,8 +132,8 @@ public void show() {
GridPane.setHalignment(manageDiscounts, HPos.CENTER);
GridPane.setHalignment(manageAddAdmin, HPos.CENTER);
GridPane.setHalignment(logout, HPos.CENTER);

allButtons.getChildren().addAll(editInfoButton, manageUser, manageRequest, manageProduct, manageCategories, manageDiscounts, manageAddAdmin, logout, onlineUser, manageLogs);
GridPane.setHalignment(manageAddSupporter, HPos.CENTER);
allButtons.getChildren().addAll(editInfoButton, manageUser, manageRequest, manageProduct, manageCategories,manageAddSupporter, manageDiscounts, manageAddAdmin, logout, onlineUser, manageLogs);

try {
dataOutputStream.writeUTF("GetLoggedAccount");
Expand Down Expand Up @@ -1434,4 +1440,83 @@ public void handleAddAdmin()

Menu.window.setScene(scene);
}

public void handleAddSupporter()
{
super.setPane();
Scene scene = new Scene(super.mainPane, 1000, 600);
scene.getStylesheets().add(new File("Data/Styles/Buttons.css").toURI().toString());
scene.getStylesheets().add(new File("Data/Styles/textfield.css").toURI().toString());
scene.getStylesheets().add(new File("Data/Styles/backgrounds.css").toURI().toString());
scene.getStylesheets().add(new File("Data/Styles/choicebox.css").toURI().toString());
super.mainPane.getStyleClass().add("admin-page");

GridPane gridPane = new GridPane();
gridPane.setHgap(20);
gridPane.setVgap(10);
gridPane.setAlignment(Pos.CENTER);
Label status = new Label();
TextField userName = new TextField();
userName.getStyleClass().add("text-field");
userName.setPromptText("username");
TextField firstName = new TextField();
firstName.setPromptText("first name");
TextField lastName = new TextField();
lastName.setPromptText("last name");
TextField email = new TextField();
email.setPromptText("email");
TextField phoneNumber = new TextField();
phoneNumber.setPromptText("phone number");
TextField password = new TextField();
password.setPromptText("password");
TextField credit = new TextField();
credit.setPromptText("credit");

Button register = new Button("Register");
register.getStyleClass().add("dark-blue");
register.setMaxWidth(Double.MAX_VALUE);
register.setOnAction(e -> {
try
{
dataOutputStream.writeUTF("AddSupporter " + userName.getText() + " " + firstName.getText() + " " + lastName.getText() + " " + email.getText() + " "
+ phoneNumber.getText() + " " + password.getText() + " " + credit.getText());
dataOutputStream.flush();
status.setText(dataInputStream.readUTF());
}
catch (Exception ex)
{
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText("Register Fail");
alert.setContentText(ex.getMessage());

alert.showAndWait();
}
});

Button back = new Button("Back");
back.getStyleClass().add("dark-blue");
back.setMaxWidth(Double.MAX_VALUE);
back.setOnAction(e -> show());

GridPane.setConstraints(userName, 0, 0);
GridPane.setConstraints(firstName, 0, 1);
GridPane.setConstraints(lastName, 0, 2);
GridPane.setConstraints(email, 0, 3);
GridPane.setConstraints(phoneNumber, 0, 4);
GridPane.setConstraints(password, 0, 5);
GridPane.setConstraints(credit, 0, 6);
GridPane.setConstraints(register, 0, 7);
GridPane.setConstraints(back, 0, 8);
GridPane.setConstraints(status, 0, 9);
GridPane.setHalignment(register, HPos.CENTER);
GridPane.setHalignment(back, HPos.CENTER);
GridPane.setHalignment(status, HPos.CENTER);

gridPane.getChildren().addAll(userName, firstName, lastName, email, phoneNumber, password, credit, register, back, status);

super.mainPane.setCenter(gridPane);

Menu.window.setScene(scene);
}
}
3 changes: 2 additions & 1 deletion src/main/java/View/Menu/BuyerMenus/BuyerMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import Model.Account.SellerAccount;
import Model.Log.BuyLog;
import Model.Product.DiscountAndOff.Discount;
import View.Menu.ChatRoomMenu;
import View.Menu.Menu;
import View.Menu.UserMenu;
import com.google.gson.Gson;
Expand Down Expand Up @@ -286,7 +287,7 @@ private void handleAllLogs() {

private void handleShowSupporters()
{

new ChatRoomMenu(this).show();
}

private void handleMyAuctions()
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/View/Menu/ChatRoomMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private VBox getSupporters() {
String input = dataInputStream.readUTF();
String[] splitInput = input.split("\\n");
for (String s : splitInput) {
Button button = new Button( "SupporterName: " + s);
Button button = new Button( "User: " + s);
vBox.getChildren().add(button);
button.setOnAction(e -> {
String[] split = button.getText().split(" ");
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/View/Menu/LoginMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import Model.Account.AdminAccount;
import Model.Account.BuyerAccount;
import Model.Account.SellerAccount;
import Model.Account.SupporterAccount;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import javafx.geometry.HPos;
Expand Down Expand Up @@ -111,6 +112,10 @@ else if (role.equalsIgnoreCase("Buyer"))
type = new TypeToken<BuyerAccount>(){}.getType();
Menu.account = new Gson().fromJson(accountInJson, type);
}
else if (role.equalsIgnoreCase("Supporter")) {
type = new TypeToken<SupporterAccount>() {}.getType();
Menu.account = new Gson().fromJson(accountInJson, type);
}
else {
Menu.account = null;
}
Expand Down
Loading

0 comments on commit d3cf958

Please sign in to comment.