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

The programmer will use and extend the classes and methods developed in Homework

ID: 3577807 • Letter: T

Question

The programmer will use and extend the classes and methods developed in Homework #7 to develop a Cash Register type of application. This application will declare an enumerated type ‘DessertType’, which will be used in a switch statement to determine which action needs to be taken.

Depending on the DessertType, the program should prompt for specific details in order to calculate the price for each item.

CANDY

Prompt for Name, Number of Pounds, Price per Pound

COOKIE

Prompt for Name, Number of Cookies, Price per Dozen

ICE CREAM

Prompt for Name, Number of Scoops, Price per Scoop

SUNDAE

Prompt for Name, Number of Scoops, Price per Scoop, Number of Toppings, Name of Toppings and Price per Topping

This will require a loop to Prompt for each Topping based on “Number of Toppings”

The application should be called ‘DessertShop’, which will prompt the user for items to purchase. This application will prompt the user for the name of the product. If no name is entered, the application will calculate the total cost of items entered.

The application will define four arrays for each DessertItem, which can store up to 10 items each.

Each derived class will contain a static variable which will store the totalDessertItem cost and total DessertItem tax (if applicable) for that specificDessertType. The CANDY and COOKIE dessert types are taxable at 7%. The ICE CREAM and SUNDAY dessert types are non-taxable.

The DessertItem superclass will also contain a total cost and total tax static variable that will be shared between all items that were derived from that superclass.

Upon successfully entering an item, the total cost and total tax of that DessertItemwill be updated, along with the total cost and total tax for all items. The application should check an item to determine if that item has already been entered, except for Sundae’s. If the item has already been entered, the existing information should be updated to include the new items.

Upon entering all items, the application should print out a summary of theDessertItem’s, along with the count, the cost and tax. (See Sample Output)

CANDY   [2] Cost: $4.52 Tax: $0.32

COOKIES [24] Cost: $12.96 Tax: $0.91

ICE CREAM [1] Cost: $3.99

SUNDAE [4] Cost: $8.87

---- -------- -----

Subtotals [31] $30.34 $1.23

   ========

Total $31.57

The assignment will be graded based upon all tasks being performed and handed in with all required documentation.

Description   Points

Definition of the abstract DessertItem superclass, including the definition of a static totalCost variable and a static totalTax variable   10 points

Definition of enumerated DessertType and use of switch statement to determine actions required   10 points

Use of Arrays for storing up to 10 items of the four different DessertType’s.   10 points

Definition of the Candy derived class   5 points

Definition of the Cookie derived class   5 points

Definition of the IceCream derived class   5 points

Definition of the Sundae derived class   5 points

package dessertshop;

import java.text.NumberFormat;

abstract class DessertItem { //abstract superclass where types of DessertItem will be derived
protected String name; //Instance variable name of item String
NumberFormat currency = NumberFormat.getCurrencyInstance(); //Formats numbers to a currency format to the nearest cent

public DessertItem () //default constructor DessertItem()
{
this.name = "";
}

public DessertItem ( String itemName ) //constructor to pass in the name of the item
{
this.name = itemName;
}

public String getName ()
{
return name;
}

public void setName ( String name )
{
this.name = name;
}

public abstract double getCost ();

public String toString()
{
return name;
}

}

class Candy extends DessertItem { //Candy class derived from DessertItem class
double weight;
double pricePerPound;
double candyTotal;

public Candy ( String name, double weight, double pricePerPound )
{
super( name ); //calls name from superclass
this.weight = weight;
this.pricePerPound = pricePerPound;
}

@Override

public double getCost ()
{
this.candyTotal = this.weight*this.pricePerPound;
return this.candyTotal;
}

public String toString()
{
return name + " " + currency.format(this.getCost()) + " "
+ this.weight + " lbs @ " + currency.format(this.pricePerPound);
}
}

class Cookie extends DessertItem { //Cookie class derived from DessertItem class
int number;
double pricePerDozen;
double cookieTotal;

Cookie ( String name, int number, double pricePerDozen )
{
super( name ); //calls name from superclass
this.number = number;
this.pricePerDozen = pricePerDozen;
}

@Override
  
public double getCost ()
{ this.cookieTotal = (this.number*this.pricePerDozen)/12;
return this.cookieTotal;
}

public String toString()
{
return name + " " + currency.format(this.getCost()) + " "
+ this.number + " cookies @ " + currency.format(this.pricePerDozen) + " per Dozen";
}
}

class IceCream extends DessertItem { //IceCream class derived from DessertItem class
double cost;

IceCream ( String name, double cost )
{
super( name ); //calls name from superclass
this.cost = cost;
}

@Override

public double getCost ()
{
return this.cost;
}

public String toString()
{
return name + " " + currency.format(this.getCost());
}
}

class Sundae extends IceCream { //Sundae class derived from DessertItem class
String toppingName;
double toppingCost;
double sundaeTotal;

Sundae ( String name, double cost, String toppingName, double toppingCost )
{
super( name, cost ); //calls name from superclass and cost from IceCream
this.toppingName = toppingName;
this.toppingCost = toppingCost;
}

@Override

public double getCost ()
{
this.sundaeTotal = this.cost + this.toppingCost;
return this.sundaeTotal;
}
/*
Method : toString()
Parameters : void
Returns : String
Description : Creates a String, returning Chocolate Chip Ice Cream, Fudge and cost of each. Uses derived getCost() of superclass
*/
public String toString()
{
return name + " " + currency.format(this.getCost()) + " " + this.toppingName
+ " @ " + currency.format(this.toppingCost);
}

}

public class DessertShop { //Test harness provided by instructor
public static void main ( String[] args )
{
Candy item1 = new Candy( "Peanut Butter Fudge", 2.25, 3.99 );
Cookie item2 = new Cookie( "Oatmeal Raisin Cookies", 4, 3.99 );
IceCream item3 = new IceCream( "Vanilla Ice Cream", 1.05 );
Sundae item4 = new Sundae( "Chocolate Chip Ice Cream", 1.45, "Hot Fudge", 0.50 );
System.out.println( item1 );
System.out.println( item2 );
System.out.println( item3 );
System.out.println( item4 );
}
}

Explanation / Answer

Redefined Abstract Class:

package temp;

import java.text.NumberFormat;
import java.util.Scanner;

abstract class DessertItem { //abstract superclass where types of DessertItem will be derived
   protected String name; //Instance variable name of item String
   NumberFormat currency = NumberFormat.getCurrencyInstance(); //Formats numbers to a currency format to the nearest cent
   static double totalTax;
   static double totalCost;
   Scanner sc;
  
   public DessertItem () //default constructor DessertItem()
   {
       this.name = "";
   }
  
   public DessertItem ( String itemName ) //constructor to pass in the name of the item
   {
       this.name = itemName;
   }
  
   public String getName ()
   {
       return name;
   }
     
   public void setName ( String name )
   {
       this.name = name;
   }
  
   public abstract void calculateCost ();
  
   public String toString()
   {
       return name;
   }
}

class Candy extends DessertItem { //Candy class derived from DessertItem class
   double weight;
   double pricePerPound;
   static double candyTotal = 0;
   static double candyTax = 0;
   String candyname;
  
   public Candy (double weight, double pricePerPound )
   {
       this.weight = weight;
       this.pricePerPound = pricePerPound;
   }
  
   @Override
   public void calculateCost()
   {
       System.out.println("Enter the name of Candy: ");
       candyname = sc.nextLine();
       candyTotal = this.weight*this.pricePerPound;
       candyTax = 0.07 * candyTotal;
       totalCost += candyTotal;
       totalTax += candyTax;
   }
  
   public double getCost(){
       return totalCost;
   }
  
   public double getTax(){
       return totalTax;
   }
}

class Cookie extends DessertItem { //Cookie class derived from DessertItem class
   int quantity;
   double pricePerDozen;
   static double cookieTotal = 0;
   static double cookieTax = 0;
   String cookiename;
   Cookie (int number, double pricePerDozen )
   {
       this.quantity = number;
       this.pricePerDozen = pricePerDozen;
   }

   @Override
   public void calculateCost()
   {
       System.out.println("Enter the cookie name");
       cookiename = sc.nextLine();
       cookieTotal = this.quantity * this.pricePerDozen;
       cookieTax = 0.07 * cookieTotal;
       totalCost += cookieTotal;
       totalTax += cookieTax;
   }
  
  
}

class IceCream extends DessertItem { //IceCream class derived from DessertItem class
   int scoops;
   double cost;
   static double iceCost = 0;
   String icecreamname;
   IceCream ( int scoops, double cost )
   {
       this.scoops = scoops;
       this.cost = cost;
   }

   @Override
   public void calculateCost()
   {
       System.out.println("Enter the name of icecream");
       icecreamname = sc.nextLine();
       iceCost = this.scoops * this.cost;
       totalCost += iceCost;  
   }  
}

class Sundae extends DessertItem { //Sundae class derived from DessertItem class
   String toppingName;
   double toppingCost;
   int scoops;
   double priceperscoop;
   static double sundaeTotal = 0;
   String sundaename;
   String toppingname;
  
   Sundae (String toppingName, double toppingCost,int scoops,double priceperscoop )
   {
       this.toppingName = toppingName;
       this.toppingCost = toppingCost;
       this.scoops = scoops;
       this.priceperscoop = priceperscoop;
   }
  
   @Override
   public void calculateCost()
   {
       System.out.println("Enter the sundae name");
       sundaename = sc.nextLine();
       System.out.println("Enter the topping name");
       toppingname = sc.nextLine();
       sundaeTotal += this.toppingCost;
       sundaeTotal += this.scoops * this.priceperscoop;
       totalCost += sundaeTotal;  
   }
  
  
}
/*
public class DessertShop { //Test harness provided by instructor
public static void main ( String[] args )
{
Candy item1 = new Candy( "Peanut Butter Fudge", 2.25, 3.99 );
Cookie item2 = new Cookie( "Oatmeal Raisin Cookies", 4, 3.99 );
IceCream item3 = new IceCream( "Vanilla Ice Cream", 1.05 );
Sundae item4 = new Sundae( "Chocolate Chip Ice Cream", 1.45, "Hot Fudge", 0.50 );
System.out.println( item1 );
System.out.println( item2 );
System.out.println( item3 );
System.out.println( item4 );
}
}*/

DessertItem class:

package temp;
import java.util.Scanner;

import temp.DessertItem;

public class DessertItem {
  
   enum DessertType {Candy,Cookie,IceCream,Sundae}
  
  
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       int[] candy = new int[10];
       int[] cookie = new int[10];
       int[] icecream = new int[10];
      
      
       Scanner sc = new Scanner(System.in);
       String productname;
       int quantity;
       System.out.println("Enter the items to purchase");
       quantity = sc.nextInt();
       System.out.println("Enter the name of the product");
       System.out.println("1.Candy, 2.Cookie, 3.Icecream");
       productname = sc.next();
      
       DessertType dt = DessertType.valueOf(productname);
      
       switch(dt){
           case Candy:
               Candy di = new Candy(quantity,4.52);
               di.calculateCost();
               break;
              
           case Cookie:
               Cookie ck = new Cookie(quantity,4.52);
               ck.calculateCost();
               break;
              
           case IceCream:
               IceCream ic = new IceCream(quantity, 3.99);
               ic.calculateCost();
               break;
              
           case Sundae:
               Sundae sc = new Sundae(quantity, 3.99);
               sc.calculateCost();
               break;
              
           default:
               System.out.println("Entered choice is wrong");
       }
      
   }

}