简体   繁体   English

Java-覆盖父类

[英]Java - Overriding A Parent Class

I am new to Java and I am working on a project that works with calculating prices with/without employee discounts. 我是Java的新手,我正在从事一个可以计算有/没有员工折扣的价格的项目。 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. 我将在帖子末尾详细解释这个问题。

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;
    
    public DiscountBill(Employee clerk, boolean preferred)
    {
        super(clerk);
        
        String name = "";
        double price = 0;
        double discount = 0;
        
        GroceryBill.Item myBill = new GroceryBill.Item(name, price, discount);
        myDiscountAmount = myBill.getDiscount();
        
        if (myDiscountAmount > 0 && preferred)
        {
            myDiscountCount++;
        }
    }

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

        if (myDiscountAmount > 0 )
        {
            myDiscountCount++;
        }
    }
    public double getTotal()
    {
        if (myDiscountCount > 0)
        {
            return myPrice - myDiscountAmount;
        }
        return myPrice;
    }
    public int getDiscountCount()
    {
        return myDiscountCount;
    }
    public double getDiscountAmount()
    {
        return myDiscountAmount;
    }
    public double getDiscountPercent()
    {
        return ((myPrice - myDiscountAmount) / myPrice * 100);
    }
}

Lastly, here is the expected output followed by my specific question: 最后, 是预期的输出,后面是我的具体问题:

The outputs that I am getting for my methods are one step ahead of where they should be. 我为我的方法获得的输出比应该达到的输出领先了一步。 That is to say, my getTotal, for example, should start out as 1.35 (the first value input by the website I am using that tests my child class that I wrote) and then after another step it should be reduced to 1.1 (the website uses the employee discount using the boolean preferred from the constructor), but my program outputs 1.1 because my child class overrides the parent class's getTotal() method and never starts at the total it should (1.35). 也就是说,例如,我的getTotal应该以1.35(我正在使用的网站输入的第一个值测试我编写的子类)开头,然后再将其减小到1.1(该网站使用员工折扣(使用构造函数中的首选布尔值),但是我的程序输出1.1,因为我的子类覆盖父类的getTotal()方法,并且从不以应有的总和开始(1.35)。 Basically I need to know how to get those original values from my parent class and THEN use the override methods to get the values after they are changed. 基本上,我需要知道如何从父类中获取这些原始值,然后在更改后使用重写方法来获取这些值。 If you want to see how this website operates, here is a link to the question I'm working on. 如果您想了解本网站的运作方式,请点击以下链接访问我正在处理的问题。

PS 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! 谢谢!

As far as I understood your question, what you want to do is : 据我了解您的问题,您想做的是:

Add this additional code to your DiscountBill 将此附加代码添加到DiscountBill

public class DiscountBill extends GroceryBill
{
    private static boolean isFirstTime = true;

    public double getTotal()
    {
        if (!isFirstTime && myDiscountCount > 0)
        {
           return myPrice - myDiscountAmount;
        }
        isFirstTime = false;
        return myPrice;
    }

}

I'm not sure if that is what you want. 我不确定这是否是您想要的。 Do you want to call parent method? 您要调用父方法吗? For that you can use super.parentMethodName Please correct me if I'm wrong 为此,您可以使用super.parentMethodName如果我输入错误,请更正我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM