In: Computer Science
I'm having trouble with my do while loop. I'm trying to get it where if the user enter's 3 after whatever amount of caffeinated beverages they've entered before then the loop will close and the rest of my code would proceed to execute and calculate the average price of all the caffeinated beverages entered. Can someone please help me with this?
Here's my Code:
import java.util.Scanner; public class Main { public static void main(String[] args) { CaffeinatedBeverage[] inventory = new CaffeinatedBeverage[10]; int choice, ounces, roastType, count = 0; String name, flavor; double price; Scanner keyboard = new Scanner(System.in); do { System.out.println(); System.out.println("1) Enter new Coffee"); System.out.println("2) Enter new Energy Drink"); System.out.println("3) Exit"); choice = keyboard.nextInt(); if(choice == 1){ System.out.println(); System.out.println("Enter name : "); name = keyboard.nextLine(); keyboard.nextLine(); System.out.println("Enter the ounces : "); ounces = keyboard.nextInt(); System.out.println("Enter the price : $"); price = keyboard.nextDouble(); System.out.println("Enter the roast type: "); roastType = keyboard.nextInt(); inventory[count] = new Coffee(name, ounces, price, roastType); } else if(choice == 2){ System.out.println(); System.out.println("Enter name : "); name = keyboard.nextLine(); keyboard.nextLine(); System.out.println("Enter the ounces : "); ounces = keyboard.nextInt(); System.out.println("Enter the price : $"); price = keyboard.nextDouble(); System.out.println("Enter the flavor: "); flavor = keyboard.nextLine(); keyboard.nextLine(); inventory[count++] = new EnergyDrink(name, ounces, price, flavor); } else if(choice == 3){ System.exit(0); } } while (choice != 3); } public static double findAveragePrice(CaffeinatedBeverage[] inventory) { double totalPrice = 0.0; int count = 0; for(int i = 0; i < inventory.length; i++){ if(inventory[i] != null){ totalPrice += inventory[i].getPrice(); count++; } } return totalPrice / count; } public static EnergyDrink FindHighestPricedEnergyDrink(CaffeinatedBeverage[] inventory){ double maxPrice = 0.0; EnergyDrink max = null; for(int i = 0; i < inventory.length; i++){ if(inventory[i] != null && inventory[i] instanceof EnergyDrink){ EnergyDrink eDrink = (EnergyDrink) inventory[i]; if(eDrink.getPrice() > maxPrice){ maxPrice = eDrink.getPrice(); max = eDrink; } } } return max; } }
import java.util.Objects; public abstract class CaffeinatedBeverage { protected String mName; protected int mOunces; protected double mPrice; public CaffeinatedBeverage(String name, int ounces, double price) { mName = name; mOunces = ounces; mPrice = price; } public String getName() { return mName; } public void setName(String name) { mName = name; } public int getOunces() { return mOunces; } public void setOunces(int ounces) { mOunces = ounces; } public double getPrice() { return mPrice; } public void setPrice(double price) { mPrice = price; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; CaffeinatedBeverage that = (CaffeinatedBeverage) o; return mOunces == that.mOunces && Double.compare(that.mPrice, mPrice) == 0 && mName.equals(that.mName); } @Override public int hashCode() { return Objects.hash(mName, mOunces, mPrice); } }
import java.text.NumberFormat; import java.util.Objects; public class Coffee extends CaffeinatedBeverage { private int mRoastType; public Coffee(String name, int ounces, double price, int roastType) { super(name, ounces, price); mRoastType = roastType; } public int getRoastType() { return mRoastType; } public void setRoastType(int roastType) { mRoastType = roastType; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; if (!super.equals(o)) return false; Coffee coffee = (Coffee) o; return mRoastType == coffee.mRoastType; } @Override public int hashCode() { return Objects.hash(super.hashCode(), mRoastType); } @Override public String toString() { NumberFormat currency = NumberFormat.getCurrencyInstance(); String roast = ""; switch(mRoastType){ case 1: roast = "light roast"; break; case 2: roast = "medium roast"; break; case 3: roast = "dark roast"; break; } return "Coffee:" + mName + " , " + mOunces + " ounces, " + roast + " , " + currency.format(mPrice); } }
import java.text.NumberFormat; import java.util.Objects; public class EnergyDrink extends CaffeinatedBeverage { private String mflavor; public EnergyDrink(String name, int ounces, double price, String flavor) { super(name, ounces, price); mflavor = flavor; } public String getFlavor() { return mflavor; } public void setFlavor(String flavor) { this.mflavor = flavor; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; if (!super.equals(o)) return false; EnergyDrink that = (EnergyDrink) o; return mflavor.equals(that.mflavor); } @Override public int hashCode() { return Objects.hash(super.hashCode(), mflavor); } @Override public String toString() { NumberFormat currency = NumberFormat.getCurrencyInstance(); return "Energy Drink:" + mName + " , " + mOunces + " ounces, " + mflavor + " , " + currency.format(mPrice); } }
My Sample output is as follows:
1) Enter new Coffee
2) Enter new Energy Drink
3) Exit
1
Enter name :
Blonde
Enter the ounces :
16
Enter the price : $
6.50
Enter the roast type:
2
1) Enter new Coffee
2) Enter new Energy Drink
3) Exit
2
Enter name :
RedBull
Enter the ounces :
12
Enter the price : $
3.25
Enter the flavor:
citrus
1) Enter new Coffee
2) Enter new Energy Drink
3) Exit
3
Process finished with exit code 0
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
CaffeinatedBeverage[] inventory = new CaffeinatedBeverage[10];
int choice, ounces,
roastType, count = 0;
String name,
flavor;
double price;
Scanner keyboard =
new Scanner(System.in);
do {
System.out.println();
System.out.println("1) Enter new Coffee");
System.out.println("2) Enter new Energy Drink");
System.out.println("3) Exit");
choice = keyboard.nextInt();
if(choice == 1){
System.out.println();
System.out.println("Enter
name :
");
name = keyboard.nextLine();
keyboard.nextLine();
System.out.println("Enter the ounces : ");
ounces = keyboard.nextInt();
System.out.println("Enter the price :
$");
price = keyboard.nextDouble();
System.out.println("Enter the roast type: ");
roastType = keyboard.nextInt();
inventory[count++] = new Coffee(name, ounces, price,
roastType);
}
else if(choice == 2){
System.out.println();
System.out.println("Enter
name :
");
name = keyboard.nextLine();
keyboard.nextLine();
System.out.println("Enter the ounces : ");
ounces = keyboard.nextInt();
System.out.println("Enter the price :
$");
price = keyboard.nextDouble();
System.out.println("Enter the flavor: ");
flavor = keyboard.nextLine();
keyboard.nextLine();
inventory[count++] = new EnergyDrink(name, ounces, price,
flavor);
}
} while (choice !=
3);
if (count!=0)
System.out.println("Average price of all the drinks entered: $"+
findAveragePrice(inventory));
}
public static double findAveragePrice(CaffeinatedBeverage[] inventory) {
double totalPrice =
0.0;
int count = 0;
for(int i = 0; i < inventory.length; i++){
if(inventory[i] != null){
totalPrice += inventory[i].getPrice();
count++;
}
}
return totalPrice /
count;
}
public static EnergyDrink FindHighestPricedEnergyDrink(CaffeinatedBeverage[] inventory){
double maxPrice =
0.0;
EnergyDrink max =
null;
for(int i = 0; i < inventory.length; i++){
if(inventory[i] != null && inventory[i] instanceof EnergyDrink){
EnergyDrink eDrink = (EnergyDrink) inventory[i];
if(eDrink.getPrice() > maxPrice){
maxPrice = eDrink.getPrice();
max = eDrink;
}
}
}
return max;
}
}