Assignment2 Drink.java DrinkInBox.java DrinkInCylinder.java DrinkParser.java Inh
ID: 3550007 • Letter: A
Question
Assignment2
Drink.java
DrinkInBox.java
DrinkInCylinder.java
DrinkParser.java
Inheritance
The protected modifier
The super Reference
Abstract class
NumberFormat
ArrayList
The main program Assignment2 is below
Need to make use of inheritance by creating a class hierarchy for drinks to sell.
Drink is an abstract class, which represents the basic attributes of any drink in a container to be sold. It is used as the root of the drink hierarchy. It has the following attributes (should be protected):
Attribute name
Attribute type
Description
volume
int
The volume of the drink
unitPrice
double
The price per unit of the drink
totalPrice
double
The total price of the drink
drinkId
String
The Id of the drink
The following constructor method should be provided to initialize the instance variables.
public Drink(String, double)
The instance variable volume is initialized to 0, totalPrice is initialized to 0.0, unitPrice is initialized to the value of the second parameter, and drinkId is initialized to the string value of the first parameter.
The following accessor method should be provided for drinkId :
public String getDrinkId()
The class Drink also has an abstract method (which should be implemented by its child classes, DrinkInCylinder and DrinkInBox) to compute the volume of the drink:
public abstract void computeTotalPrice();
The following public method should be provided:
public String toString()
toString method returns a string of the following format:
nThe DrinkId:t 10001
The Volume: 150
The Unit Price: 0.0015
The Total Price: $330.00
DrinkInCylinder is a child of Drink class. It represents a drink in a can (cylinder). It has the following attribute in addition to the inherited ones:
Attribute name
Attribute type
Description
radius
int
The radius of the cylinder of the drink.
height
int
The height of the cylinder of the drink.
The following constructor method should be provided:
public DrinkInCylinder(String, double, int, int)
The radius is initialized to the value of the third parameter, the height is initialized to the value of the forth parameter, and the constructor of the parent class Drink should be called using the first and second parameters. Leave volume and totalPrice as their default value.
The following method should be implemented:
public void computeTotalPrice()
First, it computes the volume for the cylinder drink. (computed by PI*(radius*radius*height), the constant value PI is defined in the Math class. -- (int) (Math.PI*(radius*radius*height)) Also, compute (radius*radius*height) first since they are all integers. PI is a float point number, so you need to cast the final value to an integer ("volume" is an integer.) Then compute the total price of the drink. (Computed by volume * unitPrice)
Also, the following method should be implemented:
public String toString()
The toString() method inherited from Drink class should be used to create a new string, and display a cylinder drink's information using the following format:
nThe Drink in a Cylinder
The Radius:t 5
The Height: 10
The DrinkId: sona200
The Volume: 785
The Unit Price: 0.0022
The Total Price: $1.73
This toString method should make use of the toString method of the parent class.
DrinkInBox is a child of Drink class. It represents a drink in a carton. It has the following attributes:
Attribute name
Attribute type
Description
height
int
The heigt of the box of the drink.
width
int
The width of the box of the drink.
depth
int
The depth of the box of the drink.
The following constructor method should be provided:
public DrinkInBox(String, double, int, int, int)
The height, width, depth are initialized to the value of the third parameter, the fourth parameter, and the fifth parameter, respectively, and the constructor of the parent class Drink should be called using the first and second parameters. Leave volume and totalPrice as their default value.
The following method should be implemented:
public void computeTotalPrice()
First, it computes the volume of the box of the drink. (computed by height*width*depth)
Then compute the total price of the drink. (computed by volume * unitPrice)
Also, the following method should be implemented:
public String toString()
The toString() method inherited from the Drink class should be used to create a new string, and display a box drink's information using the following format:
nThe Drink in a Box
The Height:t 5
The Width: 10
The Depth: 5
The DrinkId: milk515
The Volume: 250
The Unit Price: 0.0055
The Total Price: $1.38
This toString method should make use of the toString method of the parent class.
The DrinkParser class is a utility class that will be used to create a drink object (either a cylinder drink object or a box drink object) from a parsable string. The DrinkParser class object will never be instantiated. It must have the following method:
public static Drink parseStringToDrink(String lineToParse)
The parseStringToDrink method's argument will be a string in the following format:
For a cylinder drink,
shape/drinkId/unitPrice/radius/height
For a box drink,
shape/drinkId/unitPrice/height/width/depth
A real example of this string would be:
Cylinder/soda200/0.0054/5/10
OR
Box/milk03/0.0035/10/15/10
This method will parse this string, pull out the information, create a new DrinkInCylinder or DrinkInBox object using their constructor with attributes of the object, and return it to the calling method. The type will always be present and always be either Cylinder or Box. (It can be lower case or upper case) You may add other methods to the DrinkInCylinder and DrinkInBox class in order to make your life easier.
In this assignment, download Assignment5.java file by clicking the link, and use it for your assignment. You need to add codes to this file. The parts you need to add are written in the Assignment5.java file, namely for the four cases "Add Drink", "Add Compute Total Prices", "Search for Drink", and "List Drinks".
All input and output should be handled here. The main method should start by displaying this updated menu in this exact format:
Choice tAction
------ ------
A tAdd Drink
C tCompute Total Prices
D tSearch for Drink
L tList Drinks
Q tQuit
?ttDisplay Help
Next, the following prompt should be displayed:
What action would you like to perform?n
Read in the user input and execute the appropriate command. After the execution of each command, redisplay the prompt. Commands should be accepted in both lowercase and uppercase.
Your program should display the following prompt:
Please enter a drink information to add:n
Read in the information and parse it using the drink parser.
Then add the new drink object (created by drink parser) to the drink list.
Your program should compute total price for all drinkes created so far by calling computeTotalPrice method for each of them in the drink list.
After computing total prices, display the following:
total prices computed
Your program should display the following prompt:
Please enter a drinkID to search:n
Read in the string and look up the drink list, if there exists a drink object with the same drink ID, then display the following:
drink found
Otherwise, display this:
drink not found
List all drinks in the drink list. Make use of toString method defined in DrinkInBox and DrinkInCylinder classes.
A real example is looked like this:
The Drink in a Cylinder
The Radius: 5
The Height: 10
The DrinkId: soda200
The Volume: 785
The Unit Price: 0.0054
The Total Price: $4.24
The Drink in a Box
The Height: 10
The Width: 15
The Depth: 10
The DrinkId: milk03
The Volume: 1500
The Unit Price: 0.0035
The Total Price: $5.25
If there is no drink in the drink list (the list is empty), then display following:
no drink
Your program should stop executing and output nothing.
Your program should redisplay the "choice action" menu.
Attribute name
Attribute type
Description
volume
int
The volume of the drink
unitPrice
double
The price per unit of the drink
totalPrice
double
The total price of the drink
drinkId
String
The Id of the drink
Explanation / Answer
public abstract class Drink {
protected int voulme;
protected double unitPrice;
protected double totalPrice;
protected String drinkId;
public Drink(String drinkId, double unitPrice){
super();
this.voulme = 0;
this.drinkId = drinkId;
this.unitPrice = unitPrice;
this.totalPrice = 0.0;
}
public String getDrinkId() {
return drinkId;
}
public abstract void computeTotalPrice();
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("The DrinkId: ");
builder.append(drinkId);
builder.append(" The Voulme: ");
builder.append(voulme);
builder.append(" The UnitPrice: ");
builder.append(unitPrice);
builder.append(" The TotalPrice: $");
builder.append(totalPrice);
return builder.toString();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.