Results 1 to 3 of 3
Thread: Tracking Values Using Objects
- 10-20-2008, 07:43 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 3
- Rep Power
- 0
Tracking Values Using Objects
I have this program I've been trying to write, which is supposed to loop each time, asking for a stand ID number and number of items sold. I do not know how I should go about tracking which object (stand ID) is being referred to, and to store and track the number of hot dogs sold through each iteration of the loop. Here is what I have so far:
Java Code:package lab5; public class HotDogStand { int standID; int standSold; static int TotalSold = 0; public HotDogStand (int standID, int standSold) { this.standID = standID; this.standSold = standSold; } public HotDogStand (HotDogStand copy) { standID = copy.standID; standSold = copy.standSold; } //don't know how to use copy constructor public void justSold() { standSold = standSold + 1; TotalSold = TotalSold + 1; } public void setStandSold(int newStandSold) { standSold = newStandSold; } public int getStandSold() { return standSold; } public void setStandID(int newStandID) { standID = newStandID; } public int getStandId() { return standID; } public static int getTotalSold() { return TotalSold; } } public class Main { public static void main ( String[] args) { Scanner input = new Scanner(System.in); HotDogStand myStand1 = new HotDogStand(0, 0); System.out.println("Enter ID number of stand: "); int standID = input.nextInt(); myStand1.setStandID(standID); System.out.println("How many dogs would you like: "); int standSold = input.nextInt(); myStand1.setStandSold(standSold); myStand1.justSold(); HotDogStand myStand2 = new HotDogStand(0,0); myStand2.justSold(); HotDogStand myStand3 = new HotDogStand(91011,0); myStand3.justSold(); myStand3.justSold(); myStand3.justSold(); myStand3.justSold(); myStand3.justSold(); myStand3.justSold(); System.out.println (myStand1.getStandSold()+" HD sold by Stand "+ myStand1.getStandId()); System.out.println (myStand2.getStandSold()+" HD sold by Stand "+ myStand2.getStandId()); System.out.println (myStand3.getStandSold()+" HD sold by Stand "+ myStand3.getStandId()); System.out.println (" Total # of HD sold "+ myStand1.getTotalSold()); } }
- 10-20-2008, 07:52 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 23
Are you looking how to deal with two values at the time?
- 10-20-2008, 08:18 PM #3
You need some kind of container to hold the info for a particular stand. Perhaps a HashMap. Where the key is the stand ID and the value is the object holding the data for that stand.
Or if the stand IDs are in a contiguous range of numbers, you could use an array with the stand ID being the index into the array of objects with the info.Last edited by Norm; 10-20-2008 at 08:20 PM.
Similar Threads
-
System Tray Message tracking.
By Hichhiker in forum AWT / SwingReplies: 2Last Post: 10-11-2008, 03:58 AM -
How to show pixel Coordinate and RGB value when mouse is tracking over an image
By Mazharul in forum Java 2DReplies: 1Last Post: 08-25-2008, 08:48 PM -
Retaining DB values as well as Dynamically generated Values.. Help Needed !
By rajivjha in forum Advanced JavaReplies: 0Last Post: 05-22-2008, 10:53 AM -
Download Tracking
By ganeshsalian in forum Advanced JavaReplies: 7Last Post: 07-30-2007, 04:57 PM -
Tracking Operating System
By Mansi in forum NetworkingReplies: 2Last Post: 07-23-2007, 01:41 PM
Bookmarks