Skip to content

Commit

Permalink
complete Auction
Browse files Browse the repository at this point in the history
  • Loading branch information
parhampch committed Jul 24, 2020
1 parent ef77d51 commit b01e39c
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 58 deletions.
96 changes: 54 additions & 42 deletions .idea/workspace.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Data/Accounts/Buyers.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"buyLogs":[{"discountValue":0,"sellerUsername":"Seller","addressOfBuyer":"home","wasProductHasFile":false,"date":"2020/07/24 01:00:20","price":10,"deliveryStatus":"Received","logId":26,"productId":10},{"discountValue":0,"sellerUsername":"Seller","addressOfBuyer":"my","wasProductHasFile":false,"date":"2020/07/24 01:04:44","price":10,"deliveryStatus":"Received","logId":27,"productId":10}],"discountIds":[1,2,3,4],"numberOfUse":{"1":0,"2":2,"3":2,"4":2},"cart":{"productsID":[],"muchOfProductID":{}},"username":"Buyer","firstName":"Buyer","lastName":"Buyer","password":"Buyer","email":"[email protected]","phoneNumber":"021","credit":8731},{"buyLogs":[],"discountIds":[1,2,4],"numberOfUse":{"1":0,"2":0,"4":0},"cart":{"productsID":[],"muchOfProductID":{}},"username":"parham","firstName":"parham","lastName":"chavoshian","password":"parham","email":"[email protected]","phoneNumber":"021","credit":99970},{"buyLogs":[],"discountIds":[],"numberOfUse":{},"cart":{"productsID":[],"muchOfProductID":{}},"username":"milad","firstName":"milad","lastName":"saadat","password":"salam","email":"[email protected]","phoneNumber":"1231","credit":12312}]
[{"buyLogs":[],"discountIds":[],"numberOfUse":{},"cart":{"productsID":[],"muchOfProductID":{}},"username":"Buyer","firstName":"Buyer","lastName":"Buyer","password":"Buyer","email":"[email protected]","phoneNumber":"021","credit":10000}]
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}]
[{"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"}]
[{"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"}]
20 changes: 16 additions & 4 deletions src/main/java/Controller/AccountManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,24 @@ public String register(String role,String username, String firstName, String las
if (Database.getAccountByUsername(username) != null)
return "Exist account with this username.";
if (role.equalsIgnoreCase("Buyer")) {
Database.addAllAccounts(new BuyerAccount(username, firstName, lastName, password, email, phoneNumber, credit));
return "New buyer account registered.";
try {
Database.addAllAccounts(new BuyerAccount(username, firstName, lastName, password, email, phoneNumber, credit));
return "New buyer account registered.";
}
catch (Exception e)
{
return e.getMessage();
}
}
else if (role.equalsIgnoreCase("Seller")) {
Database.addRequest(new NewSellerRequest(username, firstName, lastName, password, email, company, credit, phoneNumber));
return "Your Request registered";
try {
Database.addRequest(new NewSellerRequest(username, firstName, lastName, password, email, company, credit, phoneNumber));
return "Your Request registered";
}
catch (Exception e)
{
return e.getMessage();
}
}
else
return "Role is invalid!";
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/Controller/BuyerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import Model.Product.DiscountAndOff.Discount;
import Model.Product.Product;

import java.util.Objects;

public class BuyerManager {

public String showAllDiscounts(Account account) {
Expand Down Expand Up @@ -122,4 +124,12 @@ public void payToSeller(int discountId, Account account, String addressOfBuyer)
public void joinAuction(Auction auction, Account account){
auction.joinAuction((BuyerAccount) account);
}

public String setPriceForAuction(int auctionID, int price, Account account)
{
Auction auction = Database.getAuctionByID(auctionID);
if (!Objects.requireNonNull(auction).setMostPrice(price, (BuyerAccount) account))
return "Fail to set price";
return "Your price submitted";
}
}
21 changes: 20 additions & 1 deletion src/main/java/Controller/ClientThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -443,10 +443,29 @@ else if (input.startsWith("addMessage")) {
arrayList.add(account);
arrayList.add((Database.getAccountByUsername(split[1])));
Chat chat = new Chat(arrayList);
buyerAccount.addChat(chat);
//buyerAccount.addChat(chat);
output = String.valueOf(chat.getId());
}
}
else if (input.startsWith("GetMPBOfAuction"))
{
try {
output = new Gson().toJson(Objects.requireNonNull(Database.getAuctionByID(Integer.parseInt(input.split(" ")[1]))).getBuyerWithMostPrice().getUsername());
}
catch (Exception e)
{
output = "NoOne";
}
}
else if (input.startsWith("GetMPOfAuction"))
{
output = new Gson().toJson(Objects.requireNonNull(Database.getAuctionByID(Integer.parseInt(input.split(" ")[1]))).getMostPrice());
}
else if (input.startsWith("SetMostPriceOfAuction"))
{
details = input.split(" ");
output = buyerManager.setPriceForAuction(Integer.parseInt(details[1]), Integer.parseInt(details[2]), account);
}
else if (input.startsWith("Exit"))
{
clientSocket.close();
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/Controller/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public static Chat getChatById(int id) {
return null;
}


public static ArrayList<Chat> getAllChats() {
return allChats;
}

public static ArrayList<Account> getAllAccounts() {
return allAccounts;
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/Model/Account/BuyerAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public class BuyerAccount extends Account {
private ArrayList<Integer> discountIds = new ArrayList<>();
private HashMap<Integer, Integer> numberOfUse = new HashMap<>();
private Cart cart;
private ArrayList<Chat> chats = new ArrayList<>();
//private ArrayList<Chat> chats = new ArrayList<>();


public void addChat(Chat chat) {
/*public void addChat(Chat chat) {
chats.add(chat);
}
}*/

public BuyerAccount(String username, String firstName, String lastName, String password, String email, String phoneNumber, int credit) throws Exception {
super(username, firstName, lastName, password, email, phoneNumber, credit);
Expand All @@ -30,7 +30,8 @@ public BuyerAccount(String username, String firstName, String lastName, String p

public Chat hasChatWith(Account account) {
ArrayList<Account> arrayList;
for (Chat chat : chats) {
ArrayList<Chat> allChats = Database.getAllChats();
for (Chat chat : allChats) {
arrayList = chat.getMembers();
if (arrayList.size() == 2 && arrayList.contains(account))
return chat;
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/Model/Product/Auction.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public Auction(Product product, Date endDate)
mostPrice = 0;
currentDate = new Date();
this.auctionID = numberOfAllAuctions;
allBuyers = new ArrayList<>();
numberOfAllAuctions++;
}

Expand Down Expand Up @@ -88,4 +89,12 @@ private void payAndFinish()
Objects.requireNonNull(Database.getAccountByUsername(product.getSellerUsername())).setCredit((int) (Objects.requireNonNull(Database.getAccountByUsername(product.getSellerUsername())).getCredit() + mostPrice));
Log.addLog(buyerWithMostPrice.getUsername(), product.getSellerUsername(), (int) mostPrice, product.getProductId(), 0, 0, "Bought in Auction", product.doesHasFile);
}

public BuyerAccount getBuyerWithMostPrice() {
return buyerWithMostPrice;
}

public long getMostPrice() {
return mostPrice;
}
}
71 changes: 69 additions & 2 deletions src/main/java/View/Menu/BuyerMenus/BuyerMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,11 @@ private void handleMyAuctions()
button.getStyleClass().add("dark-blue");
button.setAlignment(Pos.CENTER);
button.setOnAction(e -> {
handleShowAuction(string);
Stage newWindow = new Stage();
handleShowAuction(string, newWindow, "");
newWindow.initModality(Modality.APPLICATION_MODAL);
newWindow.showAndWait();
newWindow.setOnCloseRequest(event -> handleMyAuctions());
});
GridPane.setConstraints(label, 0, i);
GridPane.setConstraints(button, 2, i);
Expand Down Expand Up @@ -368,7 +372,70 @@ private void handleShowLog(BuyLog buyLog) {
newWindow.showAndWait();
}

private void handleShowAuction(String string){}
private void handleShowAuction(String string, Stage newWindow, String submitStatus){
try {
Pane pane = new GridPane();
((GridPane)pane).setAlignment(Pos.CENTER);
Scene scene = new Scene(pane, 600, 400);
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());
pane.getStyleClass().add("admin-popup");

Label auctionStatus = new Label();
String aStatus = "";
auctionStatus.setFont(Font.font(20));
dataOutputStream.writeUTF("GetMPBOfAuction " + string.split(":")[1].split("_")[0]);
dataOutputStream.flush();
aStatus += dataInputStream.readUTF();
aStatus += ":";
dataOutputStream.writeUTF("GetMPOfAuction " + string.split(":")[1].split("_")[0]);
dataOutputStream.flush();
aStatus += dataInputStream.readUTF();
auctionStatus.setText(aStatus);

Label submitStatus1 = new Label();
submitStatus1.setFont(Font.font(20));
submitStatus1.setText(submitStatus);

TextField textField = new TextField();
textField.setPromptText("New Price");
textField.getStyleClass().add("textfield.css");

Button submit = new Button("Submit");
submit.getStyleClass().add("dark-blue");
submit.setMaxWidth(Double.MAX_VALUE);
submit.setOnAction(e -> {
try {
String id = string.split(":")[1].split("_")[0];
dataOutputStream.writeUTF("SetMostPriceOfAuction " + id + " " + textField.getText());
dataOutputStream.flush();
handleShowAuction(string, newWindow, dataInputStream.readUTF());
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
});
GridPane.setConstraints(auctionStatus, 0, 0);
GridPane.setConstraints(textField, 0, 1);
GridPane.setConstraints(submit, 0, 2);
GridPane.setConstraints(submitStatus1, 0, 3);

GridPane.setHalignment(auctionStatus, HPos.CENTER);
GridPane.setHalignment(textField, HPos.CENTER);
GridPane.setHalignment(submit, HPos.CENTER);
GridPane.setHalignment(submitStatus1, HPos.CENTER);
((GridPane)pane).setVgap(10);

pane.getChildren().addAll(auctionStatus, textField, submit, submitStatus1);

newWindow.setScene(scene);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}

public void handleEdit()
{
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/View/Menu/LoginMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ else if (newValue.equalsIgnoreCase("Seller"))
if (role.getValue().equalsIgnoreCase("Buyer"))
{
dataOutputStream.writeUTF("register " + role.getValue() + " " + userName.getText() + " " + firstName.getText() + " " + lastName.getText() + " " + email.getText() + " "
+ phoneNumber.getText() + " " + password.getText() + " " + Integer.parseInt(credit.getText()) + " " + "");
+ phoneNumber.getText() + " " + password.getText() + " " + Integer.parseInt(credit.getText()) + " " + "IsBuyer");
dataOutputStream.flush();
status.setText(dataInputStream.readUTF());
}
Expand Down

0 comments on commit b01e39c

Please sign in to comment.