943,682 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 688
  • Java RSS
Mar 23rd, 2009
0

boolean help!

Expand Post »
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!");
		}
	}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
peedi is offline Offline
25 posts
since Mar 2009
Mar 23rd, 2009
0

Re: boolean help!

The "==" operator won't evaluate two Strings as true unless they are the same object. You should use String.equals() instead.
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Mar 23rd, 2009
0

Re: boolean help!

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,757 posts
since May 2007
Mar 24th, 2009
0

Re: boolean help!

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;
}
Reputation Points: 10
Solved Threads: 0
Light Poster
peedi is offline Offline
25 posts
since Mar 2009
Mar 24th, 2009
0

Re: boolean help!

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.
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: 2 polynomials
Next Thread in Java Forum Timeline: Having all kinds of problems





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC