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!