-3

I am a new java student. I am olderrrrr and for the life of me I've not be able to figure this out. We have not done arrays yet so that isn't part of the problem.

This program runs as it should with one exception. I need to implement a while loop to validate that the flavor and/or size choices.

import java.util.Scanner;

public class Cheesecake2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //variable declarations


        //Total cost  based on calculation of size * flavor
        double pricePerInch = 0.0;  //cost per inch 
        double cost = 0.0;          //cost calculation base on flavor and size selections
        double inches = 0.0;        //inches per size
        double totalCost = 0.0;     //accumulator to hold value of items selected


          Scanner scnr = new Scanner(System.in);
          String flavor = "";   //flavor choice entered by user
          String size = "";     //size choice entered by user
          String addAnother;  //Holds 'yes' or 'no'


          do {        
            System.out.println("Enter the flavor you'd like (plain, caramel, chocolate, raspberry, or strawberry:)"); 
            flavor = scnr.nextLine();

            //Calculate users total cost based on flavor and size choices
            if (flavor.equalsIgnoreCase("plain")) {             //plain cheesecake 
                pricePerInch = 0.50;                
            }
            else if (flavor.equalsIgnoreCase("caramel")) {  //caramel cheesecake                            
                pricePerInch = 0.75;
            }
            else if (flavor.equalsIgnoreCase("chocolate")) {    //chocolate cheesecake
                pricePerInch = 0.85;
            }
            else if (flavor.equalsIgnoreCase("raspberry")) {    //raspbery cheesecake
                pricePerInch = 1.15;
            }
            else if (flavor.equalsIgnoreCase("strawberry")) {   //strawberry cheesecake
                pricePerInch = 1.25;
            }
            else {
                System.out.println("That's not a valid flavor, please enter: plain, caramel, chocolate, raspberry, or strawberry.");
                  }

            while (flavor != ("plain") || flavor != ("caramel") ||flavor != ("chocolate") ||flavor != ("raspberry") ||flavor != ("strawberry")); {
                System.out.println("Please enter a valid flavor choice");
                flavor = scnr.nextLine();
            }

            //ask user for size input
             System.out.println("Enter the size you'd like (bite size, small, or large)");  
             size = scnr.nextLine();  

             //Prompt user for a size selection
             if (size.equalsIgnoreCase("bite size")) {      //bite size                     
                inches = 3;                     //size in inches
            }
             else if (size.equalsIgnoreCase("small")) {         //small                     
                 inches = 6;                        //size in inches
            }
             else if (size.equalsIgnoreCase("large")) {     //large
                  inches = 9;                       //size in inches
             }
             else {
                 System.out.println("I'm sorry " + size + " isn't one our size options. Please choose: bite size, small, or large.");
             }

             cost = pricePerInch * inches;  //calculate cheesecake cost base on flavor and size selections
             System.out.printf(flavor + " " + size + " cheesecake" + ": $%.2f\n",cost);

              totalCost += cost;            //add cheesecake cost to accumulator totalCost

              //Prompt user if they want to add to their order?
              System.out.println("Would you like to add another cheesecake to your order? Enter yes or no");
              addAnother = scnr.nextLine();

          } while (addAnother.equals("yes"));

          //Display total sales
          System.out.printf("The total cost for your order is" + ": $%.2f\n",totalCost);
}
}

But no matter where I put it or how I set it up, it breaks something else and it will only go through a single iteration.

Any advice would be appreciated!

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
trk1018
  • 1
  • 1

1 Answers1

-2

Modified it a bit:

public static void main( final String[] args )
{
    // `Scanner` should be closed... this will close it up in the end...
    try ( final Scanner scnr = new Scanner( System.in ) )
    {
        // variable declarations
        double totalCost = 0.0; // accumulator to hold value of items selected
        String flavor;          // flavor choice entered by user
        String size;            // size choice entered by user
        String addAnother;      // Holds 'yes' or 'no'

        // infinte loop with a label, will use `break label` to get out of it...
        addAnotherLoop: while ( true )
        {
            // Total cost based on calculation of size * flavor
            // moved here, because the initialization
            double pricePerInch = 0.0;  // cost per inch
            double inches = 0.0;        // inches per size
            double cost = 0.0;          // cost calculation base on flavor and size selections

            // ask user for flavour input
            System.out.println( "Enter the flavor you'd like:\n" +
                                "(plain, caramel, chocolate, raspberry, or strawberry)" );
            do
            {
                flavor = scnr.nextLine();
                if ( "plain".equalsIgnoreCase( flavor ) )
                {             // plain cheesecake
                    pricePerInch = 0.50;
                }
                else if ( "caramel".equalsIgnoreCase( flavor ) )
                {  // caramel cheesecake
                    pricePerInch = 0.75;
                }
                else if ( "chocolate".equalsIgnoreCase( flavor ) )
                {    // chocolate cheesecake
                    pricePerInch = 0.85;
                }
                else if ( "raspberry".equalsIgnoreCase( flavor ) )
                {    // raspbery cheesecake
                    pricePerInch = 1.15;
                }
                else if ( "strawberry".equalsIgnoreCase( flavor ) )
                {   // strawberry cheesecake
                    pricePerInch = 1.25;
                }
                else
                {
                    System.out.println( "That's not a valid flavor, please enter: plain," +
                                        "caramel, chocolate, raspberry, or strawberry." );
                }
            }
            // if no valid flavour was entered, `pricePerInch` is still 0...
            while ( 0.0 == pricePerInch );

            // ask user for size input
            System.out.println( "Enter the size you'd like:\n(bite size, small, or large)" );
            do
            {
                size = scnr.nextLine();

                // Prompt user for a size selection
                if ( "bite size".equalsIgnoreCase( size ) )
                {      // bite size
                    inches = 3;                     // size in inches
                }
                else if ( "small".equalsIgnoreCase( size ) )
                {         // small
                    inches = 6;                        // size in inches
                }
                else if ( "large".equalsIgnoreCase( size ) )
                {     // large
                    inches = 9;                       // size in inches
                }
                else
                {
                    System.out.printf( "I'm sorry %s isn't one our size options.\n" +
                                       "Please choose: bite size, small, or large.\n",
                                       size );
                }
            }
            // if no valid size was entered, `inches` is still 0...
            while ( 0.0 == inches );

            // calculate cheesecake cost base on flavor and size selections
            cost = pricePerInch * inches;
            System.out.printf( "%s %s cheesecake: $%.2f%n", flavor, size, cost );

            // add cheesecake cost to accumulator totalCost
            totalCost += cost;

            // Prompt user if they want to add to their order?
            System.out.println( "Would you like to add another cheesecake to your order?" );
            do
            {
                System.out.print( "Enter yes or no: " );
                addAnother = scnr.nextLine();
                // if no, exit the outer loop
                if ( "no".equalsIgnoreCase( addAnother ) )
                {
                    break addAnotherLoop;
                }
            }
            // if not yes, continue the loop
            while ( ! "yes".equals( addAnother ) );
        }

        // Display total sales
        System.out.printf( "The total cost for your order is: $%.2f\n", totalCost );
    }
}
Usagi Miyamoto
  • 6,196
  • 1
  • 19
  • 33