Java - 从 A Child Class 访问和覆盖超级 Class

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

我是 Java 的新手,我正在从事一个计算价格 with/without 员工折扣的项目。阅读以下代码后,有人可以向我解释我需要如何更改代码才能获得正确的输出吗?这个问题我会在post的最后详细解释。如果我需要提供 more/less 信息和清理此 post 或使其更易于理解的方法,请告诉我。如果我的问题太宽泛,请问我你不明白的地方,我会尽力告诉你!谢谢!

Parent Class(不允许我编辑):

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) + ")";
        }
    }
}

这是我的代码:

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;
    }
}

最后,here 是我的具体问题后的预期输出:

如何让我的 getTotal() 方法正确输出总价,同时仍然能够访问原始价格 (1.35),以及如何将该答案应用到我的每个方法?

一些更重要的细节:

我得到的输出非常接近我需要的答案。从屏幕截图中可以看出,我通过了 getTotal() 方法的大部分测试,但随后它突然停止工作。此外,非常重要的一点是,由于我设置的 isFirstTime 布尔标志,getTotal 中通过测试的原始值(值 1.35)只能通过。如您所见,其他三种方法在第一次测试时都失败了,因为它们没有布尔标志来检查它是否是第一个值。我知道这个标志系统效率很低,但我找不到另一种方法来获得正确的值。如果你想看看这个网站是如何运作的,here 是我正在处理的问题的 link。

问题在于你第二次调用'getTotal()',你实际上并没有减去折扣金额。

顺便说一句,这个 isFirstTime 布尔值似乎是错误的:为什么您只在第一次致电时才提供折扣 'getTotal()'?

你不应该检查首选标志是否为真来实际提供折扣吗?这就是其余测试中出现的问题。

您应该保留首选值,在 class 中声明一个(非静态)变量并修改构造函数,如下所示:

private boolean preferred;

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

然后,在 getTotal 函数中我会做这样的事情:

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

有了这个它应该可以工作。我还注意到您在添加商品时忘记添加折扣:

myDiscountAmount = myBill.getDiscount();

而不是

myDiscountAmount += myBill.getDiscount();

其他方法也一样,例如:

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