我是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;
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);
}
}
最后,http://www.w3.org/2001/XMLSchema#date是预期的输出,然后是我的具体问题:
我为我的方法获得的输出比它们应该的位置提前一步。也就是说,例如,我的getTotal应该从1.35开始(我使用的网站输入的第一个值测试我编写的我的子类)然后在另一个步骤后它应该减少到1.1(网站使用构造函数中的boolean preferred使用员工折扣,但我的程序输出1.1,因为我的子类重写了父类的getTotal()方法,并且从不以它应该的总数(1.35)开始。基本上我需要知道如何从我的父类中获取这些原始值,然后使用override方法在更改后获取值。如果您想查看此网站的运作方式,here是我正在处理的问题的链接。
P.S。如果我需要提供更多/更少的信息和方法,我可以清理这篇文章或让它更容易理解,请告诉我。如果我的问题太广泛,请问我对你不了解的内容,我会尽力告诉你!谢谢!
答案 0 :(得分:0)
据我了解你的问题,你想做的是:
将此附加代码添加到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;
}
}
答案 1 :(得分:0)
我不确定这是不是你想要的。你想调用父方法吗?
为此你可以使用super.parentMethodName
如果我错了,请纠正我