This programming assignment will the lab from Module #12, DessertShop to develop a Cash Register type of application. The purpose of this assignment is to use those programming concepts that have been developed recently.
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 and the Price per Topping
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 total DessertItem cost and totalDessertItem tax (if applicable) for that specific DessertType. 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 DessertItem will 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 the DessertItem’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
After the total has been displayed, the application will prompt the user to see if they would like to save the receipt to a file. If ‘yes’, the application should prompt the user for the file name they would like to write the receipt to. The application should then open a text file and output the receipt information to the output file.
Programming Code Use in Lab 12:
package dessertshop;
import java.text.NumberFormat;
abstract class DessertItem {
protected String name;
public DessertItem()
{
this.name = “”;
}
public DessertItem( String name )
{
this.name = name;
}
public final String getName()
{
return name;
}
public abstract double getCost();
}
class Candy extends DessertItem
{
private double weight;
private double pricePerPound;
public Candy( String name, double unitWeight, double unitPrice )
{
super( name );
this.weight = unitWeight;
this.pricePerPound = unitPrice;
}
@Override
public double getCost()
{
return( weight * pricePerPound );
}
@Override
public String toString()
{
NumberFormat formatter = NumberFormat.getCurrencyInstance();
return( name + “ttt” + formatter.format( this.getCost()) + “nt” + this.weight + ” lbs @ ” + formatter.format( this.pricePerPound ));
}
}
//Implementing class Cookie
class Cookie extends DessertItem
{
//Private Instance Variables
private int numCookies;
private double pricePerDozen;
//Constructor
public Cookie( String name, int noCookies, double price )
{
super( name );
this.numCookies = noCookies;
this.pricePerDozen = price;
}
@Override
public double getCost()
{
return( numCookies * (pricePerDozen/12.0) );
}
@Override
public String toString()
{
NumberFormat formatter = NumberFormat.getCurrencyInstance();
return( name + “ttt” + formatter.format( this.getCost()) + “nt” + this.numCookies + ” cookies @ ” + formatter.format( this.pricePerDozen ) + ” per Dozen “);
}
}
//Implementing class IceCream
class IceCream extends DessertItem
{
//Private instance variables
private int numScoops;
private double pricePerScoop;
//Constructor
public IceCream( String name, int noScoops, double price )
{
super( name );
this.numScoops = noScoops;
this.pricePerScoop = price;
}
@Override
public double getCost()
{
return( numScoops * pricePerScoop );
}
@Override
public String toString()
{
NumberFormat formatter = NumberFormat.getCurrencyInstance();
return( name + “ttt” + formatter.format( this.getCost()) + “nt” + this.numScoops + ” Scoops @ ” + formatter.format( this.pricePerScoop ) + ” per Scoop “);
}
}
public class DessertShop
{
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”, 2, 1.05 );
System.out.println( item1 );
System.out.println( item2 );
System.out.println( item3 );
}
}
Expert 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”);
}
}
}