Need help with Strategy pattern for discounts in POS system

Hi everyone, I'm working on an assignment where I have to implement two GoF patterns in a POS system. One of the patterns I chose is Strategy for calculating discounts based on criterias (customer ID, amount of products, etc.) The problem is that when I run Main.java, it doesn't show that the discount is applied on the products. I'm not sure what I'm doing wrong. Here is some context of how my code works: I have a Sale class that represents a sale in the POS system. It has a field called discounter that holds a reference to a Discounter interface. The Discounter interface declares a common method for all discount algorithms called applyDiscount, which takes a BigDecimal amount as a parameter and returns a BigDecimal amount after applying the discount. I have two concrete classes that implement the Discounter interface: QuantityDiscounter and CustomerDiscounter. They both have their own logic for fetching the discount rate from a database and applying it to the amount. They also handle any exceptions that may occur when accessing the database. In the Controller class, I have a method called createSaleAndApplyDiscount that creates a new sale and sets the discounter field to a new instance of QuantityDiscounter with a parameter of 3 (meaning 3 or more items bought will get a discount). Then it calls the applyDiscount method on the sale object and prints the discounted price. In the View class, I have a method called simulateSaleProcess that simulates a sale process with three products and calls the createSaleAndApplyDiscount method on the controller. The part of the assignment I'm stuck with is why the discount is not applied when I run Main.java. It should print something like "The discounted price is: ..." but instead it prints "The discounted price is: 0.000". Can anyone help me figure out what's wrong with my code? Any hints or suggestions would be greatly appreciated. Thanks in advance!
No description
13 Replies
JavaBot
JavaBot2y ago
This post has been reserved for your question.
Hey @dghf! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
dghf
dghfOP2y ago
@That_Guy977 https://sourceb.in/KzRuzbmzO5 This is my entire code The code that calculates the discount is in the Sale class, in the applyDiscount method. Here is the code:
// A method that uses the strategy object to apply the discount and handles any exceptions that may occur
public BigDecimal applyDiscount(BigDecimal amount) {
try {
// Use the discounter object to apply the discount
// Convert the double value to a BigDecimal value
// For example, you can use the BigDecimal.valueOf method
BigDecimal payAmount = BigDecimal.valueOf(payment.getPayAmount());
return discounter.applyDiscount(payAmount);
} catch (DatabaseFailureException e) {
// Handle the exception here
// For example, you can print or log an error message for developers and users
System.out.println("Developer log: " + e.getMessage());
System.out.println("User message: There was an error when fetching discounts from database.");
// You can also return a default value or do something else
// For example, you can return the original amount without any discount
return amount;
}
}
// A method that uses the strategy object to apply the discount and handles any exceptions that may occur
public BigDecimal applyDiscount(BigDecimal amount) {
try {
// Use the discounter object to apply the discount
// Convert the double value to a BigDecimal value
// For example, you can use the BigDecimal.valueOf method
BigDecimal payAmount = BigDecimal.valueOf(payment.getPayAmount());
return discounter.applyDiscount(payAmount);
} catch (DatabaseFailureException e) {
// Handle the exception here
// For example, you can print or log an error message for developers and users
System.out.println("Developer log: " + e.getMessage());
System.out.println("User message: There was an error when fetching discounts from database.");
// You can also return a default value or do something else
// For example, you can return the original amount without any discount
return amount;
}
}
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
dghf
dghfOP2y ago
You think the issue is that createSaleAndApplyDiscount creates an empty sale and doesn't add any products to it?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Kyo-chan
Kyo-chan2y ago
Generally speaking, you can't claim that you've tried to solve a problem, if you haven't first moved that problem to a 20 lines program
dghf
dghfOP2y ago
So I'd have to move the createSaleAndApplyDiscount method from the Controller class to the View class, and call it after adding the products to the sale? So
public void simulateSaleProcess() {

cont.initializeNewSale();
getProductById(firstProductId);
getProductById(secondProductId);
getProductById(firstProductId);
cont.finalizeSaleAndUpdateSystems();
sendProductInfoToDisplay();
// Call the createSaleAndApplyDiscount method here
createSaleAndApplyDiscount();
cont.processPaymentAndReturnChange(Payment.FIRST_PAYMENT);

cont.initializeNewSale();
getProductById(firstProductId);
getProductById(firstProductId);
getProductById(121);
cont.finalizeSaleAndUpdateSystems();
sendProductInfoToDisplay();
// Call the createSaleAndApplyDiscount method here
createSaleAndApplyDiscount();
cont.processPaymentAndReturnChange(Payment.SECOND_PAYMENT);
}

// Move the createSaleAndApplyDiscount method from the Controller class to the View class
private void createSaleAndApplyDiscount() {
// Set the discounter field to a new instance of QuantityDiscounter with a parameter of 3
Discounter discounter = new QuantityDiscounter(3);
cont.setDiscounter(discounter);
// Call the applyDiscount method on the sale object and print the discounted price
BigDecimal amount = cont.getSaleDTO().getPaymentDTO().getTotalAmount();
BigDecimal discountedPrice = cont.applyDiscount(amount);
System.out.println("The discounted price is: " + discountedPrice);
}
public void simulateSaleProcess() {

cont.initializeNewSale();
getProductById(firstProductId);
getProductById(secondProductId);
getProductById(firstProductId);
cont.finalizeSaleAndUpdateSystems();
sendProductInfoToDisplay();
// Call the createSaleAndApplyDiscount method here
createSaleAndApplyDiscount();
cont.processPaymentAndReturnChange(Payment.FIRST_PAYMENT);

cont.initializeNewSale();
getProductById(firstProductId);
getProductById(firstProductId);
getProductById(121);
cont.finalizeSaleAndUpdateSystems();
sendProductInfoToDisplay();
// Call the createSaleAndApplyDiscount method here
createSaleAndApplyDiscount();
cont.processPaymentAndReturnChange(Payment.SECOND_PAYMENT);
}

// Move the createSaleAndApplyDiscount method from the Controller class to the View class
private void createSaleAndApplyDiscount() {
// Set the discounter field to a new instance of QuantityDiscounter with a parameter of 3
Discounter discounter = new QuantityDiscounter(3);
cont.setDiscounter(discounter);
// Call the applyDiscount method on the sale object and print the discounted price
BigDecimal amount = cont.getSaleDTO().getPaymentDTO().getTotalAmount();
BigDecimal discountedPrice = cont.applyDiscount(amount);
System.out.println("The discounted price is: " + discountedPrice);
}
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
dghf
dghfOP2y ago
But I am not sure if that'd break encapsulation or violate OOP principles and bring bad code smells
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JavaBot
JavaBot2y ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one.

Did you find this page helpful?