JAVA Write a program that manages the desserts available for
JAVA :Write a program that manages the desserts available for purchase as a dessert shop. The class DessertCafe is a driver class containing a main() method that will be used to test the other classes, all of which have package-level access. Thus, do not provide a visibility modifier for those classes when you define them inside DessertCafe.java.
The abstract class Dessert contains data members name and cost. It also defines public methods String getName(), which returns the name of the dessert, and public method int getCost(). Implement both of these methods and the standard methods toString() (return the name and cost of the dessert concatenated in a String) and equals(Object) in the following subclasses:
The Cake class is derived from the Dessert class. A Cake object has a number of layers (int). The price of a cake is $15 + $2 per layer. For example, a 3-layer cake costs $21. The number of layers is passed as the parameter to the Cake constructor.
The CookiePack class is derived from the Dessert class. A CookiePack object has a number (int) indicating how many cookies are in the pack and a price per cookie in dollars (int) which are used to determine its cost. For example, a 5-cookie pack at $2 per cookie = $10. The name/type of the cookies (e.g., Chocolate chip cookies) and these two integers are passed in as the parameters to the CookiePack constructor.
The IceCream class is derived from the Dessert class. An IceCream object simply has a cost in dollars (int). The starting value of this cost is passed as the parameter to the IceCream constructor.
The Sundae class is derived from the IceCream class. The cost of a Sundae is the cost of the IceCream plus the cost of the topping in dollars (int). The starting value of the topping’s cost is passed as a parameter to the Sundae constructor.
Inside of your DessertCafe class write a main() method that defines an ArrayList of Dessert ref- erences: ArrayList<Dessert> desserts. Due to polymorphism you can store objects of any subclass of Dessert inside this ArrayList. Create two objects of every type of subclass of Dessert (i.e., Cake, CookiePack, IceCream and Sundae). Pick appropriate arguments (actual values) for each constructor when you construct each object. Add these objects in any order to the ArrayList. Finally, write a for-loop that iterates over the ArrayList, printing the name and cost of every dessert item in the collection by calling the getName() and getCost() methods.
Solution
import java.util.*;
 import java.util.ArrayList;
 import java.util.List;
 //Abstract class Dessert
 abstract class Dessert
 {
    String name;
    int cost;
    //Abstract methods
    abstract String getName();
    abstract int getCost();
 }
 //Class Cake derived from abstract Dessert class
 class Cake extends Dessert
 {
    //Cake constructor
    Cake (String type, int m)
    {
        name = type;
        cost = 15 + (m * 2);
    }
    //Overriding abstract method
    String getName() { return name; }
    int getCost() { return cost; }
 }
 //Class CookiePack derived from abstract Dessert class
 class CookiePack extends Dessert
 {
    //CookiePack constructor
    CookiePack(String type, int h, int p)
    {
        name = type;
        cost = h * p;
    }
    //Overriding abstract method
    String getName() { return name; }
    int getCost() { return cost; }
 }
 //Class IceCream derived from abstract Dessert class
 class IceCream extends Dessert
 {
    static int ci;
    //IceCream constructor
    IceCream(String type, int p)
    {
        ci = p;
        name = type;
        cost = p;
    }
    //Overriding abstract method
    String getName() { return name; }
    int getCost() { return cost; }
 }
 //Class Sundae derived from abstract Dessert class
 class Sundae extends Dessert
 {
    //Sundae constructor
    Sundae(String type, int p)
    {
        name = type;
        cost = IceCream.ci + p;
    }
    //Overriding abstract method
    String getName() { return name; }
    int getCost() { return cost; }
 }
class DessertCafe
 {
    public static void main(String ss[])
    {
        //Creates an array list
        ArrayList<Dessert> desserts = new ArrayList<Dessert>();
       //Creates object for cake class
        Cake c1 = new Cake(\"Cream\",10);
        Cake c2 = new Cake(\"Plain\",20);
       //Creates object for CookiePack class
        CookiePack cp1 = new CookiePack(\"Chocolate\", 2, 11);
        CookiePack cp2 = new CookiePack(\"Orange\", 3, 22);
       //Creates object for IceCream class
        IceCream Ic1 = new IceCream(\"Chocolate\",20);
        IceCream Ic2 = new IceCream(\"Mango\",30);
       //Creates object for Sundae class
        Sundae sd1 = new Sundae(\"Chocolate\",20);
        Sundae sd2 = new Sundae(\"Orange\",30);
       //Adds object to array list
        desserts.add(c1);
        desserts.add(c2);
        desserts.add(cp1);
        desserts.add(cp2);
        desserts.add(Ic1);
        desserts.add(Ic2);
        desserts.add(sd1);
        desserts.add(sd2);
       
        //Calls all methods using array list
        for (int i = 0; i < desserts.size(); i++)
        {
            if(i == 0)
                System.out.println(\"\ Cake\");
            else if(i == 2)
                System.out.println(\"\ CookiePack\");
            else if(i == 4)
                System.out.println(\"\ IceCream\");
            else if(i == 6)
                System.out.println(\"\ Sundae\");
             System.out.println(\"Name: \" + desserts.get(i).getName());
             System.out.println(\"Cost: \" + desserts.get(i).getCost());  
            }
        }
 }
Output:
 Cake
 Name: Cream
 Cost: 35
 Name: Plain
 Cost: 55
CookiePack
 Name: Chocolate
 Cost: 22
 Name: Orange
 Cost: 66
IceCream
 Name: Chocolate
 Cost: 20
 Name: Mango
 Cost: 30
Sundae
 Name: Chocolate
 Cost: 50
 Name: Orange
 Cost: 60



