All right, since I don't want to bother my teacher (I'm wondering if he'll eventually get annoyed since I'm having to ask him for help every weekend now...), can anyone help me with my code?
The question is: You operate several hot dog stands distributed throughout town. 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 values.
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 that you can track the total number of hot dogs sold by the stand. Add another method that returns the value in this variable.
Finally, ad a static variable that tracks the total number of hot dogs sold by all hot dog stands and a static method that returns the value in this variable.
This is my class definition...
public class HotDogStand { // instance variables private String hotDogStandID; private int hotDogsSold; private static int totalSold; // mutators public void setHotDogStandID(String standID) { hotDogStandID = standID; } public void setHotDogsSold(int sold) { hotDogsSold = sold; } public void setAll(String standID, int sold) { hotDogStandID = standID; hotDogsSold = sold; } // constructors public HotDogStand() { hotDogStandID = "A Hot Dog Stand"; hotDogsSold = 0; } public HotDogStand(String standID, int sold) { hotDogStandID = standID; if(hotDogsSold >= 0) { hotDogsSold = sold; } else { System.out.println("Error! Stop giving away free hot dogs!"); System.exit(0); }; } // accessors public String getHotDogStandID() { return hotDogStandID; } public int getHotDogsSold() { return hotDogsSold; } public int getTotalSold() { return totalSold; } // other methods public int justSold(int sold) { return sold++; } public static int totalSold(int sold) { return totalSold++; } }
And this is my driver/tester/demo:
import javax.swing.JOptionPane; public class StandDemo { public static void main(String[] args) { String nameStandOne = JOptionPane.showInputDialog("What is the first stand's name?"); String inputFirstSold = JOptionPane.showInputDialog("How many hot dogs have been sold so far for "+nameStandOne+"?"); int firstSold = Integer.parseInt(inputFirstSold); HotDogStand firstStand = new HotDogStand(nameStandOne, firstSold); String nameStandTwo = JOptionPane.showInputDialog("What is the second stand's name?"); String inputSecondSold = JOptionPane.showInputDialog("How many hot dogs have been sold so far for "+nameStandTwo+"?"); int secondSold = Integer.parseInt(inputSecondSold); HotDogStand secondStand = new HotDogStand(nameStandOne, firstSold); System.out.println("The third hot dog stand has just opened!\n"); HotDogStand thirdStand = new HotDogStand(); int k = 1; while(k != 0) { String choiceWord = JOptionPane.showInputDialog("Enter 1 to sell a hot dog! Enter 0 for the next stand."); int choice = Integer.parseInt(choiceWord); if (choice == 1) { System.out.println("Hot dogs sold: "+firstStand.justSold(firstSold)); } else if(choice == 0) { break; } } } }
Obviously, the code isn't done yet. I'm stopping a moment to see if part of it works, but I've hit a roadblock. You see, whenever I try to increase the amount of hot dogs sold by the first stand, nothing actually happens. When, for the first hot dog stand, I enter 1 as the current amount of hot dogs sold and increment it, nothing happens. It remains at one and I'm not sure why.
I'm not very great at class definitions yet (I'm bumbling through them), but I'm hoping to get a bit of help.
The attached file shows the error.
Error.jpg
EDIT: or... it's too small. Nonetheless, the amount of hot dogs sold isn't incrementing and I'm not sure why. The program is compiling, but I have a logical error somewhere in there.