To do: We will now write a driver using ArrayLists for our Drink class from #14
ID: 3741140 • Letter: T
Question
To do: We will now write a driver using ArrayLists for our Drink class from #14 above. Make a copy of theDrinkDriverArray and name it DrinkDriverArrayList. Convert this program from an array collection to an ArrayList. You do not need to make any changes in the Drink program (that one blueprint will work for both drivers or any type of collection). We want it to use an ArrayList instead of an array.
public class DrinkDriverArray{
public static void main(String[] args){
Drink[] DrinkDriverArray = new Drink[20];
Scanner sc = new Scanner(System.in);
int count = 0;
for (int i = 0; i<20; i++){
DrinkDriverArray[i] = new Drink();
System.out.println("Enter the type of drink");
DrinkDriverArray[i].setType(sc.nextLine());
System.out.println("What size?(S,M or L)");
DrinkDriverArray[i].setSize(sc.nextLine().charAt(0));
System.out.println("Enter the cost of each drink");
DrinkDriverArray[i].setCost(Double.parseDouble(sc.nextLine()));
System.out.println("Enter number of these drinks purchased");
DrinkDriverArray[i].setNumOrdered(Integer.parseInt(sc.nextLine()));
System.out.println("Enter another drink order? (yes or no)");
String ch = sc.nextLine();
count++;
if (ch.equals("no"))
break;
}
System.out.println("The orders are:");
double totalCostPurchased = 0;
for (int i = 0; i<count; i++){
String type = DrinkDriverArray[i].getType();
int n = DrinkDriverArray[i].getNumOrdered();
String size = "";
if (DrinkDriverArray[i].getSize() == 'L')
size = "Large";
if (DrinkDriverArray[i].getSize() == 'M')
size = "Medium";
if (DrinkDriverArray[i].getSize() == 'S')
size = "Small";
double cost = DrinkDriverArray[i].getCost();
double totalCost = DrinkDriverArray[i].getCost() * DrinkDriverArray[i].getNumOrdered();
totalCostPurchased = totalCostPurchased + totalCost;
System.out.printf("You ordered %d %s %s at $%.2f for each drink. The total cost was %.2f ",n,size,type,cost,totalCost);
}
System.out.printf("The cost of total order purchased for all orders is $%.2f ",totalCostPurchased);
}
}
Java Class GDrinkDriverArrayList itp 120mod5ANSWERS17_18 DrinkDriverArrayList0 Omain(Stringl)void printOrders(ArrayList sDrinkshvoid totalNum(ArrayListDrinks>)intExplanation / Answer
import java.util.Scanner;
import java.util.ArrayList;
public class DrinkDriverArray{
class Drink
{
}
public static void main(String[] args){
ArrayList<Drink> DrinkDriverArrayList = new ArrayList<Drink>();
Scanner sc = new Scanner(System.in);
int count = 0;
for (int i = 0; i<20; i++){
DrinkDriverArrayList.add(new Drink());
System.out.println("Enter the type of drink");
DrinkDriverArrayList.get(i).setType(sc.nextLine());
System.out.println("What size?(S,M or L)");
DrinkDriverArrayList.get(i).setSize(sc.nextLine().charAt(0));
System.out.println("Enter the cost of each drink");
DrinkDriverArrayList.get(i).setCost(Double.parseDouble(sc.nextLine()));
System.out.println("Enter number of these drinks purchased");
DrinkDriverArrayList.get(i).setNumOrdered(Integer.parseInt(sc.nextLine()));
System.out.println("Enter another drink order? (yes or no)");
String ch = sc.nextLine();
count++;
if (ch.equals("no"))
break;
}
System.out.println("The orders are:");
double totalCostPurchased = 0;
for (int i = 0; i<count; i++){
String type = DrinkDriverArrayList.get(i).getType();
int n = DrinkDriverArrayList.get(i).getNumOrdered();
String size = "";
if (DrinkDriverArrayList.get(i).getSize() == 'L')
size = "Large";
if (DrinkDriverArrayList.get(i).getSize() == 'M')
size = "Medium";
if (DrinkDriverArrayList.get(i).getSize() == 'S')
size = "Small";
double cost = DrinkDriverArrayList.get(i).getCost();
totalCostPurchased = totalCostPurchased + totalCost;
System.out.printf("You ordered %d %s %s at $%.2f for each drink. The total cost was %.2f ",n,size,type,cost,totalCost);
}
System.out.printf("The cost of total order purchased for all orders is $%.2f ",totalCostPurchased);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.