The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Java if-else statements pizza order
Discuss Java if-else statements pizza order in the Java Help forum on Dev Shed. Java if-else statements pizza order Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 10th, 2013, 11:50 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 18
Time spent in forums: 1 h 35 m 30 sec
Reputation Power: 0
|
|
|
Java if-else statements pizza order
Hello all, I have a problem with my pizza order code. I have two problems rights now, one problem is that the final output price is like $100.00+. Also I want the toppings of the pizza to be displayed IF the user has input "y" or "Y". However it outputs all the toppings out, even if they did not select that topping.
Here is my code:
import java.util.Scanner;
public class pizza
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
// Variables
String firstName; // first name of user
char crustType; // code for type
String crust; // name of crust
int inches; // pizza size
double cost = 0.0; // pizza cost
final double taxRate = 0.08; // amount tax owed
double tax; // tax amount
boolean discount; // $2.00 discount
double total; // total of pizz + toppings
double subTotal;
double lastTotal; // total of everything
int numberOfToppings = 0;
String toppings = "Cheese";
// Prompts for name & determines discount
System.out.println("Enter your first name? " );
firstName = keyboard.nextLine();
if (firstName.equalsIgnoreCase("Mike")
|| firstName.equalsIgnoreCase("Diane")
|| firstName.equalsIgnoreCase("Michael")
|| firstName.equalsIgnoreCase("Diane") )
discount = true;
else
discount = false;
// Prompts user for pizza size
System.out.print("What size of pizza would you like? (10, 12, 14, or 16) " );
inches = keyboard.nextInt();
if (inches == 10 )
cost = 10.99;
else if (inches == 12)
cost = 12.99;
else if (inches == 14)
cost = 14.99;
else if (inches == 16)
cost = 16.99;
else if (inches != 10 && inches != 12 && inches != 14 && inches != 16)
System.out.println("The number you have entered is illegal, your pizza size will be set to 12 inches. " );
cost = 12;
keyboard.nextLine();
// Prompts user for type of crust
System.out.print("What type of crust do you want? (H)and-Tossed, (T)hin-crust, or (D)eep-dish (enter H, T, or D,): " );
crustType = keyboard.nextLine().charAt(0);
if (crustType == 'H' || crustType == 'h' )
crust = "Hand-Tossed";
else if (crustType == 'T' || crustType == 't' )
crust = "Thin-Crust";
else if (crustType == 'D' || crustType == 'd' )
crust = "Deep-Dish";
else if (crustType != 'H' && crustType != 'h' && crustType != 'T' && crustType != 't' && crustType != 'D' && crustType != 'd' )
System.out.println("The crust type you have entered is illegal, your crust type will be set to hand-tossed. " );
crust = "Hand-Tossed";
// Prompts user for additonal toppings
System.out.println("All pizzas come with cheese." );
System.out.println("Additional toppings are $1.25 each, choose from Pepperoni, Sausage, Onion, or Mushroom." );
// Pepperoni
System.out.println("Do you want Pepperoni? (Y/N)" );
numberOfToppings = keyboard.nextLine().charAt(0);
if (numberOfToppings == 'Y' || numberOfToppings == 'y' )
numberOfToppings = numberOfToppings + 1;
toppings = toppings + " Pepperoni";
// Sausage
System.out.println("Do you want Sausage? (Y/N)" );
numberOfToppings = keyboard.nextLine().charAt(0);
if (numberOfToppings == 'Y' || numberOfToppings == 'y' )
numberOfToppings = numberOfToppings + 1;
toppings = toppings + " Sausage";
// Onion
System.out.println("Do you want Onion? (Y/N)" );
numberOfToppings = keyboard.nextLine().charAt(0);
if (numberOfToppings == 'Y' || numberOfToppings == 'y' )
numberOfToppings = numberOfToppings + 1;
toppings = toppings + " Onion";
// Mushroom
System.out.println("Do you want Mushroom? (Y/N)" );
numberOfToppings = keyboard.nextLine().charAt(0);
if (numberOfToppings == 'Y' || numberOfToppings == 'y' )
numberOfToppings = numberOfToppings + 1;
toppings = toppings + " Mushroom";
// Calculations
total = (cost) + (numberOfToppings * 1.25);
if (discount == true )
total = total - 2;
else
total = total;
tax = total * taxRate;
lastTotal = total * ( 1 + taxRate );
// Payment Confirmation
System.out.println( firstName + ", your order is as follows:" );
System.out.println( inches + " inch pizza" );
System.out.println( crust );
System.out.println( toppings );
if ( discount == true )
System.out.println("You are eligible for a $2.00 discount" );
else
System.out.println("You are not eligible for a $2.00 discount" );
System.out.println("The cost of your order is: $" + total );
System.out.println("The tax is: $" + tax );
System.out.println("The total due is: $" + lastTotal );
System.out.println("Your order will be ready for pickup in 30 minutes." );
}
}
|

March 11th, 2013, 12:31 AM
|
|
Contributing User
|
|
Join Date: Oct 2007
Posts: 80
Time spent in forums: 1 Day 19 h 41 m 39 sec
Reputation Power: 6
|
|
Code:
// Prompts user for additonal toppings
System.out.println("All pizzas come with cheese." );
System.out.println("Additional toppings are $1.25 each, choose from Pepperoni, Sausage, Onion, or Mushroom." );
// Pepperoni
System.out.println("Do you want Pepperoni? (Y/N)" );
numberOfToppings = keyboard.nextLine().charAt(0);
if (numberOfToppings == 'Y' || numberOfToppings == 'y' )
numberOfToppings = numberOfToppings + 1;
toppings = toppings + " Pepperoni";
// Sausage
System.out.println("Do you want Sausage? (Y/N)" );
numberOfToppings = keyboard.nextLine().charAt(0);
if (numberOfToppings == 'Y' || numberOfToppings == 'y' )
numberOfToppings = numberOfToppings + 1;
toppings = toppings + " Sausage";
// Onion
System.out.println("Do you want Onion? (Y/N)" );
numberOfToppings = keyboard.nextLine().charAt(0);
if (numberOfToppings == 'Y' || numberOfToppings == 'y' )
numberOfToppings = numberOfToppings + 1;
toppings = toppings + " Onion";
// Mushroom
System.out.println("Do you want Mushroom? (Y/N)" );
numberOfToppings = keyboard.nextLine().charAt(0);
if (numberOfToppings == 'Y' || numberOfToppings == 'y' )
numberOfToppings = numberOfToppings + 1;
toppings = toppings + " Mushroom";
So basically, this is where the problem is. What is happening is that you're not using { } brackets on your if statements. Normally, that is ok, but only if the following statement is one line. If you're using a multi-line if-statement, you need to enclose it in the { }. Right now, the way you have it, the user can input their choice, and it'll increment the numberOfToppings, but then, regardless of their input, it'll add that ingredient to toppings.
Enclose those if statements in those brackets and it'll solve your problem.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|