Skip to content

Commit

Permalink
Edit Discount (Finished Without Testing)
Browse files Browse the repository at this point in the history
  • Loading branch information
sepehrkianian09 committed Jul 3, 2020
1 parent 0334537 commit 1e6bd59
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 23 deletions.
59 changes: 52 additions & 7 deletions src/main/java/controller/account/AdminControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/model/existence/Discount.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/notification/Notification.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
34 changes: 19 additions & 15 deletions src/main/java/view/SaleProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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<ButtonType> 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<ButtonType> 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);
}
}

Expand Down

0 comments on commit 1e6bd59

Please sign in to comment.