# Boolean problem

• March 7th, 2013, 08:14 PM
RepersGhost
Boolean problem
Ok working on another problem and everything works like it should except for the fact that my boolean discount isn't showing the output for some reason? I think I'm missing something really simple here, but just not seeing it yet. Any help or suggestions are always appreciated. I marked where the parts are with a <----.

Code Java:

```public class Pizza { private double cost;   //the cost of the pizza private String crust;   //the type of crust private int size;   //the diameter in inches private int numToppings;   //the number of toppings private String toppingList; //a list of the toppings   //constructor creates a 12" hand tossed pizza   public Pizza() { cost = 12.99; crust = "Hand-tossed"; size = 12; numToppings = 0; toppingList = null; } //adds the parameter amount to the cost public void setCost (double amount) { cost += amount; }   //sets the crust type public void setCrust (String type) { crust = type; }   //changes the size of the pizza to the parameter diameter public void setSize (int diameter) { size = diameter; }   //sets the number of toppings to the parameter number public void setNumToppings(int number) { numToppings = number; }   //sets the list of toppings public void setToppingList (String newTopping) { toppingList = newTopping; }   //returns the cost of the pizza public double getCost() { return cost; }   //returns the size of the pizza public int getSize() { return size; }   //returns the number of toppings public int getNumToppings() { return numToppings; }   //returns the list of the toppings public String getToppingList() { return toppingList; } }```

Code Java:

```import java.util.Scanner;   //TASK #5 add an import statement to use the DecimalFormat class import java.text.DecimalFormat;   public class PizzaOrder { private double cost; //cost of the pizza private String crust; //type of crust private String toppingList; //list of the toppings private int size; //diameter in inches private int numToppings = 0; //number of toppings   public PizzaOrder() //Constructor creates a 12" hand-tossed pizza { cost = 12.99; crust = "Hand-tossed"; size = 12; numToppings = 0; toppingList = null; } public void setCost (double amount) //adds the parameter amount to the cost { cost += amount; } public void setCrust (String type) //sets crust type { crust = type; } public void setSize (int diameter) //changes the size of the pizza to the parameter diameter { size = diameter; } public void setNumToppings (int number) //sets the number of toppings to the parameter number { numToppings = number; } public void setToppingList (String newTopping) //sets the list of toppings { toppingList = newTopping; } public double getCost() //returns the cost of the pizza { return cost; } public String getCrust() //returns crust type { return crust; } public int getSize() //returns the size of the pizza { return size; } public int getNumToppings() //returns the number of toppings { return numToppings; } public String getToppingList() //returns the list of toppings { return toppingList; }   public static void main (String [] args) { //TASK #5 Create a DecimalFormat object with 2 decimal places DecimalFormat formatter = new DecimalFormat("#0.00");   //create a Scanner object to read input Scanner keyboard = new Scanner (System.in);   //create an instance ofa pizza Pizza order = new Pizza();   String firstName; //user's first name boolean discount = false; //flag, true if user is eligible for discount   int inches; //sizes 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 input String toppings = "Cheese "; // list of toppings int numberOfToppings = 0; //number of toppings String maleOwner = "Mike"; //compare with user's first name for discount String femaleOwner = "Diane"; //compare with user's first name for discount       //prompt user and get first name System.out.println("Welcome to Mike and Diane's Pizza"); System.out.println("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 //add lines here for task 1 firstName = firstName.toUpperCase(); if ( (firstName.equals(maleOwner)) || (firstName.equals(femaleOwner)) ) { 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 //add lines her for task 2 if ( inches == 10 ) { order.setSize(inches); order.setCost(10.99); } else if ( inches == 12 ) { order.setSize(inches); order.setCost(12.99); } else if ( inches == 14 ) { order.setSize(inches); order.setCost(14.99); } else if ( inches == 16 ) { order.setSize(inches); order.setCost(16.99); } else { System.out.println("The input was not one of the choices so a 12 inch pizza will be made."); order.setSize(12); order.setCost(12.99); }   //consume the remaining newline character keyboard.nextLine();   //prompt user and get crust choice System.out.println("What type of crust do you want? "); 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 // add lines for 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"); default: System.out.println("The input was not one of the choices so a Hand-tossed crust will be made."); order.setCrust("Hand-tossed"); }   //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, Mushroom");   //if topping is desired //add to topping list and number of toppings System.out.println("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.println("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.println("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.println("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 toppings list on pizza ordered order.setNumToppings (numberOfToppings); order.setToppingList (toppings);   //add additional toppings cost to cost of pizza ordered 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("The type of crust is " + crustType); //("The type of crust is " + crustType) (order.getCrust() + " crust ") System.out.println(order.getToppingList());   //display cost of pizza cost = order.getCost();   //apply discount if user is elibible //add lines for task 4 here if (discount) { System.out.println("You are eligible for a \$2.00 discount."); //<--------------------------------------------- cost -= 2; } //edit program for task 5 //so all money output appears with 2 deciamal places System.out.println("The cost of the order is: \$" + formatter.format(cost));   //calculate and display tax and total cost tax = cost * TAX_RATE; System.out.println("The tax is: \$" +formatter.format(tax)); System.out.println("The total due is: \$" + formatter.format((tax + cost))); System.out.println("Your order will be ready" + " for pickup in 30 minutes."); } }```
• March 7th, 2013, 08:20 PM
Norm
Re: Boolean problem
Quote:

boolean discount isn't showing the output
Please explain. A boolean variable can have two values: true and false. I don't know how it would show any output.

Copy the full contents of the console window from when you execute the program and paste it here. Add some comments to the post describing what is wrong.
• March 7th, 2013, 08:26 PM
RepersGhost
Re: Boolean problem
What I mean is for this problem the boolean is checking the names and if the name is the same it is true and should give a discount and then do the following.

Code Java:

```if (discount) { System.out.println("You are eligible for a \$2.00 discount."); cost -= 2; }```

However it isn't showing up on the output screen. So the boolean isn't working for some reason.
• March 7th, 2013, 08:32 PM
Norm
Re: Boolean problem
Try debugging the code by adding some println statements to print out the values of the variables that control when the boolean is set true so you can see what the computer sees when it executes those statements.
Print out the values of the 3 variables on this line:
Code :

`if ( (firstName.equals(maleOwner)) || (firstName.equals(femaleOwner)) )`
• March 7th, 2013, 08:56 PM
RepersGhost
Re: Boolean problem
Ok well I did as you said and used the println to see what they were being seen as and I think the problem is with the

firstName = firstName.toUpperCase();

because I made the firstName Mike but its being seen as MIKE so that could be why it isn't doing the discount. So my question is how to I make the above only upper case the first letter instead of the whole word cause just putting a 0 in doesn't work?

Thanks for the tip by the way.
• March 7th, 2013, 09:02 PM
Norm
Re: Boolean problem
Quote:

how to I make the above only upper case the first letter
That would take several steps:
Split the String into two parts: first letter and the rest of the letters
Uppercase first letter
lowercase the rest
concatenate the two Strings
• March 7th, 2013, 09:14 PM
RepersGhost
Re: Boolean problem
Thanks for the quick replies. For the above I have no idea how to do that. If someone wants to show me how that would be done I would appreciate it otherwise I got it to work by just creating another lower cased name.
• March 8th, 2013, 06:05 AM
Norm
Re: Boolean problem
All the steps I listed would use methods from the String class plus the concatenation operator: +