Question

In: Computer Science

I'm having trouble with my do while loop. I'm trying to get it where if the...

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

Solutions

Expert Solution

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;
    }
}


Related Solutions

I'm having trouble with my ZeroDenominatorException. How do I implement it to where if the denominator...
I'm having trouble with my ZeroDenominatorException. How do I implement it to where if the denominator is 0 it throws the ZeroDenominatorException and the catch catches to guarantee that the denominator is never 0. /** * The main class is the driver for my Rational project. */ public class Main { /** * Main method is the entry point to my code. * @param args The command line arguments. */ public static void main(String[] args) { int numerator, denominator =...
I'm having trouble trying to create the italian, polish, and netherlands flag in C Here's my...
I'm having trouble trying to create the italian, polish, and netherlands flag in C Here's my code so far: #include<stdio.h> int main(){    int country_choice;    fprintf(stderr, "Enter the country_code of the chosen flag.(1 for Poland, 2 for Netherlands, 3 for Italy)");    fscanf(stdin, "%d", &country_choice);    switch (country_choice) { case 1: printf("Poland Flag\n"); printf("%c%c%c", 255,255,255); printf("%c%c%c", 220,20,60);    break;       case 2: printf("Italian Flag\n"); printf("%c%c%c", 0,146,70); printf("%c%c%c", 225,255,255); printf("%c%c%c", 206,43,55); break;    case 3: printf("Netherlands Flag\n"); printf("%c%c%c", 174,28,40);...
I'm trying to get the union of two arrays and I tried making a loop that...
I'm trying to get the union of two arrays and I tried making a loop that does so. I tried also making the loop give me the size of the array as well from the union of the two arrays. Please check my loop to see what is wrong with it because it is not doing what I want it to do. I'm also not sure how to get the correct size of the array after the two arrays have...
I'm having trouble with validating this html code. Whenever I used my validator, it says I...
I'm having trouble with validating this html code. Whenever I used my validator, it says I have a stray end tag: (tbody) from line 122 to 123. It's the last few lines. Thanks and Ill thumbs up whoever can help solve my problem. Here's my code: <!DOCTYPE html> <html lang="en"> <head> <title>L7 Temperatures Fields</title> <!--    Name:    BlackBoard Username:    Filename: (items left blank for bb reasons    Class Section: (blank)    Purpose: Making a table to demonstrate my...
Hey! I'm having trouble answering this for my assignment. Thank you so much in advance. 1)...
Hey! I'm having trouble answering this for my assignment. Thank you so much in advance. 1) Which type of vessels, arteries or veins, has more muscle fibers? What is the functional significance of this? 2a) In general, we have no conscious control over smooth muscle or cardiac muscle function, whereas we can consciously control to some extent all skeletal muscles. Can you consciously control your breathing? What does this tell you about the muscle type of the diaphragm? 2b) What...
I'm having trouble understanding the following code (a snippet of a code). What does it do?...
I'm having trouble understanding the following code (a snippet of a code). What does it do? The whole code is about comparing efficiencies of different algorithms. def partition(list,first,last): piv = list[first] lmark = first+1 rmark = last done = False while not done: while lmark <= rmark and list[lmark]<=piv: lmark=lmark+1 while list[rmark]>=piv and rmark>=lmark: rmark=rmark-1 if rmark<lmark: done = True else: temp = list[lmark] list[lmark]=list[rmark] list[rmark]=temp temp = list[first] list[first]=list[rmark] list[rmark]=temp return rmark
I'm having trouble understanding smart pointer functions what is get(), release(), reset(), swap() for unique pointers?...
I'm having trouble understanding smart pointer functions what is get(), release(), reset(), swap() for unique pointers? How do I use it? what is get(), reset(), swap(), unique(), use_count() for shared pointers? How do I use it? what is expired(), lock(), reset(),swap(), use_count for weak pointer? How do I use it?
I'm having difficulty trying to make my code work. Create a subclass of Phone called SmartPhone....
I'm having difficulty trying to make my code work. Create a subclass of Phone called SmartPhone. Make this subclass have a no-arg constructor and a constructor that takes a name, email, and phone. Override the toString method to return the required output. Given Files: public class Demo1 { public static void test(Phone p) { System.out.println("Getter test:"); System.out.println(p.getName()); System.out.println(p.getNumber()); } public static void main(String[] args) { Phone test1 = new SmartPhone(); Phone test2 = new SmartPhone("Alice", "8059226966", "[email protected]"); System.out.println(test1); System.out.println(test2); System.out.println(test1);...
I'm having trouble understanding a CS assignment. I would appreciate it if you all code do...
I'm having trouble understanding a CS assignment. I would appreciate it if you all code do this for me. The template for the lab is below which you must use. You're only supposed to create/edit the product function. The assignment has to be written in asm(Mips) You will need to create a NOS table and use the structure below and write a function called product. The structure used in this program is: struct Values { short left; short right; int...
hello, I'm having trouble understanding how to do these two problems could you show me a...
hello, I'm having trouble understanding how to do these two problems could you show me a step by step. 1)Eight sprinters have made it to the Olympic finals in the 100-meter race. In how many different ways can the gold, silver, and bronze medals be awarded? 2)Suppose that 34% of people own dogs. If you pick two people at random, what is the probability that they both own a dog? Give your answer as a decimal (to at least 3...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT