In: Computer Science
Language: Java
Crust :
A small pizza costs $5.99, a medium is $7.99, and a large is $9.99.
A handtossed crust is an additional $0.50, and a deep dish pan crust is an additional $1.00.
Do a CrustSize enum [S(5.99), M(7.99), and L(9.99)] and a CrustType enum [THIN(0.00), HAND(0.50), and PAN(1.00)].
Place your enums in CrustEnum.java.
Both enums should have cost methods.
Do a Crust class with a constructor that accepts and sets the crust size and type, and provide a crustCost method.
Do a toString method to report the state of the crust.
Write getCrust (returns "THIN", "HAND", or "PAN") and getSize (returns a char 'S', 'M', or 'L').
I have most of it already, I just need help with the Crust class, aka the crustCost, toString, and getCrust.
There is a toString method, though I dont know if its right, as well as a crustCost.
If you need to delete something because it is wrong, feel free.
CrustEnum.java:
public enum CrustSize{
S(5.99),
M(7.99),
L(9.99);
private double cost;
private CrustSize(double cost){
this.cost = cost;
}
public double getCost(){
return this.cost;
}
public enum CrustType{
THIN(0.00),
HAND(0.50),
PAN(1.00);
private double cost;
CrustType(double cost){
this.cost = cost;
}
public double getCost(){
return this.cost;
}
}
private CrustSize size;
private CrustType type;
public CrustEnum(String size, String type){
this.size = CrustSize.valueOf(size);
this.type = CrustType.valueOf(type);
}
public double CrustCost(){
return (size.getCost() + type.getCost());
}
}
public class Crust {
private CrustEnum crust;
public Crust(String size, String type){
this.crust = new
CrustEnum(size,type);
}
public String toString(){
return "The crust is a " +
this.size + " " + this.type + " pizza";
public double crustCost(){
return crust.crustCost();
}
}
Code for the given question:
public class CrustEnum {
// Enum for crust size
public static enum CrustSize {
S(5.99), M(7.99), L(9.99);
// Attribute
private double cost;
/**
* Constructor
*
* @param size
*/
private CrustSize(double cost) {
this.cost = cost;
}
/**
* Gets the cost
*/
public double getCost() {
return this.cost;
}
}
// Enum for crust type
public static enum CrustType {
THIN(0.00), HAND(0.50), PAN(1.00);
// Attribute
private double cost;
/**
* Constructor
*
* @param size
*/
private CrustType(double cost) {
this.cost = cost;
}
/**
* Gets the cost
*/
public double getCost() {
return this.cost;
}
}
//Attibutes
private CrustSize size;
private CrustType type;
/**
* Constructor
* @param size
* @param type
*/
public CrustEnum(String size, String type) {
this.size = CrustSize.valueOf(size);
this.type = CrustType.valueOf(type);
}
/**
* Returns the crustcost
* @return
*/
public double crustCost() {
return (size.getCost() + type.getCost());
}
}
public class Crust {
//Attribute
private CrustEnum crust;
/**
* Constructor
* @param crustType
*/
public Crust(String size, String type) {
this.crust = new CrustEnum(size, type);
}
/**
* Returns the crustcost
* @return
*/
public double crustCost() {
return crust.crustCost();
}
}
We can create an object as
Crust crust = new Crust("M", "PAN");
So call crust.crustCost() method to get the total cost.