这是关于在toString
方法中定位字符串的问题:
即“获得利润”部分。我知道这可以通过单独的showProfit()
方法来实现,但问题不允许。必须使用toString()
方法显示所有内容。
public String toString(){
String str = String.format("%-10s", drinkID);
str += String.format("%15s%.2f", "Cost Price: $",drinkCostPrice);
str += String.format("%20s%.2f", "Selling Price: $", drinkSellPrice);
str += String.format("%18s%d", "Quantity Sold: ", drinkQuantitySold);
str += String.format("\nProfit Earned for %s:$%.2f", drinkID, computeProfit());
return str;
}
哪个输出:
latte Cost Price: $2.00 Selling Price: $3.50 Quantity Sold: 30
Profit Earned for latte:$45.00
mocca Cost Price: $2.00 Selling Price: $4.00 Quantity Sold: 30
Profit Earned for mocca:$60.00
espresso Cost Price: $1.00 Selling Price: $3.00 Quantity Sold: 20
Profit Earned for espresso:$40.00
但我要输出的是:
latte Cost Price: $2.00 Selling Price: $3.50 Quantity Sold: 30
mocca Cost Price: $2.00 Selling Price: $4.00 Quantity Sold: 30
espresso Cost Price: $1.00 Selling Price: $3.00 Quantity Sold: 20
Profit Earned for latte:$45.00
Profit Earned for mocca:$60.00
Profit Earned for espresso:$40.00
即。 “获利”部分分开。
如何在不使用其他功能的情况下完成这项工作?
答案 0 :(得分:0)
使用while循环浏览Drink[]
或<List>Drink
以获取:
latte Cost Price: $2.00 Selling Price: $3.50 Quantity Sold: 30
mocca Cost Price: $2.00 Selling Price: $4.00 Quantity Sold: 30
espresso Cost Price: $1.00 Selling Price: $3.00 Quantity Sold: 20
然后第二次循环再次运行它们以打印出所赚取的利润。你可以用一种方法完成所有这些。