Define a class named CookieStore that has an instance variab

Define a class named CookieStore that has an instance variable for the cookie store\'s ID number and an instance variable for how many cookies the store sold that day. Create a constructor that allows the user to initialize both values through parameters. Also create a method named JustSold that increments the number of cookies the store has sold by one. This method will be invoked each time the store sells a hot dog so that you can track the total number of cookies sold by the store. Add another method getSold that returns the number of cookies sold. Finally, add a static variable TotalCookies that tracks the total number of cookies sold by all cookie stores and a static method getTotal that returns the value in this variable. Write a main method to test your class with at least three cookie stores that sell a variety of cookies.

Use Java.

Solution

/*
CookieStore.java
*/

public class CookieStore
{
   private int storeID;
   private int cookiesSold;
   public static int TotalCookies = 0;

   public CookieStore(int id, int sold)
   {
       storeID = id;
       cookiesSold = sold;
       TotalCookies = TotalCookies + sold;
   }

   public void JustSold()
   {
       cookiesSold++;
       TotalCookies++;
   }

   public int getSold()
   {
       return cookiesSold;
   }

   public static int getTotal()
   {
       return TotalCookies;
   }

   public static void main(String[] args)
   {
       CookieStore store1 = new CookieStore(23, 3);
       CookieStore store2 = new CookieStore(44, 1);
       CookieStore store3 = new CookieStore(34, 5);

       for (int i=1; i <= 5; i++ )
       {
           store1.JustSold();
           store2.JustSold();
           store3.JustSold();
       }

       System.out.println(\"Cookies sold by store1: \" + store1.getSold());
       System.out.println(\"Cookies sold by store2: \" + store2.getSold());
       System.out.println(\"Cookies sold by store3: \" + store3.getSold());

       System.out.println(\"\ Total Cookies sold: \" + CookieStore.getTotal());
   }
}

/*
output:

Cookies sold by store1: 8
Cookies sold by store2: 6
Cookies sold by store3: 10

Total Cookies sold: 24

*/

Define a class named CookieStore that has an instance variable for the cookie store\'s ID number and an instance variable for how many cookies the store sold th
Define a class named CookieStore that has an instance variable for the cookie store\'s ID number and an instance variable for how many cookies the store sold th

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site