Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: if else statement: I need to place a price in a variable: to later ad taxes and topping to final cost.

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default if else statement: I need to place a price in a variable: to later ad taxes and topping to final cost.

    package lab_3;

    /**
    This program allows the user to order a pizza
    */
    import java.util.Scanner;
    //import java.text.DecimalFormat;

    public class PizzaOrder
    {
    public static void main (String [] args)
    {
    //TASK #5 Create a DecimalFormat object with 2 decimal places

    //Create a Scanner object to read input
    Scanner keyboard = new Scanner (System.in);

    String firstName; //user's first name
    boolean discount = false; //flag, true if user is eligible for discount
    double inches; //size of the pizza
    char crustType; //code for type of crust
    //No need for this line String crust = "Hand-tossed"; //name of crust***************
    double cost; //12.99; //cost of the pizza***************
    double toppingCost;
    final double TAX_RATE = .08; //sales tax rate
    double tax; //amount of tax
    char choice; //user's choice
    String input; //user input
    String toppings = ("With these Toppings\n"); //list of toppings
    double numberOfToppings = 0; //number of toppings

    //prompt user and get first name
    System.out.println("Welcome to Mike and Diane's Pizza\n");
    System.out.println("Enter your first name: ");

    firstName = keyboard.nextLine();
    System.out.println("Hello "+" "+ firstName+" \n");
    //determine if user is eligible for discount by
    //having the same first name as one of the owners
    //ADD LINES HERE FOR TASK #1

    //prompt user and get pizza size choice
    System.out.println("Pizza Size (inches) Cost");
    System.out.println("We have a varity of Pizzia sizes to choice from");
    System.out.println(" 10 $10.99");
    System.out.println(" 12 $12.99");
    System.out.println(" 14 $14.99");
    System.out.println(" 16 $16.99\n");
    System.out.println("What size pizza would you like?");
    System.out.print("10, 12, 14, or 16 (enter the number only:\n");
    inches = keyboard.nextInt();


    //set price and size of pizza ordered
    if (inches==10)
    System.out.println("Cost 10.99\n");
    else if (inches==12)
    System.out.println("Cost 12.99\n");
    else if (inches==14)
    System.out.println("Cost 14.99\n");
    else if (inches==16)
    System.out.println("Cost 16.99\n");

    keyboard.nextLine();

    //ADD LINES HERE FOR TASK #2




    //prompt user and get crust choice
    System.out.println("What type of crust do you want?");
    System.out.print("(H)Hand-tossed, (T) Thin-crust, or " +
    "(D) Deep-dish (enter H, T, or D): \n");
    input = keyboard.nextLine();
    crustType = Character.toUpperCase(input.charAt(0)); // I added this line. if user enters lower case******
    // it is automaticly converted to uppercase***********

    //I used the else if statement to set user's crust choice on pizza ordered****************

    if (crustType=='H')
    System.out.println("Hand-tossed\n");
    else if(crustType=='T')
    System.out.println("Thin Crust\n");
    else if(crustType=='D')
    System.out.println("Deep Pan\n");
    else
    crustType=keyboard.next();

    System.out.println("Invalid Selection\n");




    //ADD LINES FOR TASK #3

    //prompt user and get topping choices one at a time

    System.out.println("All pizzas come with cheese.\n");
    System.out.println("Additional toppings are $1.25 each choose from");
    System.out.println("Pepperoni, Sausage, Onion, Mushroom");

    //if topping is desired,
    //add to topping list and number of toppings
    System.out.print("Do you want Pepperoni? (Y/N): ");
    input = keyboard.nextLine();
    choice = input.charAt(0);
    if (choice == 'Y' || choice == 'y')
    {
    numberOfToppings += 1;
    toppings = toppings + "Pepperoni ";
    }
    System.out.print("Do you want Sausage? (Y/N): ");
    input = keyboard.nextLine();
    choice = input.charAt(0);
    if (choice == 'Y' || choice == 'y')
    {
    numberOfToppings += 1;
    toppings = toppings + "Sausage ";
    }
    System.out.print("Do you want Onion? (Y/N): ");
    input = keyboard.nextLine();
    choice = input.charAt(0);
    if (choice == 'Y' || choice == 'y')
    {
    numberOfToppings += 1;
    toppings = toppings + "Onion ";
    }
    System.out.print("Do you want Mushroom? (Y/N): ");
    input = keyboard.nextLine();
    choice = input.charAt(0);
    if (choice == 'Y' || choice == 'y')
    {
    numberOfToppings += 1;
    toppings = toppings + "Mushroom\n";
    }
    //add additional toppings cost to cost of pizza
    cost = (1.25*numberOfToppings);
    System.out.println(numberOfToppings);
    System.out.println();
    System.out.println();

    //display order confirmation
    System.out.println();
    System.out.println("Your order is as follows:\n ");
    System.out.println(inches + " inch pizza\n");
    System.out.println(crustType + " crust\n"); //changed
    System.out.println(toppings);

    //apply discount if user is elibible
    //ADD LINES FOR TASK #4 HERE



    //calculate and display tax and total cost


    toppingCost= 1.25*numberOfToppings;
    System.out.println("Topping cost "+ toppingCost);

    tax = cost * TAX_RATE;
    System.out.println("The tax is: $" + tax);
    System.out.println("The total due is: $" + (tax+cost));

    //EDIT PROGRAM FOR TASK #5
    //SO ALL MONEY OUTPUT APPEARS WITH 2 DECIMAL PLACES
    cost=(inches+toppingCost+tax);
    System.out.println("The cost of your order is: $" + cost);

    // Order completed
    System.out.println("Your order will be ready for pickup in 30 minutes.");
    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: if else statement: I need to place a price in a variable: to later ad taxes and topping to final cost.

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

  3. #3
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: if else statement: I need to place a price in a variable: to later ad taxes and topping to final cost.

    Your code should look like this and please ask a direct question:

    package lab_3;
     
    /** 
    This program allows the user to order a pizza 
    */ 
    import java.util.Scanner; 
    //import java.text.DecimalFormat; 
     
    public class PizzaOrder 
    { 
    public static void main (String [] args) 
    { 
    //TASK #5 Create a DecimalFormat object with 2 decimal places 
     
    //Create a Scanner object to read input 
    Scanner keyboard = new Scanner (System.in); 
     
    String firstName; //user's first name 
    boolean discount = false; //flag, true if user is eligible for discount 
    double inches; //size of the pizza 
    char crustType; //code for type of crust 
    //No need for this line String crust = "Hand-tossed"; //name of crust*************** 
    double cost; //12.99; //cost of the pizza*************** 
    double toppingCost;
    final double TAX_RATE = .08; //sales tax rate 
    double tax; //amount of tax 
    char choice; //user's choice 
    String input; //user input 
    String toppings = ("With these Toppings\n"); //list of toppings 
    double numberOfToppings = 0; //number of toppings 
     
    //prompt user and get first name 
    System.out.println("Welcome to Mike and Diane's Pizza\n"); 
    System.out.println("Enter your first name: "); 
     
    firstName = keyboard.nextLine(); 
    System.out.println("Hello "+" "+ firstName+" \n");
    //determine if user is eligible for discount by 
    //having the same first name as one of the owners 
    //ADD LINES HERE FOR TASK #1 
     
    //prompt user and get pizza size choice 
    System.out.println("Pizza Size (inches) Cost");
    System.out.println("We have a varity of Pizzia sizes to choice from");
    System.out.println(" 10 $10.99"); 
    System.out.println(" 12 $12.99"); 
    System.out.println(" 14 $14.99"); 
    System.out.println(" 16 $16.99\n"); 
    System.out.println("What size pizza would you like?"); 
    System.out.print("10, 12, 14, or 16 (enter the number only:\n"); 
    inches = keyboard.nextInt();
     
     
    //set price and size of pizza ordered 
    if (inches==10)
    System.out.println("Cost 10.99\n");
    else if (inches==12)
    System.out.println("Cost 12.99\n");
    else if (inches==14)
    System.out.println("Cost 14.99\n");
    else if (inches==16)
    System.out.println("Cost 16.99\n");
     
    keyboard.nextLine();
     
    //ADD LINES HERE FOR TASK #2 
     
     
     
     
    //prompt user and get crust choice 
    System.out.println("What type of crust do you want?"); 
    System.out.print("(H)Hand-tossed, (T) Thin-crust, or " + 
    "(D) Deep-dish (enter H, T, or D): \n"); 
    input = keyboard.nextLine(); 
    crustType = Character.toUpperCase(input.charAt(0)); // I added this line. if user enters lower case******
    // it is automaticly converted to uppercase***********
     
    //I used the else if statement to set user's crust choice on pizza ordered****************
     
    if (crustType=='H')
    System.out.println("Hand-tossed\n");
    else if(crustType=='T')
    System.out.println("Thin Crust\n");
    else if(crustType=='D')
    System.out.println("Deep Pan\n");
    else 
    crustType=keyboard.next();
     
    System.out.println("Invalid Selection\n");
     
     
     
     
    //ADD LINES FOR TASK #3 
     
    //prompt user and get topping choices one at a time 
     
    System.out.println("All pizzas come with cheese.\n"); 
    System.out.println("Additional toppings are $1.25 each choose from"); 
    System.out.println("Pepperoni, Sausage, Onion, Mushroom"); 
     
    //if topping is desired, 
    //add to topping list and number of toppings 
    System.out.print("Do you want Pepperoni? (Y/N): "); 
    input = keyboard.nextLine(); 
    choice = input.charAt(0); 
    if (choice == 'Y' || choice == 'y') 
    { 
    numberOfToppings += 1; 
    toppings = toppings + "Pepperoni "; 
    } 
    System.out.print("Do you want Sausage? (Y/N): "); 
    input = keyboard.nextLine(); 
    choice = input.charAt(0); 
    if (choice == 'Y' || choice == 'y') 
    { 
    numberOfToppings += 1; 
    toppings = toppings + "Sausage "; 
    } 
    System.out.print("Do you want Onion? (Y/N): "); 
    input = keyboard.nextLine(); 
    choice = input.charAt(0); 
    if (choice == 'Y' || choice == 'y') 
    { 
    numberOfToppings += 1; 
    toppings = toppings + "Onion "; 
    } 
    System.out.print("Do you want Mushroom? (Y/N): "); 
    input = keyboard.nextLine(); 
    choice = input.charAt(0); 
    if (choice == 'Y' || choice == 'y') 
    { 
    numberOfToppings += 1; 
    toppings = toppings + "Mushroom\n"; 
    } 
    //add additional toppings cost to cost of pizza 
    cost = (1.25*numberOfToppings); 
    System.out.println(numberOfToppings);
    System.out.println();
    System.out.println();
     
    //display order confirmation 
    System.out.println(); 
    System.out.println("Your order is as follows:\n "); 
    System.out.println(inches + " inch pizza\n"); 
    System.out.println(crustType + " crust\n"); //changed
    System.out.println(toppings); 
     
    //apply discount if user is elibible 
    //ADD LINES FOR TASK #4 HERE 
     
     
     
    //calculate and display tax and total cost 
     
     
    toppingCost= 1.25*numberOfToppings;
    System.out.println("Topping cost "+ toppingCost);
     
    tax = cost * TAX_RATE; 
    System.out.println("The tax is: $" + tax); 
    System.out.println("The total due is: $" + (tax+cost)); 
     
    //EDIT PROGRAM FOR TASK #5 
    //SO ALL MONEY OUTPUT APPEARS WITH 2 DECIMAL PLACES 
    cost=(inches+toppingCost+tax);
    System.out.println("The cost of your order is: $" + cost); 
     
    // Order completed
    System.out.println("Your order will be ready for pickup in 30 minutes."); 
    } 
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: if else statement: I need to place a price in a variable: to later ad taxes and topping to final cost.

    Your code should look like this
    @ jocdrew21 Not quite. The code in post#3 is missing all its indentations.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: if else statement: I need to place a price in a variable: to later ad taxes and topping to final cost.

    Norm I just cute a pasted his code showing him what it should look like. Obviously once he does that from his JDK it will naturally do it for him. Correct?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: if else statement: I need to place a price in a variable: to later ad taxes and topping to final cost.

    Hopefully it will be formatted and indented.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Sep 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How to place the chosen pizza size in a integer variable.

    //set price and size of pizza ordered

    if (inches==10)
       System.out.println("Cost 10.99\n");
     
    else if (inches==12)
       System.out.println("Cost 12.99\n");
     
    else if (inches==14)
       System.out.println("Cost 14.99\n");
     
    else if (inches==16)
       System.out.println("Cost 16.99\n");
     
    keyboard.nextLine();

    I hope that my formatting is better? The value I get here will eventually be added to other totals in my program.

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: if else statement: I need to place a price in a variable: to later ad taxes and topping to final cost.

    Threads merged. Please do not start multiple threads on the same topic. Continue the discussion with improvements in your original thread.

  9. #9
    Junior Member
    Join Date
    Sep 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: if else statement: I need to place a price in a variable: to later ad taxes and topping to final cost.

    Hello GregBrannon, I'm new to this site and as you have probably deducted already, I'm new to programming. I appreciate your guidance on the proper way to conduct myself on this site.

    I hope my second post has a better formatted to it. I'm trying to get assistance with this java issue. Could you help me with this issue. I'm not sure if you are an administrator of this site or just one of the many programming experts. In either case I sure hope this site is able to help me.

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: if else statement: I need to place a price in a variable: to later ad taxes and topping to final cost.

    Yes, now what is your question? What Java issue do you need help with?

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: if else statement: I need to place a price in a variable: to later ad taxes and topping to final cost.

    One weakness I see for the code in post#7 is that the {}s that should surround the code inside of an if statement are missing.

    What problems are you having with the code? Can you explain in more detail and ask some specific questions?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. final variable!!!!
    By Arnab Kundu in forum Java Theory & Questions
    Replies: 0
    Last Post: July 19th, 2013, 10:07 AM
  2. topic related to static final variable and static block !!!!
    By Arnab Kundu in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 18th, 2013, 12:12 PM
  3. final class, final <variable> or <data member>
    By chronoz13 in forum Object Oriented Programming
    Replies: 9
    Last Post: September 20th, 2009, 08:19 AM