[Solved] SOLVED:add a drink, compute their total price, search a drink, list drinks

$25

File Name: SOLVED:add_a_drink,_compute_their_total_price,_search_a_drink,_list_drinks.zip
File Size: 697.08 KB

SKU: [Solved] SOLVED:add a drink, compute their total price, search a drink, list drinks Category: Tag:
5/5 - (1 vote)

Drink.javaDrinkInBox.javaDrinkInCylinder.javaDrinkParser.javaSkills to be applied:InheritanceThe protected modifierThe super ReferenceAbstract classNumberFormatArrayList

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:
); 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
); break; case D: //Search for Drink System.out.print(Please enter a drinkID to search:
); 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
); else System.out.print(drink not found
); 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
***********************************************************************************/ break; case Q: //Quit break; case ?: //Display Menu printMenu(); break; default: System.out.print(Unknown action
); break; } } else { System.out.print(Unknown action
); } } 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(ChoicettAction
+ tt
+ AttAdd Drink
+ CttCompute Total Prices
+ DttSearch for Drink
+ LttList Drinks
+ QttQuit
+ ?ttDisplay Help

); } }

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:
The DrinkId:tt10001
The Volume:tt150
The Unit Price:tt0.0015
The Total Price:t$330.00

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 drinks information using the following format:
The Drink in a Cylinder
The Radius:tt5
The Height:tt10
The DrinkId:ttsona200
The Volume:tt785
The Unit Price:tt0.0022
The Total Price:t$1.73

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 drinks information using the following format:
The Drink in a Box
The Height:tt5
The Width:tt10
The Depth:tt5
The DrinkId:ttmilk515
The Volume:tt250
The Unit Price:tt0.0055
The Total Price:t$1.38

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 methods 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:ChoicettAction
tt
AttAdd Drink
CttCompute Total Prices
DttSearch for Drink
LttList Drinks
QttQuit
?ttDisplay Help

Next, the following prompt should be displayed:What action would you like to perform?
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 DrinkYour program should display the following prompt:Please enter a drink information to add:
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 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
Search for DrinkYour program should display the following prompt:Please enter a drinkID to search:
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 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 CylinderThe Radius: 5The Height: 10The DrinkId: soda200The Volume: 785The Unit Price: 0.0054The Total Price: $4.24

The Drink in a BoxThe Height: 10The Width: 15The Depth: 10The DrinkId: milk03The Volume: 1500The Unit Price: 0.0035The Total Price: $5.25If there is no drink in the drink list (the list is empty), then display following:no drink
QuitYour program should stop executing and output nothing.Display HelpYour program should redisplay the choice action menu.Click BUY NOW BUTTON for solution

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] SOLVED:add a drink, compute their total price, search a drink, list drinks
$25