0

all of the methods where blank, my hw told to follow the comments and fill them in. Just having trouble putting the array items and description together.

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  

}

}

Would create a separate variable to solely keep track of items? What do i do to make this work. this class is supposed to create the output of a cash register like

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

here is the 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;
    }

}

and the item tester

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

}

}
44hz
  • 23
  • 1
  • 8
  • I havent completely finished it, so it wont print anything. – 44hz May 01 '13 at 01:18
  • The [`ArrayList`](http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html) holds type `Item`. I'm pretty sure that isn't a built-in class (although my Java knowledge is rusty). Did the teacher give you an `Item` class? It seems to me that that class is the key to your problem. – acattle May 01 '13 at 01:24
  • @acattle yes he gave us a itemtesterlist and the items i will add them right now. – 44hz May 01 '13 at 01:27
  • Reading your code I see you have a few other mistakes (that aren't part of your original question). I suggest you look at the [`ArrayList` API](http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html). For starters, the `size()` function might be useful to you. Also, read up on [Java for loops](http://stackoverflow.com/questions/7763131/java-for-loop-syntax) for doing things like calculating the total prices. Best of luck. – acattle May 01 '13 at 01:43
  • Yes, acattle is right. One of your mistakes is that you're assigning `0` to your `items` object, when it is not an integer, it is an object of class `ArrayList`. – sversch May 01 '13 at 01:53
  • @zaidaus I was gonna ask that next because now knowing what I know it doesn't make sense to initialize to 0 – 44hz May 01 '13 at 01:58

2 Answers2

0

in your addItem() method, you can do this:

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

Explanation:

You already have Item array list which is private ArrayList<Item> items = new ArrayList<Item>();

Which means you can store all your Item objects in them by calling this.item.add(item);

So, basically what above code does ?

1) Creates new Item object

2) Populates Item object with values (description, price, isTaxable)

3) Then adds Item object into Item array list.

Ravi Trivedi
  • 2,340
  • 2
  • 14
  • 20
  • @acattle, Hmm, I appreciate what you are saying. Let me explain the code. – Ravi Trivedi May 01 '13 at 01:33
  • @acattle does the item.java get used in the array created in the first segment of code. trying to put the pieces together – 44hz May 01 '13 at 01:39
  • @JDMshifter, it will be used once you create Item object and populate Item object with values as I have suggested in the answer. – Ravi Trivedi May 01 '13 at 01:41
  • @JDMshifter, see my answer, short answer: yes the Item.java is the Item class you see in the first segment of code – sversch May 01 '13 at 01:45
  • @Ravi Trivedi I think why I was so confused was because I didn't know you could call a class in array – 44hz May 01 '13 at 01:45
  • @JDMshifter, That is not calling class in an array. It is called parameterized array. What that means is that your array is of type `Item`. – Ravi Trivedi May 01 '13 at 01:49
  • @RaviTrivedi so it would be kinda like a specified type that he created. – 44hz May 01 '13 at 01:51
  • @JDMshifter, Yes, you can specify the *Type* when you create an array. It is Java Generics concept. You may want to read Java Generics. – Ravi Trivedi May 01 '13 at 01:54
  • @RaviTrivedi I know the basics like string,int,double arrays but he didn't explain this far. – 44hz May 01 '13 at 01:56
  • @JDMshifter, That's alright. We read something only after we come to know something about. And that's fair ! – Ravi Trivedi May 01 '13 at 02:16
  • @JDMshifter, Please accept the answer if this has resolved your main issue, thanks ! – Ravi Trivedi May 01 '13 at 03:33
0

The teacher has provided you with the following member variable:

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

This is an ArrayList containing Item objects. The class you've provided, "Item.java", contains the definition of the Item class.

If you consult the Java API documentation here, you'll see that one of the methods provided for an object of class ArrayList is the add method:

public boolean add(E e)

Which "Appends the specified element to the end of this list." Using that method, you can add items of the class Item to your list.

So in your addItems method, you could create a new Item object and add that to your list of items like so:

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

Here we've created a new object of class Item using the provided description, price and isTaxable variables. We then add the item we've just created to our list of items.

sversch
  • 856
  • 8
  • 22