Java program not returning any values

Alicia

I am currently working on a class assignment and cannot figure out why I am getting the output that I am getting. The programming question is:

You operate several hotdog stands. Define a class named HotDogStand that has an instance variable for the hot dog stand's ID number and an instance variable for how many hot dogs the stand has sold that day. Create a constructor that allows a user of the class to initialize both variables. Also create a method named justSold that increments by one the number of hot dogs the stand has sold. The idea is that this method will be invoked each time the stand sells a hot dog so the total can be tracked. Add another method that returns the number of hot dogs sold.

Add a static variable that tracks the total number of hot dogs sold by all the stands and a static method that returns the value in this variable.

So my code is:

public class HotDogStand {
    // instance variable declaration
    private int IDNumber;
    private int hotDogsSold = 0;
    private static int totalSold = 0;

    public HotDogStand(int ID, int sold) {
        IDNumber = ID;
        hotDogsSold = sold;
    }

    public int getID() {
        return IDNumber;
    }

    public void setID(int ID) {
        IDNumber = ID;
    }

    public void justSold() {
        if (hotDogsSold > 0) {
            hotDogsSold++;
        }
    }

    public int sold() {
        return hotDogsSold;
    }

    public static int getTotal() {
        return totalSold;
    }
}

And my testing class is:

public class HotDogTest {
    public static void main(String[] args) {
        HotDogStand stand1 = new HotDogStand(1, 11);
        HotDogStand stand2 = new HotDogStand(2, 17);
        HotDogStand stand3 = new HotDogStand(3, 6);

        stand1.getID();
        stand2.getID();
        stand3.getID();
        stand1.setID(1);
        stand2.setID(2);
        stand3.setID(3);
        stand1.justSold();
        stand2.justSold();
        stand3.justSold();
        stand1.justSold();
        stand1.justSold();
        stand1.justSold();
        stand3.justSold();

        stand1.getTotal();
        stand2.getTotal();
        stand3.getTotal();

        int grandTotal = stand1.getTotal() + stand2.getTotal() + stand3.getTotal();

        System.out.println("Stand " + stand1.getID() + " sold a total of " + stand1.getTotal() + " hotdogs.");
        System.out.println("Stand " + stand2.getID() + " sold a total of " + stand2.getTotal() + " hotdogs.");
        System.out.println("Stand " + stand3.getID() + " sold a total of " + stand3.getTotal() + " hotdogs.");

        System.out.println("The total amount of hotdogs sold by all the stands was " + grandTotal);
    }
}

My output is:

Stand 1 sold a total of 0 hotdogs.
Stand 2 sold a total of 0 hotdogs.
Stand 3 sold a total of 0 hotdogs.
The total amount of hotdogs sold by all the stands was 0

bchoi

There is no point at which you change the totalSold variable. justsold increments the instance variable hotDogsSold but getTotal return totalSold.

You need a method getSold which returns the instance variable hotDogsSold.

The other problem is that totalSold is a static variable (it's a class variable; it's not tied to any individual instance of the class like hot dog seller 1 or 2 but rather to the entire model of hot dog sellers). As a result, your grand total would, if totalSold were incremented correctly, give 3 times the number of sold hot dogs for everyone.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related