Results 1 to 5 of 5
  1. #1
    Aimforthehead is offline Member
    Join Date
    Sep 2011
    Posts
    45
    Rep Power
    0

    Default Need help with CashRegister program

    I have created a program using a CashRegister class. (Well, some of it. I am new to objects and whatnot so most was given to me...)
    Java Code:
    /**
    * A simulated cash register that tracks the item count and
    * the total amount due.
    * 
    * @author
    * B14 - 11/5/11, 11:30 AM
    * CashRegister.java 
    */
    public class CashRegister {
    private int itemCount;
    private double totalPrice;
    
    /**
     * Adds an item to this cash register.
     * @param price the price of this item
     */
    public void addItem(double price) {
    	itemCount++;
    	totalPrice = totalPrice + price;
    }
    
    /**
     * Gets the price of all items in the current sale.
     * @return the total amount
     */
    public double getTotal() {
    	return totalPrice;
    }
    
    /**
     * Gets the number of items in the current sale.
     * @return the item count
     */
    public int getCount() {
    	return itemCount;
    }
    
    /**
     * Clears the item count and the total.
     */
    public void clear() {
    	itemCount = 0;
    	totalPrice = 0;
    }
    
    /**
     * creates 3 new cash register objects
     * with items added to find the total count and total value of 
     * the elements in each object.
     * @param args
     */
    public static void main(String[] args) {
    	CashRegister reg = new CashRegister();
    	reg.clear();
    	reg.addItem(0.95);
    	reg.addItem(0.95);
    	CashRegister reg1 = new CashRegister();
    	reg1.addItem(3.25);
    	reg1.addItem(1.95);
    	CashRegister reg2 = new CashRegister();
    	reg2.addItem(3.25);
    	reg2.clear();
    	System.out.println(reg.getCount() + " " + reg.getTotal());
    	System.out.println(reg1.getCount() + " " + reg1.getTotal());
    	System.out.println(reg2.getCount() + " " + reg2.getTotal());
    }
    
    }
    Now, I need to make an arrayList of doubles that keeps track of each price added in each item. I am told I can remove itemCount and totalPrice instance variables, and add a method called displayAll.
    So, I figured I could put the arrayList under the class CashRegisterList outside of any method. And use main to add values to it...But what replaces itemCount and totalPrice? I'm not sure what i'd put under each method..

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: Need help with CashRegister program

    My guess would be that the totalPrice can be calculated by summing all individual prices in the list; the itemCount would just be the length of that list ... indeed that list should be a member variable.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Aimforthehead is offline Member
    Join Date
    Sep 2011
    Posts
    45
    Rep Power
    0

    Default Re: Need help with CashRegister program

    Thanks for the fast reply.
    This is what I have to start off with, as an outline sort of.
    Java Code:
    import java.util.ArrayList;
    
    /**
    * A simulated cash register that tracks the item count and
    * the total amount due.
    * 
    * @author 
    * B14 - 
    * CashRegisterList.java 
    */
    public class CashRegisterList {
    	ArrayList<Double> prices;
    	
    
    
    
    public void displayAll() {
    
    	
    }
    
    
    /**
     * Adds an item to this cash register.
     * @param price the price of this item
     */
    public void addItem(double price) {
    	
    }
    
    /**
     * Gets the price of all items in the current sale.
     * @return the total amount
     */
    public double getTotal() {
    	
    }
    
    /**
     * Gets the number of items in the current sale.
     * @return the item count
     */
    
    
    
    /**
     * Clears the item count and the total.
     */
    public void clear() {
    
    }
    
    /**
     * @param args
     */
    public static void main(String[] args) {
    	
    }
    
    }
    On the right track?
    Oh and to print out each price, would I use something like
    for (int i:prices){
    System.out.println(prices.get(i));
    }

    or is that improper? (I am new to arraylists as well as objects so...:()

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: Need help with CashRegister program

    Quote Originally Posted by Aimforthehead View Post
    On the right track?
    Oh and to print out each price, would I use something like
    for (int i:prices){
    System.out.println(prices.get(i));
    }

    or is that improper? (I am new to arraylists as well as objects so...:()
    Yep, you're on the right track but the last snippet is incorrect, that's not how the enhanced for loop works; it's more like this:

    Java Code:
    for (double price : prices)
       System.out.println(price);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Aimforthehead is offline Member
    Join Date
    Sep 2011
    Posts
    45
    Rep Power
    0

    Default Re: Need help with CashRegister program

    Ah it all makes sense now, I owe ya'.

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2011, 12:57 PM
  2. Replies: 13
    Last Post: 03-22-2011, 06:49 AM
  3. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •