所有空白的方法,我的硬件告诉我要按照评论填写。只是无法将数组项和描述放在一起。
public class CashRegisterItemList {
private ArrayList<Item> items = new ArrayList<Item>();
private double taxRate;
/**
* Constructs a cash register with with no items.
* Sets the tax rate to the default of 7.25%. *
*/
public CashRegisterItemList() {
items=0;
double taxRate= 7.25;
}
/**
* Constructs a cash register with no items.
* Sets the tax rate to the given tax rate.
* @param taxRate tax rate for taxable items
*/
public CashRegisterItemList(double taxRate) {
items= 0;
double taxRate= taxRate;
}
/**
* Adds an item to this cash register.
* @param description the description of this item
* @param price the price of this item
* @param isTaxable the taxability of this item
*/
public void addItem(String description, double price, boolean isTaxable) {
itemCount++;
totalPrice = totalPrice + price;
if(description isTaxable)
totalTax = price*taxRate;
}
/**
* Gets the total price of all items in the current sale,
* not including the tax.
* @return the total amount
*/
public double getTotal() {
return totalPrice;
}
/**
* Gets the total tax of all the taxable items in the current sale,
* @return the total tax
*/
public double getTotalTax() {
return totalTax;
}
/**
* Gets the number of items in the current sale.
* @return the item count
*/
public int getCount() {
return items;
}
/**
* Removes all items from current sale
*/
public void clear() {
int items= 0;
}
/**
* Prints a receipt, in the form shown on the assignment page.
*/
public void printReceipt() {
for(int i=0; i< items.length; i++){
if
}
}
会创建一个单独的变量来单独跟踪项目吗?我该怎么做才能完成这项工作。此类应该创建收银机的输出,例如
Cash Register 1 has 6 items Cash Register 1 total = 30.10
Cash Register 1 Receipt
6 items
Bread 0.90
Paper 1.95 T
Bananas 0.90
Milk 1.95
Turkey 21.50
Bowl +
2.90 T
--------
30.10
0.35T -------- 30.45
Cash Register 1 Receipt
6 items
Bread 0.90
Paper 1.95 T
Bananas 0.90
Milk 1.95
Turkey 21.50
Bowl +
After clearing
Cash Register 1
0 items
--------
0.00
0.00T -------- 0.00
这是 item.java
public class Item {
private String description;
private double price;
private boolean isTaxable;
/**
* Constructs an Item with a given description, price and taxability.
* @param description the description of item
* @param price the price of item
* @param isTaxable whether the item is taxable or not
*/
public Item(String description, double price, boolean isTaxable) {
this.description = description;
this.price = price;
this.isTaxable = isTaxable;
}
/**
* Returns the description of the item.
* @return the description of the item
*/
public String getDescription() {
return description;
}
/**
* Returns the price of the item.
* @return the price of the item
*/
public double getPrice() {
return price;
}
/**
* Returns true if item is taxable, false otherwise
* @return true if item is taxable, false otherwise
*/
public boolean isTaxable() {
return isTaxable;
}
}
和项目测试员
public class CashRegisterItemListTester {
public static void main(String[] args) {
CashRegisterItemList register1 = new CashRegisterItemList();
register1.addItem("Bread", 0.90, false);
register1.addItem("Paper", 1.95, true);
register1.addItem("Bananas", 0.90, false);
register1.addItem("Milk", 1.95, false);
register1.addItem("Turkey", 21.50, false);
register1.addItem("Bowl", 2.90, true);
System.out.printf("Cash Register 1 has %d items\n", register1.getCount());
System.out.printf("Cash Register 1 total = %.2f\n", register1.getTotal());
System.out.println();
System.out.println("Cash Register 1 Receipt");
register1.printReceipt();
register1 = new CashRegisterItemList(0.085);
register1.addItem("Bread", 0.90, false);
register1.addItem("Paper", 1.95, true);
register1.addItem("Bananas", 0.90, false);
register1.addItem("Milk", 1.95, false);
register1.addItem("Turkey", 21.50, false);
register1.addItem("Bowl", 2.90, true);
System.out.println();
System.out.println("Cash Register 1 Receipt");
register1.printReceipt();
register1.clear();
System.out.println("\nAfter clearing");
System.out.println("Cash Register 1");
register1.printReceipt();
}
}