From 1e6bd596e4e89775f5b1821d293958a475baf854 Mon Sep 17 00:00:00 2001 From: sepehrkianian09 <59197271+sepehrkianian09@users.noreply.github.com> Date: Fri, 3 Jul 2020 11:22:30 +0430 Subject: [PATCH] Edit Discount (Finished Without Testing) --- .../java/controller/account/AdminControl.java | 59 ++++++++++++++++--- src/main/java/model/existence/Discount.java | 4 ++ src/main/java/notification/Notification.java | 4 +- src/main/java/view/SaleProcessor.java | 34 ++++++----- 4 files changed, 78 insertions(+), 23 deletions(-) diff --git a/src/main/java/controller/account/AdminControl.java b/src/main/java/controller/account/AdminControl.java index 8bf21910..174af9b6 100644 --- a/src/main/java/controller/account/AdminControl.java +++ b/src/main/java/controller/account/AdminControl.java @@ -260,12 +260,25 @@ public Notification removeDiscountByID(String ID) } public Notification addDiscount(Discount discount) { - if(isDiscountComplete(discount) != null) - return isDiscountComplete(discount); - - String ID = ""; + Notification notification = null; try { + if(discount.getID() != null && !discount.getID().isEmpty()) { + if((notification = setNewDiscountsEmptyFields(discount)) == null) { + DiscountTable.removeDiscountCode(discount.getID()); + notification = Notification.EDIT_DISCOUNT; + } else + return notification; + } else { + if((notification = isDiscountComplete(discount)) == null) { + notification = Notification.ADD_DISCOUNT; + } else { + return notification; + } + } + + String ID = ""; + do { ID = generateDiscountID(); } while (DiscountTable.isThereDiscountWithID(ID)); @@ -277,7 +290,31 @@ public Notification addDiscount(Discount discount) { } catch (ClassNotFoundException e) { //:) } - return Notification.ADD_DISCOUNT; + return notification; + } + + public Notification setNewDiscountsEmptyFields(Discount newDiscount) throws SQLException, ClassNotFoundException { + Discount oldDiscount = DiscountTable.getDiscountByID(newDiscount.getID()); + + if(newDiscount.getCode() == null || newDiscount.getCode().isEmpty()) + newDiscount.setCode(oldDiscount.getCode()); + if(newDiscount.getDiscountPercent() == 0) + newDiscount.setDiscountPercent(oldDiscount.getDiscountPercent()); + if(newDiscount.getMaxRepetition() == 0) + newDiscount.setMaxRepetition(oldDiscount.getMaxRepetition()); + if(newDiscount.getMaxDiscount() == 0) + newDiscount.setMaxDiscount(oldDiscount.getMaxDiscount()); + if(newDiscount.getStartDate() == null) + System.out.println("Shit. Empty StartDate In Setting New Discount's Empty Fields"); + if(newDiscount.getFinishDate() == null) + newDiscount.setFinishDate(oldDiscount.getFinishDate()); + + if(newDiscount.getFinishDate().getTime() <= newDiscount.getStartDate().getTime()) + return Notification.INVALID_FINISH_DATE_EARLIER_THAN_START_DATE; + else if(newDiscount.getFinishDate().getTime() <= System.currentTimeMillis()) + return Notification.INVALID_FINISH_DATE_EARLIER_THAN_CURRENT_DATE; + + return null; } public Notification isDiscountComplete(Discount discount) { @@ -445,8 +482,16 @@ public void getGiftDiscount() { public Notification addAddedDiscount(Discount discount) { discount.setCustomersWithRepetition(new HashMap<>()); - for (String addedUser : discountsAddedUsers.get(discount)) { - discount.addCustomerWithRepetition(addedUser, 0); + if(discount.getID() == null || discount.getID().isEmpty()) { + for (String addedUser : discountsAddedUsers.get(discount)) { + discount.addCustomerWithRepetition(addedUser, 0); + } + } else { + for (String addedUser : discountsAddedUsers.get(discount)) { + if(!discount.isCustomerInDiscount(addedUser)) { + discount.addCustomerWithRepetition(addedUser, 0); + } + } } return addDiscount(discount); diff --git a/src/main/java/model/existence/Discount.java b/src/main/java/model/existence/Discount.java index 60d98dba..32bcceef 100644 --- a/src/main/java/model/existence/Discount.java +++ b/src/main/java/model/existence/Discount.java @@ -144,6 +144,10 @@ public int getCustomerRepetition(String username) { return customerRepetition; } + public boolean isCustomerInDiscount(String username) { + return customersWithRepetition.containsKey(username); + } + public boolean canCustomerUseThisDiscount(String username) { return customersWithRepetition.containsKey(username) && customersWithRepetition.get(username) < maxRepetition; } diff --git a/src/main/java/notification/Notification.java b/src/main/java/notification/Notification.java index 4fd14252..a0225237 100644 --- a/src/main/java/notification/Notification.java +++ b/src/main/java/notification/Notification.java @@ -208,7 +208,9 @@ public enum Notification { EDIT_PRODUCT("You Edited Your Product Successfully. Wait For The Lord's Approval", Alert.AlertType.INFORMATION, "Editing Product Successful", "Product Request"), DECLINE_EDITING_PRODUCT("Editing Off Requests Denied Successfully Great King.", - Alert.AlertType.INFORMATION, "Editing Product Accepted", "Product Request"); + Alert.AlertType.INFORMATION, "Editing Product Accepted", "Product Request"), + EDIT_DISCOUNT("Discount Editted Successfully. Are You Done?", Alert.AlertType.INFORMATION, + "Congratulations", "Edited Successfully"); private String message; diff --git a/src/main/java/view/SaleProcessor.java b/src/main/java/view/SaleProcessor.java index 5eae4138..fe722bb1 100644 --- a/src/main/java/view/SaleProcessor.java +++ b/src/main/java/view/SaleProcessor.java @@ -128,8 +128,11 @@ private void setDiscountFields() { if(mainDiscount.getMaxDiscount() != 0) maxDiscountTextField.setText(Double.toString(mainDiscount.getMaxDiscount())); - if(mainDiscount.getStartDate() != null) + if(mainDiscount.getStartDate() != null) { setDateFieldsFromDate(startDatePicker, startTimePicker, mainDiscount.getStartDate()); + startDatePicker.setDisable(true); + startTimePicker.setDisable(true); + } if(mainDiscount.getFinishDate() != null) setDateFieldsFromDate(finishDatePicker, finishTimePicker, mainDiscount.getFinishDate()); @@ -301,21 +304,22 @@ public void discountInfoMouseClicked(MouseEvent mouseEvent) { } public void AddDiscountMouseClicked(MouseEvent mouseEvent) { - if(discount.getID() == null || discount.getID().isEmpty()) { - //Todo Setting Notifications - Notification notification = adminControl.addAddedDiscount(discount); - - Optional optionalButtonType = notification.getAlert().showAndWait(); - - if(optionalButtonType.get() == ButtonType.OK) { - if(notification.equals(Notification.ADD_DISCOUNT)) { - updateParentTable(); - this.myStage.close(); - } else - discountInfoMouseClicked(null); - } - } else { + //Todo Setting Notifications + Notification notification = adminControl.addAddedDiscount(discount); + + Optional optionalButtonType = notification.getAlert().showAndWait(); + + if(optionalButtonType.get() == ButtonType.OK) { + if(notification == Notification.ADD_DISCOUNT || notification == Notification.EDIT_DISCOUNT) { + //Todo Check + for (Discount discount : adminControl.getAllDiscounts()) { + System.out.println(discount.getCode()); + } + updateParentTable(); + this.myStage.close(); + } else + discountInfoMouseClicked(null); } }