0

所有空白的方法,我的硬件告诉我要按照评论填写。只是无法将数组项和描述放在一起。

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();

}

}
4

2 回答 2

0

在您的addItem()方法中,您可以这样做:

Item item = new Item(description, price, isTaxable);          
this.item.add(item);

解释:

您已经有Item 数组列表private ArrayList<Item> items = new ArrayList<Item>();

这意味着您可以 通过调用将所有Item对象存储在其中this.item.add(item);

那么,基本上上面的代码是做什么的?

1)创建新的Item对象

2)使用值(描述、价格、isTaxable)填充项目对象

3)然后将Item对象添加到Item 数组列表中

于 2013-05-01T01:29:53.997 回答
0

老师为你提供了以下成员变量:

private ArrayList<Item> items = new ArrayList<Item>();

这是一个ArrayList包含Item对象。您提供的类“Item.java”包含Item该类的定义。

如果您在此处查阅 Java API 文档,您将看到为类对象提供的方法之一ArrayListadd方法:

public boolean add(E e)

其中“将指定元素附加到此列表的末尾。” 使用该方法,您可以将类的项目添加Item到列表中。

因此,在您的addItems方法中,您可以创建一个新Item对象并将其添加到您的项目列表中,如下所示:

Item item = new Item(description, price, isTaxable);
items.add(item);

在这里,我们使用提供的描述、价格和 isTaxable 变量创建了一个 Item 类的新对象。然后,我们将刚刚创建的项目添加到项目列表中。

于 2013-05-01T01:30:03.397 回答