Assignment5.java Solution

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 codes to complete it.) 
Soup.java
SoupInBox.java
SoupInCylinder.java
SoupParser.java

Requirements to get full credits in Documentation

The assignment number, your name, StudentID, Lecture number/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 (especially for a "main" method) to explain code that are hard to follow should be written.
You can look at Java programs in the text book to see how comments are added to programs.

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/DecimalFormat 
Wrapper classes 
ArrayList

Program Description

Class Diagram:



 

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

Soup is an abstract class, which represents the basic attributes of any soup in a container to be sold. It is used as the root of the soup hierarchy. It has the following attributes (should be protected):

Attribute name
Attribute type
Description
volume
int
The volume of the soup
unitPrice
double
The price per unit of the soup
totalPrice
double
The total price of the soup
soupId
String
The Id of the soup
The following constructor method should be provided to initialize the instance variables.

public Soup(String id, double someUnitPrice)

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 soupId is initialized to the string value of the first parameter.

 

The following accessor method should be provided for soupId :

public String getSoupId()

The Class Soup also has an abstract method (which should be implemented by its child classes, SoupInCylinder and SoupInBox) to compute the volume of the soup:

public abstract void computeTotalPrice();

The following public method should be provided:

public String toString()

toString method returns a string of the following format:
 

\nThe SoupId:\t\ttomatosoup591\n 
The Volume:\t\t150\n
The Unit Price:\t\t0.0015\n 
The Total Price:\t$330.00\n\n
 

 You should make use of the NumberFormat class and DecimalFormat (in java.text package) to format the total price in the dollar format (NumberFormat) and the unit price using 4 digits after their decimal point (DecimalFormat using "0.0000").

 

SoupInCylinder class

SoupInCylinder is a subclass of Soup class. It represents a soup 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 soup.
height
int
The height of the cylinder of the soup.
The following constructor method should be provided:

public SoupInCylinder(String id, double someUnitPrice, int someRadius, int someHeight)

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 Soup should be called using the first and second parameters. Leave volume and totalPrice as their default values (defined in the parent’s constructor).

 

The following method should be implemented:

public void computeTotalPrice()

First, it computes the volume for the cylinder of the soup. (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 soup. (computed by volume * unitPrice)

Also, the following method should be implemented:

public String toString()

The toString() method inherited from Soup class should be used to create a new string, and display a cylinder soup's information using the following format:

\nThe Soup in a Cylinder Container\n
The Radius:\t\t5\n
The Height:\t\t10\n
The SoupId:\t\ttomatosoup591\n 
The Volume:\t\t785\n
The Unit Price:\t\t0.0022\n 
The Total Price:\t$1.73\n\n
 

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

 

SoupInBox class

SoupInBox is a subclass of Soup class. It represents a soup in a carton. It has the following attributes:

Attribute name
Attribute type
Description
height
int
The height of the box of the soup.
width
int
The width of the box of the soup.
depth
int
The depth of the box of the soup.
The following constructor method should be provided:

public SoupInBox(String id, double someUnitPrice, int someHeight, int someWidth, int someDepth)

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 Soup 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 soup. (computed by height*width*depth) 
Then compute the total price of the soup. (computed by volume * unitPrice)

Also, the following method should be implemented:

public String toString()

The toString() method inherited from the Soup class should be used to create a new string, and display a box soup's information using the following format:

\nThe Soup in a Box Container\n
The Height:\t\t5\n
The Width:\t\t10\n
The Depth:\t\t5\n
The SoupId:\t\tsplitPeaSoup515\n 
The Volume:\t\t250\n
The Unit Price:\t\t0.0055\n 
The Total Price:\t$1.38\n\n
 

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

 

SoupParser class

The SoupParser class is a utility class that will be used to create a soup object (either a cylinder soup object or a box soup object) from a parsable string. The SoupParser class object will never be instantiated. It must have the following method:

public static Soup parseStringToSoup(String lineToParse)

The parseStringToSoup method's argument will be a string in the following format:

For a cylinder soup,

shape/soupId/unitPrice/radius/height

For a box soup,

shape/soupId/unitPrice/height/width/depth

A real example of this string would be:

Cylinder/tomateSoup514/0.0054/5/10

OR

Box/splitPeaSoup7192/0.0035/10/15/10

This method will parse this string, pull out the information, create a new SoupInCylinder or SoupInBox 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 SoupInCylinder and SoupInBox 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 Soup", "Add Compute Total Prices", "Search for Soup", and "List Soups".

All input and output should be handled here. The main method should start by displaying this updated menu in this exact format:

Choice\t\tAction\n
------\t\t------\n
A\t\tAdd Soup\n
C\t\tCompute Total Prices\n
D\t\tSearch for Soup\n
L\t\tList Soups\n
Q\t\tQuit\n
?\t\tDisplay Help\n\n

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 Soup

Your program should display the following prompt:

Please enter a soup information to add:\n

Read in the information and parse it using the soup parser.

Then add the new soup object (created by soup parser) to the soup list.

Compute Total Prices

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

After computing total prices, display the following:

total prices computed\n

Search for Soup

Your program should display the following prompt:

Please enter a soupId to search:\n

Read in the string and look up the soup list, if there exists a soup object with the same soup ID, then display the following:

soup found\n

Otherwise, display this:

soup not found\n

List Soups

List all soups in the soup list. Make use of toString method defined in SoupInBox and SoupInCylinder classes.

A real example is looked like this:


The Soup in a Cylinder Container
The Radius: 5
The Height: 10
The SoupId: chickensoup200
The Volume: 0
The Unit Price: 0.0054
The Total Price: $0.00


The Soup in a Box Container
The Height: 10
The Width: 15
The Depth: 10
The SoupId: tomatosoup03
The Volume: 0
The Unit Price: 0.0035
The Total Price: $0.00

If there is no soup in the soup list (the list is empty), then display following:

no soup\n

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 action\n

Test cases:


You can download each file individually:



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

 
Powered by