Results 1 to 3 of 3
  1. #1
    Npcomplete is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default 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());
        }
    }
    I'm totally lost as to where to go next.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,459
    Rep Power
    25

    Default

    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

  1. System Tray Message tracking.
    By Hichhiker in forum AWT / Swing
    Replies: 2
    Last Post: 10-11-2008, 03:58 AM
  2. Replies: 1
    Last Post: 08-25-2008, 08:48 PM
  3. Replies: 0
    Last Post: 05-22-2008, 10:53 AM
  4. Download Tracking
    By ganeshsalian in forum Advanced Java
    Replies: 7
    Last Post: 07-30-2007, 04:57 PM
  5. Tracking Operating System
    By Mansi in forum Networking
    Replies: 2
    Last Post: 07-23-2007, 01:41 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
  •