boolean help!

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2009
Posts: 25
Reputation: peedi is an unknown quantity at this point 
Solved Threads: 0
peedi peedi is offline Offline
Light Poster

boolean help!

 
0
  #1
Mar 23rd, 2009
I am almost done with my program. And for some reason i cannot get my boolean to become true.

here is the code

//This program allows the user to order a pizza

import java.util.Scanner;
import java.text.*;

public class PizzaOrder
{
	public static void main(String [] args)
	{



		//create a Scanner object to readinput
		Scanner keyboard = new Scanner (System.in);

		//creat an instance of a Pizza
		Pizza order = new Pizza ();

		String firstName;				//user's first name
		boolean discount = false;		//flag, true if user is eligible for discount
		int inches;						//size of the pizza
		char crustType;					//type of crust
		double cost;					//cost of the pizza
		final double TAX_RATE = .08;	//sales tax rate
		double tax;						//amount of tax
		char choice;					//user's choice
		String input;					//user's input
		String toppings= "Your pizza will have Cheese";	 	//list of toppings
		int numberOfToppings = 0;		//number of toppings

		//prompt user and get first name
		System.out.println("Welcome to Jack and Diane's Pizza");
		System.out.print("Enter your first name:");
		firstName = keyboard.nextLine();

		//determine if user is eligible for discount by having the same first name as one of the owners
		//TASK #1
		if (firstName == ("Jack") || firstName== ("jack"))
		{
		discount = true;
		}
		if (firstName == ("Diane") || firstName == ("diane"))
		{
		discount = true;
		}

		//prompt user and get pizza size choice
		System.out.println("Pizza Size(inches) Cost");
		System.out.println("	10	$10.99");
		System.out.println("	12	$12.99");
		System.out.println("	14	$14.99");
		System.out.println("	16	$16.99");
		System.out.println("What size pizza would you like?");
		System.out.println("10, 12, 14, or 16 (enter the number only):");
		inches = keyboard.nextInt();

		//set price and size of pizza ordered
		//TASK #2


		if (inches == 10)
		{
        	order.setSize(10);
			order.setCost(10.99);
		                }
		else if (inches == 12)
		{
			order.setSize(12);
			order.setCost(12.99);
		}
		else if (inches == 14)
		{
		order.setSize(14);
		order.setCost(14.99);
		}
		else if (inches == 16)
		{
		order.setSize(16);
		order.setCost(16.99);
		}
		else
		{
		order.setSize(12);
		order.setCost(12.99);
		System.out.println("You have choosen a size other than the above"
						+ " sizes was select,we will make a 12 inche pizza for you.");
		}


		//consume the remaining newline character
		keyboard.nextLine();

		//prompt user and get crust choice
		System.out.println("What type of crust would you like?");
		System.out.println("(H)Hand-tossed, (T)Thin-crust, or " + "(D)Deep-dish (enter H, T, or D):");
		input = keyboard.nextLine();
		crustType = input.charAt(0);

		//set user's crust choice on pizza ordered
		//TASK #3

		switch (crustType)
		{
			case 'H':
			case 'h':
			order.setCrust("Hand-Tossed");
			break;

			case 'T':
			case 't':
			order.setCrust("Thin-Crust");
			break;

			case 'D':
			case 'd':
			order.setCrust("Deep-Dish");
			break;

			default:
			System.out.println("Other then the selections above, a Hand-tossed Pizza will be made.");
			order.setCrust("Hand-Tossed");
			break;
		}


		//prompt user and get topping choices one at a time
		System.out.println("All pizzas come with cheese.");
		System.out.println("Additional toppings are $1.25 each," + " choose from");
		System.out.println("Pepperoni, Sausage, Onion or 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";
		}

		//set number of toppings and topping list on pizza ordered
		order.setNumToppings (numberOfToppings);
		order.setToppingsList(toppings);

		//add additional toppings cost to cost of pizza
		order.setCost(1.25*numberOfToppings);

		//display order confirmation
		System.out.println();
		System.out.println("Your order is as follows:");
		System.out.println(order.getSize()+ " inch pizza");
		System.out.println(order.getCrust()+ " pizza crust");
		System.out.println(order.getToppingList() + " on it.");

		//display cost of pizza
		cost = order.getCost();

		//apply discount if user is eligible
		//TASK #4 HERE

		if (discount == true)
		{
		System.out.println("Since you have the same first name as one of our owners, you will recieve a $2.00 discount!");
		order.setCost(-2.00);
		cost = order.getCost();
        }


		//TASK #5
		DecimalFormat form = new DecimalFormat("#.##"); //creates 2 decimal places

		//SO ALL MONEY OUTPUT APPREARS WITH 2 DECIMAL PLACES
		System.out.println("The cost of your order is: $" + form.format(cost));

		//calc and display tax and total cost
		tax = cost * TAX_RATE;
		System.out.println("The tax is: $" + form.format(tax));
		System.out.println("The total due is: $" + form.format((tax+cost)));

		System.out.println("Your order will be ready" + " for pickup in 30 minutes." + "Have a nice day!");
		}
	}
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: boolean help!

 
1
  #2
Mar 23rd, 2009
The "==" operator won't evaluate two Strings as true unless they are the same object. You should use String.equals() instead.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,514
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: boolean help!

 
0
  #3
Mar 23rd, 2009
sillyboy is correct in what's causing the problem and, with your particular code above, String.equalsIgnoreCase() would save you having to check the multiple forms separately.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 25
Reputation: peedi is an unknown quantity at this point 
Solved Threads: 0
peedi peedi is offline Offline
Light Poster

Re: boolean help!

 
0
  #4
Mar 24th, 2009
so the first if statement should look like this

if (String.equalsIgnoreCase(firstName) == ("Jack") || String.equalsIgnoreCase(firstName) == ("jack"))
{
discount = true;
}
if (String.equalsIgnoreCase(firstName) == ("Diane") || String.equalsIgnoreCase(firstName) == ("diane"))
{
discount = true;
}
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: boolean help!

 
0
  #5
Mar 24th, 2009
equalsIgnoreCase... just read the method name and you should get a fair indication what it does. since it ignores the case, you don't need || . just note jAck, jACk, etc... will also evaluate to true, so if you want to exlude these, use equals instead.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 343 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC