Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 10th, 2013, 11:50 PM
onion4ya onion4ya is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 18 onion4ya User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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." );
}
}

Reply With Quote
  #2  
Old March 11th, 2013, 12:31 AM
Kastro187420 Kastro187420 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2007
Posts: 80 Kastro187420 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Java if-else statements pizza order

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap