Java: add a drink, compute their total price, search a drink, list drinks

Drink.java
DrinkInBox.java
DrinkInCylinder.java
DrinkParser.javaSkills to be applied:Inheritance
The protected modifier
The super Reference
Abstract class
NumberFormat
ArrayList

The main program Assignment2 is belowimport java.util.*; //to use ArrayList public class Assignment5 { public static void main (String[] args) { char input1; String inputInfo = new String(); String line = new String(); boolean found = false; // ArrayList object is used to store drink objects ArrayList drinkList = new ArrayList(); printMenu(); // print out menu // create a Scanner object to read from a keyboard Scanner scan = new Scanner(System.in); do { System.out.println("What action would you like to perform?"); line = scan.nextLine().trim(); input1 = line.charAt(0); input1 = Character.toUpperCase(input1); if (line.length() == 1) { switch (input1) { case 'A': //Add Drink System.out.print("Please enter a drink information to add:\n"); inputInfo = scan.nextLine().trim(); /*********************************************************************************** *** ADD your code here to create an object of one of child classes of Drink class *** and add it to the drinkList ***********************************************************************************/ break; case 'C': //Compute Total Prices /*********************************************************************************** *** ADD your code here to compute the total price for all drinks in the list. ***********************************************************************************/ System.out.print("total prices computed\n"); break; case 'D': //Search for Drink System.out.print("Please enter a drinkID to search:\n"); inputInfo = scan.nextLine().trim(); /*********************************************************************************** *** ADD your code here to search a given drinkID. If found, set "found" true, *** and set "found" false otherwise. ***********************************************************************************/ if (found == true) System.out.print("drink found\n"); else System.out.print("drink not found\n"); break; case 'L': //List Drinks /*********************************************************************************** *** ADD your code here to print out all drink objects in the list. *** If there is no drink in the list, print "no drink\n" ***********************************************************************************/ break; case 'Q': //Quit break; case '?': //Display Menu printMenu(); break; default: System.out.print("Unknown action\n"); break; } } else { System.out.print("Unknown action\n"); } } while (input1 != 'Q'); // stop the loop when Q is read } /** The method printMenu displays the menu to a use **/ public static void printMenu() { System.out.print("Choice\t\tAction\n" + "------\t\t------\n" + "A\t\tAdd Drink\n" + "C\t\tCompute Total Prices\n" + "D\t\tSearch for Drink\n" + "L\t\tList Drinks\n" + "Q\t\tQuit\n" + "?\t\tDisplay Help\n\n"); } }


Need to make use of inheritance by creating a class hierarchy for drinks to sell.Drink classDrink 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 nameAttribute typeDescriptionvolumeintThe volume of the drinkunitPricedoubleThe price per unit of the drinktotalPricedoubleThe total price of the drinkdrinkIdStringThe Id of the drinkThe 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\t10001\n
The Volume:\t\t150\n
The Unit Price:\t\t0.0015\n
The Total Price:\t$330.00\n\n
DrinkInCylinder classDrinkInCylinder 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 nameAttribute typeDescriptionradiusintThe radius of the cylinder of the drink.heightintThe 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 andtotalPrice 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\n
The Radius:\t\t5\n
The Height:\t\t10\n
The DrinkId:\t\tsona200\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.DrinkInBox classDrinkInBox is a child of Drink class. It represents a drink in a carton. It has the following attributes:Attribute nameAttribute typeDescriptionheightintThe heigt of the box of the drink.widthintThe width of the box of the drink.depthintThe 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\n
The Height:\t\t5\n
The Width:\t\t10\n
The Depth:\t\t5\n
The DrinkId:\t\tmilk515\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.DrinkParser classThe 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. TheDrinkParser 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/heightFor a box drink,shape/drinkId/unitPrice/height/width/depthA real example of this string would be:Cylinder/soda200/0.0054/5/10ORBox/milk03/0.0035/10/15/10This method will parse this string, pull out the information, create a newDrinkInCylinder 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 classIn 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\t\tAction\n
------\t\t------\n
A\t\tAdd Drink\n
C\t\tCompute Total Prices\n
D\t\tSearch for Drink\n
L\t\tList Drinks\n
Q\t\tQuit\n
?\t\tDisplay Help\n\nNext, the following prompt should be displayed:What action would you like to perform?\nRead 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 DrinkYour program should display the following prompt:Please enter a drink information to add:\nRead 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 PricesYour program should compute total price for all drinkes created so far by callingcomputeTotalPrice method for each of them in the drink list.After computing total prices, display the following:total prices computed\nSearch for DrinkYour program should display the following prompt:Please enter a drinkID to search:\nRead 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\nOtherwise, display this:drink not found\nList DrinksList all drinks in the drink list. Make use of toString method defined in DrinkInBoxand 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.25If there is no drink in the drink list (the list is empty), then display following:no drink\nQuitYour program should stop executing and output nothing.Display HelpYour program should redisplay the "choice action" menu.




Powered by