Results 1 to 5 of 5
- 11-13-2011, 08:27 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 45
- Rep Power
- 0
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...)
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.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()); } }
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..
- 11-13-2011, 08:32 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-13-2011, 08:42 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 45
- Rep Power
- 0
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.
On the right track?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) { } }
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...:()
- 11-13-2011, 09:00 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Re: Need help with CashRegister program
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-13-2011, 10:29 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 45
- Rep Power
- 0
Similar Threads
-
How to code a program to send messages to a chat program?
By josh2992 in forum New To JavaReplies: 2Last Post: 04-02-2011, 12:57 PM -
How would I open a program from a single button of another program. Help...
By decgaid06 in forum New To JavaReplies: 13Last Post: 03-22-2011, 06:49 AM -
changing my program to array working program
By Chewart in forum New To JavaReplies: 39Last Post: 11-18-2009, 06:53 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks