You operate several hot dog stands distributed throughout town. Define a class n
ID: 3701459 • Letter: Y
Question
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 number of hot dogs sold. Finally, add 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. Write a main method to test your class with at least three hot dog stands that each sell a variety of hot dogs.Explanation / Answer
Please find my implementation :
public class HotDogStand
{
// Add 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.
private static int totalSold = 0;
public static int getTotalSold()
{
return totalSold;
}
// *Name of each hot dog stand
private String name;
// *Stand's ID number
private int id;
// *How many hot dogs the stand has sold that day.
private int numSold;
// Create a constructor that allows a user of the class to initialize values.
public HotDogStand(String name, int id)
{
this.name = name;
this.id = id;
}
//Also create a method named SetJustSold()(Hint ++) that increments the number of hot dogs the stand has sold by one and should also increase the TotalSold by one.
public void setJustSold()
{
numSold++;
totalSold++;
}
// Add another method that returns the number of hot dogs sold.
public int getNumSoldToday()
{
return numSold;
}
public int getID()
{
return id;
}
public int getName()
{
return name;
}
// Create a toString() method and a copy constructor.
public String toString()
{
return name+" ("+id+") - "+numSold;
}
public HotDogStand(HotDogStand rhs)
{
id = rhs.id;
name = ""+rhs.name;
numSold = rhs.numSold;
}
}
public class Test
{
// Write the main method to test your class. The main method is in a separate class from HotDogStand().
public static void main(String[] args)
{
// 1. You should create three hot dog stands with name, ID number and number hotdogs sold.
HotDogStand HotDogStand("one", 1);
HotDogStand two = new HotDogStand("two", 2);
HotDogStand three = new HotDogStand("three", 3);
// 2. Uses the toString() to print out all three hotdog stands.
System.out.println(one);
System.out.println(two);
System.out.println(three);
// 3. Print out total sold for all stands.
System.out.println("Total: "+HotDogStand.getTotalSold());
// 4. Initialize SetJustSold(), for each stand. This will add one hot dog sold to each stand and to total sold.
one.setJustSold();
two.setJustSold();
three.setJustSold();
// 5. Print out results, call the getName() to print out name, call the getID() to print out ID number and getNumberSold() to display number sold for all stands.
System.out.println("Name ID Number Sold");
System.out.println("________________________________");
System.out.println(one.getName()+" "+one.getID()+" "+one.getNumSoldToday());
System.out.println(two.getName()+" "+two.getID()+" "+two.getNumSoldToday());
System.out.println(three.getName()+" "+three.getID()+" "+three.getNumSoldToday()+" ");
// 6. Print out total sold for all stands.
System.out.println("Total: "+HotDogStand.getTotalSold());
// 7. Use the copy constructor to create stand number four with the values of stand one.
HotDogStand four = new HotDogStand(one);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.