Java-从子类访问和重写超类

Java-从子类访问和重写超类,java,overriding,parent-child,superclass,Java,Overriding,Parent Child,Superclass,我是Java新手,我正在从事一个项目,该项目可以计算有/没有员工折扣的价格。在阅读了下面的代码之后,有人能给我解释一下为了获得正确的输出,我需要如何更改代码吗?我将在文章的最后更详细地解释这个问题。请让我知道,如果我需要提供更多/更少的信息和方法,我可以清理这篇文章或使它更容易理解。如果我的问题太宽泛,请问我你不明白的地方,我会尽力告诉你!谢谢大家! 父类(不允许我编辑此内容): public class GroceryBill { private Employee clerk;

我是Java新手,我正在从事一个项目,该项目可以计算有/没有员工折扣的价格。在阅读了下面的代码之后,有人能给我解释一下为了获得正确的输出,我需要如何更改代码吗?我将在文章的最后更详细地解释这个问题。请让我知道,如果我需要提供更多/更少的信息和方法,我可以清理这篇文章或使它更容易理解。如果我的问题太宽泛,请问我你不明白的地方,我会尽力告诉你!谢谢大家!

父类(不允许我编辑此内容):

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;
    }
}
最后,是预期输出,然后是我的具体问题:

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;
    }
}
如何让getTotal()方法正确输出总价,同时仍然能够访问原始价格(1.35),以及如何将该答案应用于每个方法

一些更重要的细节:

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

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

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

顺便说一下,这是FirstTime boolean似乎是错误的:为什么只在第一次调用'getTotal()'时才给折扣


你不应该检查首选标志是否为真,以实际给予折扣吗?这就是在其余测试中出错的地方

您应该保留preferred的值,在类中声明(非静态)变量并修改构造函数,如下所示:

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

对于firsttime变量,static表示相同的值适用于所有折扣票据对象。这似乎不正确。@NathanHughes说得好,我删除了isFirstTime标志,现在使用传递给子类构造函数的首选标志,但我仍然得到完全相同的输出。折扣金额正在被减去。它做了多次,这是它通过大多数测试的唯一原因。此外,即使我删除了标志并使用首选布尔检查,我也会得到相同的输出。非常感谢!我没有看到我没有增加我的折扣!现在我通过了所有的getTotal()要求:,但我仍然需要其他要求。首选布尔值也适用于这些吗?应该,是的。在所有这些方法中返回任何内容之前,您可以使用“if(preferred)”遵循相同的结构。我更新了我的代码,似乎无法让其他方法工作,因为使用preferred实际上没有任何作用。在更改之前,是否有其他方法可以获取原始金额?请检查我的修改,这些修改是在假设您使用新代码和屏幕截图再次发布的第一个代码的情况下完成的。如果你有时间,请告诉我你的想法。