Class design problem for JAVA.(2 CLASSES 1 DRIVER)
Please Follow the directions. This is an intro to computer science class.
NO ARRAYLIST, NO IMPORTS(ONLY SCANNER).KEEP EVERYTHING AS SIMPLE AS YOU CAN.
Class Design: Donut (Suggested Time Spent: < 15 minutes)
The Donut class is intended to be an abstract and simplified representation of a yummy edible donut.
Class Properties (MUST be private)
• Name of the donut
• Type of donut
• Price of the donut Class Invariants
• Donut name must not be empty (Default: “Classic”)
• Type of donut can only be one of the following: “Plain,” “Filled,” or “Glazed” (Default: “Plain”)
Class Components
• Public Getter and Private Setter for name, type, and price – enforce invariants
• A constructor that takes in as argument the name and type of the donut (enforce invariants)
o Determine the price of the donut based on the type:
▪ $0.99 for plain
▪ $1.29 for glazed
▪ $1.49 for filled
• A toString() method that returns the name, type, and price of the donut.
• An equals() method that returns true if it’s the same name and type (regardless of price).
Class Design: DonutBox (Suggested Time Spent: < 15 minutes)
The DonutBox class is intended to be an abstract and simplified representation of a box of donuts. Among other things, this object should be able to list the price of the donuts.
Class Properties (MUST be private)
• Donuts (Donut[]) – an array of donuts
• Count – number of donuts in the box Class Invariants
• A box can have at most a dozen donuts Class Components
• Public getter ONLY for count • public void addDonut (Donut order) {}
• public double getPrice() {}
o Returns the total price (before tax) of the donuts in the box, with applicable discount:
▪ Under 6 donuts, no discount
▪ 6 to 12 donuts, 10% discount
▪ 12 donuts exactly, 20% discount
• public String toString() {} – returns a String containing all the donuts in the box
• (Extra Credit: 1 point)
An enum for box type (Small, Medium, Large)
• (Extra Credit: 1 point)
A public method that returns the size of the box as an enum constant.
o Small box fits exactly one donut, medium fits up to six donuts, large fits up to 12.
Driver Class: DonutBoxDriver (Suggested Time Spent: < 10 minutes)
The DonutBoxDriver class is intended to be a simple driver to test the Donut and DonutBox objects and should include a main() method as well as other helper methods. At minimum, the driver class should:
• Create a box with only one donut
• Create a box of up to six donuts
• Create a box of more than six but less than twelve donuts
• Create a box of dozen donuts
• For each box created
o Print out the contents
o Print out the price (before tax) of the donuts in the box
Grading Criteria
• Donut class object (Total: 15 Points)
o [1 Point] Proper class definition syntax
o [3 Points] Implements all required properties
o [1 Points] Implements required getters
o [2 Points] All invariants properly enforced
o [3 Points] Constructor properly implemented
o [3 Points] toString method properly implemented
o [2 Points] equals method properly implemented
• DonutBox class object (Total: 15 Points) o
[1 Point] Proper class definition syntax
o [2 Points] Implements all required properties
o [1 Point] Implements required getter
o [2 Points] All invariants properly enforced
o [9 Points] Methods (addDonut, getPrice, toString) properly implemented (3 points per method)
• DonutBoxDriver program (Total: 10 Points)
o [1 Point] Proper class definition syntax
o [1 Point] Proper main method syntax
o [8 Points] Create, print contents and price of the four boxes (2 points per box created)
Expert Answer
Java program to implement Donut orders by using two class and a driver class.
The objective is to create multiple donut boxes. Each box contains a number od donuts.then print the total price
and size of each box*/
import java.util.Scanner;
//class Donut defined
class Donut
{
private String name;
private String type;
private double price;
//default constructor
public Donut()
{
name=”Classic”;
type=”Plain”;
price=0.99;
}
//constructor to set the donut name and type and calculate price
public Donut(String name,String type)
{
this.name=name;
this.type=type;
if(type.equalsIgnoreCase(“Plain”))
price=0.99;
if(type.equalsIgnoreCase(“glazed”))
price=1.29;
if(type.equalsIgnoreCase(“filled”))
price=1.49;
}
//getter for name
public String getName()
{
return name;
}
//getter for type
public String getType()
{
return type;
}
//getter for price
public double getPrice()
{
return price;
}
//setter for name
private void setName(String name)
{
this.name=name;
}
//setter for type
private void setType(String type)
{
this. type=type;
}
//setter for price
private void setPrice(double price)
{
this.price=price;
}
//toString() return the name,type and price of dinuts
public String toString()
{
return “Name : “+name+”, Type : “+type+” , Price : “+price;
}
//check two donut objects are equal or not
public boolean equals(Donut other)
{
if(name.equalsIgnoreCase(other.name) && type.equalsIgnoreCase(other.type))
return true;
else
return false;
}
}
//class DonutBox
class DonutBox
{
private Donut donut[]=new Donut[12];
private int count;
//cpnstructor to set count
public DonutBox()
{
count=0;
}
//Getter fpr count
public int getCount()
{
return count;
}
//this method add a donut into DonutBox
public void addDonut(Donut order)
{
donut[count]=new Donut(order.getName(),order.getType());
count++;
}
//Getter for price of all the donuts in the DonutBox
public double getPrice()
{
double totalPrice=0.00;
double discount=0.00;
for(int i=0;i<count;i++)
{
totalPrice=totalPrice+ donut[i].getPrice();
}
//discount computaion
if(count<6)
discount=0.00;
else if(count>=6 && count<12)
discount=totalPrice*.01;
else if(count==12)
discount=totalPrice*.02;
double netPrice=totalPrice-discount;
return netPrice;
}
//toString() return the name,type and price of all the donuts in the box
public String toString()
{
String s=””;
for(int i=0;i<count;i++)
{
s+= “nName : “+donut[i].getName()+”, Type : “+donut[i].getType()+” , Price : “+donut[i].getPrice();
}
return s;
}
//returns the size of the box
public String size()
{
String boxSize=””;
if(count==1)
boxSize=”Small”;
else if(count>=1 && count<6)
boxSize=”Medium”;
else if(count>6 && count<=12)
boxSize=”Large”;
return boxSize;
}
}
public class DonutBoxDriver
{
public static void main(String arg[])
{
//cretae box1 , that contains 1 donut
DonutBox box1=new DonutBox();
box1.addDonut(new Donut(“Chocolate”,”Filled”));
System.out.println(“nBox1:”);
System.out.println(box1);
System.out.printf(“Price of box1 :%.2fn”,box1.getPrice());
System.out.println(“Size of box1 :”+ box1.size());
System.out.println(“n”);
//cretae box2, that contains 5 donut
DonutBox box2=new DonutBox();
box2.addDonut(new Donut(“Vanila”,”Filled”));
box2.addDonut(new Donut(“Strawbwrry”,”Glazed”));
box2.addDonut(new Donut(“Choco”,”Plain”));
box2.addDonut(new Donut(“Pineapple”,”Filled”));
box2.addDonut(new Donut(“Orange”,”Glazed”));
System.out.println(“nBox2:”);
System.out.println(box2);
System.out.printf(“Price of box2 :%.2fn”, box2.getPrice());
System.out.println(“Size of box2 :”+ box2.size());
System.out.println(“n”);
//cretae box1 , that contains 8 donut
DonutBox box3=new DonutBox();
box3.addDonut(new Donut(“Mango”,”Plain”));
box3.addDonut(new Donut(“Apple”,”Glazed”));
box3.addDonut(new Donut(“Choco”,”filled”));
box3.addDonut(new Donut(“Vanila”,”Filled”));
box3.addDonut(new Donut(“Papaya”,”Glazed”));
box3.addDonut(new Donut(“Gluco”,”Plain”));
box3.addDonut(new Donut(“Mango”,”Filled”));
box3.addDonut(new Donut(“Papaya”,”Plain”));
System.out.println(“nBox3:”);
System.out.println(box3);
System.out.printf(“Price of box3 :%.2fn”, box3.getPrice());
System.out.println(“Size of box3 :”+ box3.size());
}
}
//———————————————————————–