Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

To do: We will now write a driver using ArrayLists for our Drink class from #14 above. Make a copy of the DrinkDriverArray and name

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.

image text in transcribed

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

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

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>)int

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions