Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

ex1 Sarah owns several ice-cream trucks in Toronto. You will help Sarah track th

ID: 3852144 • Letter: E

Question

ex1 Sarah owns several ice-cream trucks in Toronto. You will help Sarah track the number of ice-creams sold by each of the trucks and the total number of ice-creams sold by all the trucks in the city. For this, define a class named IceCreamTruck, Each IceCreamTruck has a truckID (an instance variable) and an instance variable for ice-creams sold that day by that truck. IceCreamTruck also has two static variables. One static variable tracks the total number of ice-creams sold by all the trucks. The other static variable specifies the price of ice-cream, which Sarah will only update once per year. Add the following methods: A constructor that sets the truckID number to some value and the number of ice-creams sold by that cart to 0. An instance method named sale that increments the number of ice-creams sold by that truck by 1. A toString method that returns the number of ice-creams sold by the truck, that truck's total sales, and labels this using the truck's ID number. A static method to set the cost per ice-cream. A static method that returns the total number of ice-creams sold by all the trucks. A static method that returns the average number of ice-creams sold by all the trucks. A static method to return the revenue (total value of all sales) from all the trucks. Test the class with at least five ice-cream trucks that each sells a different number of ice-creams during the day. A sample screen dialog output is given below - your output does not have to match exactly. The cost of one ice-cream is set to exist2.00 in this example. Ice-cream Sales by Truck: Ice-creams sold by truck1: 2 Total Sales for truck1: exist4.00 Ice-creams sold by truck2: 1 Total Sales for truck2: exist2.00 Ice-creams sold by truck3: 1 Total Sales for truck3: exist2.00 Ice-creams sold by truck4: 1 Total Sales for truck4: exist2.00 Ice-creams sold by truck5: 1 Total Sales for truck5: exist2.00 Total Ice-cream sold by all trucks: 6 Total sales: exist12.00

Explanation / Answer

IceCreamTruck

public class IceCreamTruck {
   //Declaring instance variables and static variables
   private int truckId;
   private int iceCreamSoldThatDay;
   static int totalNoOfIceCreams;
   static int priceOfIceCream;
  
   //Parameterized constructor
   public IceCreamTruck(int truckId) {
       super();
       this.truckId=truckId;
       this.iceCreamSoldThatDay=0;

   }
  
//Method Selling an ice-cream
   public void sale()
   {
       iceCreamSoldThatDay++;
       totalNoOfIceCreams++;
   }

   //Method which gets no of ice creams sold by a truck on that day
   public int getIceCreamSoldThatDay() {
       return iceCreamSoldThatDay;
   }

   //Static Method which sets the cost of an ice cream
   public static void setCostPerIceCream(int costPerIceCream)
   {
       priceOfIceCream=costPerIceCream;
   }
  
   //Static Method which gets total no of ice creams sold by a all trucks
   public static int totNoOfIceCreamsSold()
   {
       return totalNoOfIceCreams;
   }
  
   //Static method which get total no of sales happened
   public static int revenue()
{
       return totNoOfIceCreamsSold()*priceOfIceCream;
}


   //toString method is used to display the contents of an each Truck object inside it
   @Override
   public String toString() {
       return "truckId=" + truckId + ", Ice Creams Sold That Day="
               + iceCreamSoldThatDay+ ", Total Sales by the Truck = $"+getIceCreamSoldThatDay()*priceOfIceCream;
   }

}

______________________

TestClass.java

public class TestClass {

   public static void main(String[] args) {
      
       //Setting the price of an Ice Cream
       IceCreamTruck.setCostPerIceCream(2);
      
       /* Creating 5 IceCreamTruck Objects and calling sale() methods
       * and display each truck Info
       */
       IceCreamTruck truck1=new IceCreamTruck(1);
       truck1.sale();
       truck1.sale();
       System.out.println(truck1.toString());
      
       IceCreamTruck truck2=new IceCreamTruck(2);
       truck2.sale();
       System.out.println(truck2.toString());
      
       IceCreamTruck truck3=new IceCreamTruck(3);
       truck3.sale();
       System.out.println(truck3.toString());
      
       IceCreamTruck truck4=new IceCreamTruck(4);
       truck4.sale();
       System.out.println(truck4.toString());
      
      
       IceCreamTruck truck5=new IceCreamTruck(5);
       truck5.sale();
       System.out.println(truck5.toString());
      
       //Displaying total no of ice-creams sold by all trucks
       System.out.println("Total Ice-Creams sold by all Trucks :"+IceCreamTruck.totNoOfIceCreamsSold());
      
       //Displaying the total sales done by all trucks
       System.out.println("Total Sales $:"+IceCreamTruck.revenue());      

   }

}

______________________

Output:

truckId=1, Ice Creams Sold That Day=2, Total Sales by the Truck = $4
truckId=2, Ice Creams Sold That Day=1, Total Sales by the Truck = $2
truckId=3, Ice Creams Sold That Day=1, Total Sales by the Truck = $2
truckId=4, Ice Creams Sold That Day=1, Total Sales by the Truck = $2
truckId=5, Ice Creams Sold That Day=1, Total Sales by the Truck = $2
Total Ice-Creams sold by all Trucks :6
Total Sales $:12

___________Thank You