我是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;
public DiscountBill(Employee clerk, boolean preferred)
{
super(clerk);
String name = "";
double price = 0;
double discount = 0;
Object myItem = new Item(name, price, discount);
myPrice = ((GroceryBill.Item) myItem).getPrice() - ((GroceryBill.Item) myItem).getDiscount();
GroceryBill.Item myBill = new GroceryBill.Item(name, price, discount);
myDiscountAmount = myBill.getDiscount();
if (myDiscountAmount > 0 && preferred)
{
myDiscountCount++;
}
}
/*
public double getTotal()
{
Override goes here?
}
*/
public int getDiscountCount()
{
return myDiscountCount;
}
public double getDiscountAmount()
{
return myDiscountAmount;
}
public double getDiscountPercent()
{
return (myPrice / getDiscountCount()) * 100;
}
}
最后,here是预期的输出:
P.S。如果我需要提供更多/更少的信息和方法,我可以清理这篇文章或让它更容易理解,请告诉我。如果我的问题太宽泛,请问我对你不了解的内容,我会尽力告诉你!谢谢!