Search code examples
javaoverridingparent-childsuperclass

Java - Accessing And Overriding A Super Class From A Child Class


I am new to Java and I am working on a project that works with calculating prices with/without employee discounts. After reading the following code could someone explain to me how I would need to change my code in order to get the correct output? I will explain this question in more detail at the end of the post. Please let me know if I need to give more/less information and ways that I can clean up this post or make it easier to understand. If my question was too broad, please ask me what you don't understand about it and I'll try my best to tell you! Thank you!

Parent Class (I am NOT allowed to edit this):

public class GroceryBill {
    private Employee clerk;
    private List<Item> receipt;
    private double total;
    private double internalDiscount;

    public GroceryBill(Employee clerk) {
        this.clerk = clerk;
        receipt = new ArrayList<Item>();
        total = 0.0;
        internalDiscount = 0.0;
    }

    public void add(Item i) {
        receipt.add(i);
        total += i.getPrice();
        internalDiscount += i.getDiscount();
    }

    public double getTotal() {
        return Math.rint(total * 100) / 100.0;
    }

    public Employee getClerk() {
        return clerk;
    }

    public void printReceipt() {
        System.out.println(this);
    }

    private String valueToString(double value) {
        value = Math.rint(value * 100) / 100.0;
        String result = "" + Math.abs(value);
        if(result.indexOf(".") == result.length() - 2) {
            result += "0";
        }
        result = "$" + result;
        return result;
    }

    public String receiptToString() {
        String build = "items:\n";
        for(int i = 0; i < receipt.size(); i++) {
            build += "   " + receipt.get(i);
            if(i != receipt.size() - 1) {
                build += "\n";
            }
        }
        return build;
    }

    public String toString() {
        return receiptToString() + "\ntotal: " + valueToString(total);
    }

    public String discountToString() {
        return receiptToString() + "\nsub-total: " + valueToString(total) + "\ndiscount: " + valueToString(internalDiscount) + "\ntotal: " + valueToString(total - internalDiscount);
    }

    public static class Employee {
        private String name;

        public Employee(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    }

    public static class Item {
        private String name;
        private double price;
        private double discount;

        public Item(String name, double price, double discount) {
            this.name = name;
            this.price = price;
            this.discount = discount;
        }

        public double getPrice() {
            return price;
        }

        public double getDiscount() {
            return discount;
        }

        private String valueToString(double value) {
            String result = "" + Math.abs(value);
            if(result.indexOf(".") == result.length() - 2) {
                result += "0";
            }
            result = "$" + result;
            return result;
        }

        public String toString() {
            return name + " " + valueToString(price) + " (-" + valueToString(discount) + ")";
        }
    }
}

Here is my code:

public class DiscountBill extends GroceryBill
{
    private int myDiscountCount;
    private double myDiscountAmount;
    private double myPrice;
    private boolean myStatus;
    private double myDiscountPercent;

    public DiscountBill(Employee clerk, boolean preferred)
    {
        super(clerk);
        myStatus = preferred;
    }

    public void add(Item myBill)
    {
        myPrice += myBill.getPrice();
        if (myStatus)
        {
            myDiscountAmount += myBill.getDiscount();
        }

        if (myDiscountAmount > 0 && myStatus)
        {
            myDiscountCount += 1;
        }
    }

    @Override
    public double getTotal()
    {
        if (myStatus)
        {
           myPrice = (double)Math.round(myPrice * 100) / 100;
           return myPrice - myDiscountAmount;
        }
        return (double)Math.round(myPrice * 100) / 100;
    }
    public int getDiscountCount()
    {
        return myDiscountCount;
    }
    public double getDiscountAmount()
    {
        return myDiscountAmount;
    }
    public double getDiscountPercent()
    {
        myDiscountPercent = (myDiscountAmount);
        return myDiscountPercent;
    }
}

Lastly, here is the expected output followed by my specific question:

How can I get my getTotal() method to correctly output the total price, while still being able to access the original price (1.35), and how can I apply that answer to each of my methods?

Some more important specifics:

The outputs that I am getting are very close to the answers I need. As you can see from the screenshot, I passed most of the tests for the getTotal() method, but then it suddenly stopped working. Also, it is very important to note that the original value that passed the test in getTotal (the value 1.35) was only able to pass through due to the isFirstTime boolean flag that I set up. As you can see the other three methods all fail on their first test because they don't have a boolean flag that checks if it's the first value. I know that this flag system is very inefficient, but I cannot find another way to get correct values. If you want to see how this website operates, here is a link to the question I'm working on.


Solution

  • You should keep the value of preferred, declaring a (non-static) variable in the class and modifying the constructor, something like this:

    private boolean preferred;
    
    public DiscountBill(Employee clerk, boolean preferred)
    {
        super(clerk);
        this.preferred = preferred;
    }
    

    Then, in the getTotal function I would do something like this instead:

    @Override
    public double getTotal()
    {
        if(preferred){
            return myPrice - myDiscountAmount;
        }
        else return myPrice;
    }
    

    With this it should work. Also I noticed that you forgot to add the discount when you add the item:

    myDiscountAmount = myBill.getDiscount();
    

    instead of

    myDiscountAmount += myBill.getDiscount();
    

    Same thing for the other methods, for instance:

    public int getDiscountCount() {
            if (preferred) {
                return myDiscountCount;
            } else
                return 0;
        }