这是我的班级任务。我一直在输出中得到错误的杂货。在运行代码时,我该如何获得正确的项目?
以下是我目前获得的输出:
购物者姓名:Gary
冰淇淋:5个单位,每个单位4.25美元= 21.25美元
冰淇淋:5个单位,每个单位4.25美元= 21.25美元
---->小计= $ 42.5
----> 7%税= 2.975美元
---->总计= $ 45.475
购物者名称:莎莉
冰淇淋:2个单位,每个单位4.25美元= 8.5美元
冰淇淋:2个单位,每个单位4.25美元= 8.5美元
---->小计= $ 17.0
----> 7%税= 1.1900000000000002
---->总计= $ 18.19
购物车类:
public class Cart
{
public String Name;
public int ItemNum;
public double tax;
public Cart(String ShopperName){
Name = ShopperName;
}
public String getShopperName(){
return Name;
}
public int getItemNumber(){
return ItemNum;
}
public void addItem1(GroceryItem GroceryItem, int NumberItem){
GroceryItem = GroceryItem;
ItemNum = NumberItem;
}
public void addItem2(GroceryItem GroceryItem, int NumberItem){
GroceryItem = GroceryItem;
ItemNum = NumberItem;
}
public double getItemTotal(){
double item_total = (double) (GroceryItem.getCost()*getItemNumber());
return item_total;
}
public double getSubtotal(){
double subtotal = (double) (getItemTotal() + getItemTotal());
return subtotal;
}
public double getTaxTotal(){
double tax = .07;
double taxtotal = (double) (getSubtotal()*tax);
return taxtotal;
}
public double getTotal(){
double Total = (double) (getTaxTotal()+getSubtotal());
return Total;
}
public void printReceipt(){
System.out.println("Shopper Name: " + getShopperName());
System.out.println("----------------------");
System.out.println(GroceryItem.getName()+":"+ getItemNumber()+ " units at $" + GroceryItem.getCost()+" per unit"+ " = $"+ getItemTotal());
System.out.println(GroceryItem.getName()+":"+ getItemNumber()+ " units at $" + GroceryItem.getCost()+" per unit"+ " = $"+ getItemTotal());
System.out.println("----> Subtotal = $" + getSubtotal());
System.out.println("----> 7% tax = $" + getTaxTotal() );
System.out.println("---->Total = $" + getTotal());
System.out.println("");
System.out.println("");
System.out.println("");
}
}
驱动程序类:
/**
* Driver for Cart and GroceryItem.
*/
public class Driver
{
public static void main(String[] args)
{
// create grocery items
GroceryItem item1 = new GroceryItem("milk", 3.39);
GroceryItem item2 = new GroceryItem("eggs", 1.75);
GroceryItem item3 = new GroceryItem("ice cream", 4.25);
// create new carts
Cart shopper1 = new Cart("Gary");
Cart shopper2 = new Cart("Sally");
// add items to first cart
shopper1.addItem1(item2, 1); //1 "eggs" is being added
shopper1.addItem2(item1, 5); //5 "milk" are being added
// add items to second cart
shopper2.addItem1(item3, 2); //2 "ice cream" are being added
shopper2.addItem2(item2, 2); //2 "eggs" are being added
// print cart's receipt
shopper1.printReceipt();
shopper2.printReceipt();
}
}
答案 0 :(得分:0)
这看起来不对:
public void addItem1(GroceryItem GroceryItem, int NumberItem){
GroceryItem = GroceryItem;
ItemNum = NumberItem;
}
你正在为自己分配GroceryItem。
addItem2方法相同。
实际上,您的购物车似乎没有办法存放这些物品。看起来你并没有抓住这里的必需品。回到书本我害怕......
答案 1 :(得分:0)
您已在addItem
方法中将项目分配给自己。声明实例变量并指定您使用this
重新分配实例变量。
例如:
public class Cart {
private GroceryItem groceryItem;
...
public void addItem1(GroceryItem groceryItem, int NumberItem){
this.groceryItem = groceryItem;
...
然后在打印结果时,使用实例变量groceryItem
。
此外,在java
中,成员(变量/方法名称)应位于lower camel case。