Home > Java > Assingment # 5

Assingment # 5

February 8th, 2010

CSE 205 – Assignment #5

Due Date: Friday, February 19th by 8:00PM

Minimal Submitted Files

You are required, but not limited, to turn in the following source files:

Assignment5.java (Download this file and use it as your driver program for this assignment. You need to add more code to complete it.)
Drink.java
DrinkInBox.java
DrinkInCylinder.java
DrinkParser.java

Requirements to get full credits in Documentation

The assignment number, your name, StudentID, Lecture time, and a class description need to be included at the top of each class/file.
A description of each method is also needed.
Some additional comments inside of methods to explain code that are hard to follow
Skills to be Applied

In addition to what has been covered in previous assignments, the use of the following items, discussed in class, will probably be needed:

Inheritance
The protected modifier
The super Reference
Abstract class
NumberFormat
ArrayList

Program Description

Class Diagram (download the .ppt file of the figure):

In Assignment #5, you will need to make use of inheritance by creating a class hierarchy for drinks to sell.

Drink class

Drink is an abstract class, which represents the basic attributes of any drink in a container. It is used as parent 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 to get the 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:tt10001n
The Volume:tt150n
The Unit Price:tt0.0015n
The Total Price:t$330.00nn

DrinkInCylinder class

DrinkInCylinder is a subclass 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 fourth 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 of the 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 Cylindern
The Radius:tt5n
The Height:tt10n
The DrinkId:ttsona200n
The Volume:tt785n
The Unit Price:tt0.0022n
The Total Price:t$1.73nn

This toString method should make use of the toString method of the parent class.

DrinkInBox class

DrinkInBox is a subclass 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 Boxn
The Height:tt5n
The Width:tt10n
The Depth:tt5n
The DrinkId:ttmilk515n
The Volume:tt250n
The Unit Price:tt0.0055n
The Total Price:t$1.38nn

This toString method should make use of the toString method of the parent class.

DrinkParser 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 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.

Assignment5 class

In this assignment, download Assignment5.java file by clicking the link, and use it for your assignment. You need to add code 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:

ChoicettActionn
——tt——n
AttAdd Drinkn
CttCompute Total Pricesn
DttSearch for Drinkn
LttList Drinksn
QttQuitn
?ttDisplay Helpnn

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.

Add Drink

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.

Compute Total Prices

Your program should compute total price for all drinks created so far by calling computeTotalPrice method for each of them in the drink list.

After computing total prices, display the following:

total prices computedn

Search for Drink

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 foundn

Otherwise, display this:

drink not foundn

List Drinks

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 drinkn

Quit

Your program should stop executing and output nothing.

Display Help

Your program should redisplay the “choice action” menu.

Invalid Command

If an invalid command is entered, display the following line:

Unknown actionn

Test cases:

Input

The following files are the test cases that will be used as input for your program (Right-click and use “Save As”):

Test Case #1
Test Case #2
Test Case #3
Test Case #4

Output

The following files are the expected outputs of the corresponding input files from the previous section (Right-click and use “Save As”):

Test Case #1
Test Case #2
Test Case #3
Test Case #4

Error Handling

Your program is expected to be robust to pass all test cases.


Assingment # 5

Categories: Java Tags: , , , , , ,
Comments are closed.
Bear