Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Boolean problem

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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 <----.

    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;
    	}
    }

    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.");
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Boolean problem

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.

    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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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:
    if ( (firstName.equals(maleOwner)) || (firstName.equals(femaleOwner)) )
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    RepersGhost (March 7th, 2013)

  6. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Boolean problem

    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
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Boolean problem

    All the steps I listed would use methods from the String class plus the concatenation operator: +
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. boolean problem.
    By Leonardo1143 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 13th, 2013, 05:32 PM
  2. Boolean Method Test Problem
    By Khadafi in forum Java Theory & Questions
    Replies: 4
    Last Post: January 13th, 2012, 11:07 PM
  3. Problem printing a two dimensional boolean array
    By sallyB in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 23rd, 2011, 03:56 PM
  4. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 10:56 AM
  5. [SOLVED] Modification of Boolean function in Java program
    By lotus in forum Java SE APIs
    Replies: 4
    Last Post: July 10th, 2009, 10:15 AM