• 0

Help with Assignment


Question

So i need help with picking the pizza size as you can see for the if PizzaSize if i end up just using else if with the !Pizza.equals blablba it will result in massive error it says like PizzaInches and so on not recognizable but with the current code right now everything that i pick will result in S small size even though it print the correct size that the user put in. 

So yeah: if the string is not equal to "M", "L" or "H" , set it to "S"

Please help

 

Attach is the assignment instructions

import java.util.Scanner; //So the user can write also

/*
 * PizzaOrder
 * The goal of this software is to create an organize and detail order for a pizza for the user
 * A software which let the user input numeric and letter and later will be calculate for the price that the user need to pay
 * 
 * Anthony Liestyo
 * 
 * OS: Windows 8
 * Hardware: PC
 * 
 * Sept 27th 2013
 */

public class PizzaOrder {

    public static void main(String[] args) {
        // TODO code application logic here
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please enter pizza size (S, M, L, or H): ");  //Pizza sizes for the user to enter
        String PizzaSize =  keyboard.nextLine() ; //Print the answer in next line
        PizzaSize = PizzaSize.substring(0, 1); //So it will only take the first letter
        
        double PizzaInches;  // Double = number with decimal places
        double PizzaPrice;   // Int = number with no decimal places
                
        if (PizzaSize == "S")  //If statement for different sizes of pizza resulting in different variation of answers
        {
            PizzaSize = "S";
            PizzaInches = 8;
            PizzaPrice = 5;
        }
        else if (PizzaSize == "M")
        {
            PizzaSize = "M";
            PizzaInches = 13;
            PizzaPrice = 8;
        }
        else if (PizzaSize == "L")
        {
            PizzaSize = "L";
            PizzaInches = 21;
            PizzaPrice = 13;
        }
        else if (PizzaSize == "H")
        {
            PizzaSize = "H";
            PizzaInches = 34;
            PizzaPrice = 21;
        }
        else if (!PizzaSize.equals("M") && !PizzaSize.equals("L") && !PizzaSize.equals("H") && !PizzaSize.equals("m") && !PizzaSize.equals("l") && !PizzaSize.equals("h"))
        {
            PizzaSize = "S";
            PizzaInches = 8;
            PizzaPrice = 5;
        }      
        else
        {
            //PizzaSize = "S";
            PizzaInches = 8;
            PizzaPrice = 5;
        }  
        System.out.println("Pizza Size: " + PizzaSize.toUpperCase()); //to let the lowercase letter become Capital/uppercase
        
        System.out.println("Enter the number of toppings you want: (0-5)");
        
        double Toppings = keyboard.nextInt();
        
        if (Toppings < 0) //Toppings less than 0 become 0
        {
            Toppings = 0;
            System.out.println("Your toppings is smaller than 0, resetting it to " + Toppings);
        }
        else if (Toppings > 5) //TOppings bigger than 5 become 5
        {
            Toppings = 5;
            System.out.println("Your toppings is bigger than 5, resetting it to " + Toppings);
        }
        else if (Toppings >= 0 && Toppings <= 5) //Toppings greater than 0 and toppings less than 5 then toppings = toppings
        {
            Toppings = Toppings;
            System.out.println("You have selected " + Toppings + " toppings for your pizza");
        }
        
        
        
        double Area = 3.14 * (PizzaInches/2) * (PizzaInches/2);         //Area Formula for circle/pizza: 3,14 r * r
        double subTotal = PizzaPrice + (Toppings * 0.89);               //subTotal : PizzaPrice + (Toppings * 0.99)
        double roundSubTotal = Math.floor(subTotal*100)/100;            //roundSubTotal : Rounding to the lowest 2 decimal points
        double Tax = subTotal * 0.095;                                  //Tax is 9.5% of the subTotal
        double roundTax = Math.floor(Tax*100)/100;                      //roundTax : Rounding to the lowest 2 decimal points
        double Total = Tax + subTotal;                                  //Total is the tax + subTotal
        double roundTotal = Math.floor(Total*100)/100;                  //roundTotal : Rounding to the lowest 2 decimal points
        double PricePerInch = roundSubTotal/Area;                       //PricePerInch : price for each inches square
        double RoundPricePerInch = Math.floor(PricePerInch*100)/100;    //roundPricePerInch : Rounding to the lowest 2 decimal points
        //These are the output for the results that we calculated earlier
        
        System.out.println("Pizza diameter: " + PizzaInches);
        System.out.println("Pizza baseprice:  $" + PizzaPrice);
        System.out.println("Pizza Size: " + PizzaSize + " (" + PizzaInches + " inches -- " + Area + " square inches)"); 
        System.out.println("Toppings: " + Toppings);
        System.out.println("Price: " + "$" + roundSubTotal);
        System.out.println("Tax: $" + roundTax);
        System.out.println("Total Price:  $" + roundTotal);
        System.out.println("Price/sq.in.:  $" + RoundPricePerInch);

          
 }
 }

P1.pdf

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0

	switch ( PizzaSize.toLowerCase() ) { 

case "h":

PizzaSize = "H";

PizzaInches = 34;

PizzaPrice = 21;

break;

case "l":

PizzaSize = "L";

PizzaInches = 21;

PizzaPrice = 13;

break;

case "m":

PizzaSize = "M";

PizzaInches = 13;

PizzaPrice = 8;

break;

default:

PizzaSize = "S";

PizzaInches = 8;

PizzaPrice = 5;

break;

}

Link to comment
Share on other sites

  • 0

You dont need the final else-if clause (the final else will do the same job)

if (PizzaSize == "S")  //If statement for different sizes of pizza resulting in different variation of answers
        {
            PizzaSize = "S";
            PizzaInches = 8;
            PizzaPrice = 5;
        }
        else if (PizzaSize == "M")
        {
            PizzaSize = "M";
            PizzaInches = 13;
            PizzaPrice = 8;
        }
        else if (PizzaSize == "L")
        {
            PizzaSize = "L";
            PizzaInches = 21;
            PizzaPrice = 13;
        }
        else if (PizzaSize == "H")
        {
            PizzaSize = "H";
            PizzaInches = 34;
            PizzaPrice = 21;
        }        
        else
        {
            PizzaSize = "S";
            PizzaInches = 8;
            PizzaPrice = 5;
        }  
Link to comment
Share on other sites

  • 0

Currently if you input a lowercase letter it will always result in selecting the default size ("S") because you only check for capital letters. Use toUpperCase() on the user input so you can then assume it's a capital letter everywhere. I would guess that is the reason for the problem you describe but you didn't give an example of input with which the program behaves incorrectly.

 

Also there are some rendundancies in the code:

(...)
else if (!PizzaSize.equals("M") && // etc.
{
     PizzaSize = "S";
     // etc
}
else
{
    PizzaSize = "S";
    // etc 
}

As you can see, both branches do the same thing, so you only need the more general one, i.e. the else clause. The else if above can be removed.

 

Also, you do:

if (PizzaSize == "S")
{
     PizzaSize = "S"; 
     // etc
}
else if (PizzaSize == "M")
{
    PizzaSize = "M";
    // etc
}
//repeat

what's the point of setting PizzaSize to "S" if it's already "S" as your condition just checked for? Same for all the other cases, it's all redundant. You're setting a variable to the value that it currently has.

 

Also there would be more robust ways of coding a mapping between values like that than using a bunch of if statements (using a HashTable to store all possible combinations, for instance), but I suppose you haven't seen those in your course yet.

Link to comment
Share on other sites

  • 0

Thank you very much and i am also wondering what is the meaning of "document the use of a command line window with: javac, javadoc and 

java" Can somebody give me a clue.
Link to comment
Share on other sites

  • 0

Thank you very much and i am also wondering what is the meaning of "document the use of a command line window with: javac, javadoc and 

java" Can somebody give me a clue.

It means you should provide the commands you issued to the java compiler (javac) such as:

$ javac PizzaOrder.java
As well as how you ran the program and input the data.

$ java PizzaOrder
Please enter pizza size (S, M, L, or H):
m
...
etc
It's basically to show you understand the whole process from implementation, to compilation, documentation, and finally using the program you created.
Link to comment
Share on other sites

  • 0

These are command-line utilities independent of your program. Your teacher is asking you to explain how you could use them with your program. javac is a compiler, so you could use it to compile your program. java is the runtime, so you could use it to run your program. javadoc is a tool for generating documentation from comments in source files, so you could use it to generate documentation from comments in your program. What is it that you don't understand?

Link to comment
Share on other sites

  • 0

Can you give me an example from my work, my instructor explains the same thing as you but i stil don get a clue :|

For the case of 'javadoc', you need to write the appropriate documentation comments and tags in your code, then run:

$ javadoc PizzaOrder.java
Your assignment notes state that extra credit is available for students who document (record) what commands they execute to complete the task. Elsewhere, it specifies that screendumps should be made. So just do both. Think of it as showing your working out in a maths or physics exam, or thinking in an english exam. Academia likes thought processes as much as it does right answers. If you show clearly what steps you're taking as you design, write, compile, document, and test the code, you'll get good marks.

I modified your code to make it a bit more structured so that you can see how you can document it:

import java.io.Console;
import java.io.PrintStream;
import java.util.Map;
import java.util.HashMap;
import java.util.Collections;

enum PizzaSize { 
	HUGE ( 34 ), LARGE ( 21 ), MEDIUM ( 13 ), SMALL ( 8 );
	
	final int diameter;
	PizzaSize ( int diameter ) {
		this.diameter = diameter;
	}
	
	public int getDiameter() { return diameter; };		
};

class Pizza {

	final double PI = 3.14;
		
	private PizzaSize size;
	private float 	  price;
	
	public Pizza ( PizzaSize size, float price ) {
		this.size  = size;
		this.price = price; 
	}

	public double getArea () {
	
		double radius = size.getDiameter() / 2;
		return PI * ( radius * radius );
	}
	
	public PizzaSize getSize () { return size; }
	public float getPrice () { return price; }
}

/** 
* Main class ??
* <p>
* This is a long description 
* ...
* </p>
* @author testAuthor
* @version 1.0
*/
public class PizzaOrder {
	
	/** Helper table for size code mappings */
	private static Map <String, Pizza> pizzaTable = createTable ();
	private static Map <String, Pizza> createTable () {
		Map <String, Pizza> table = new HashMap <String, Pizza> ();
		
		table.put ( "H", new Pizza ( PizzaSize.HUGE,   21 ) );
		table.put ( "L", new Pizza ( PizzaSize.LARGE,  13 ) );
		table.put ( "M", new Pizza ( PizzaSize.MEDIUM, 8  ) );
		table.put ( "S", new Pizza ( PizzaSize.SMALL,  5  ) );
		
		return Collections.unmodifiableMap ( table );
	}

	/** Entry point */
	public static void main ( String[] args ) {
		
		final String	defaultSize	= "S";
		final double	toppingPrice	= 0.89;
		final double	taxRate		= 0.095; /* % */
		final int	maxToppings	= 5;
		final int	minToppings	= 0;
		
		PrintStream out = System.out;

		out.println ( "Today's menu:" );
		
		for ( Pizza pizza : pizzaTable.values() )
			out.println ( pizza.getSize().toString() + " PIZZA - " + formatMoney ( pizza.getPrice() ) );
								
		Console cnsl = System.console();
		String 	size = cnsl.readLine ( "Please enter pizza size S/M/L/H (case insensitive). Default is " + defaultSize + ":" ).toUpperCase();
		
		if ( size.isEmpty() || null == pizzaTable.get ( size ) )
			size = defaultSize;
		
		Pizza	order = pizzaTable.get ( size );
		double 	total = order.getPrice();
		
		out.println ( "You selected a " + order.getSize().toString() + " Pizza" );
		int toppings = Math.min ( maxToppings, Integer.parseInt ( cnsl.readLine ( "How many toppings do you want for your pizza (0-5) ?" ) ) );
		toppings = Math.max ( minToppings, toppings );
		
		total += toppings * toppingPrice;
		
		out.println ( "Diameter: " + order.getSize().getDiameter() );
		out.println ( "Base price: " + formatMoney ( order.getPrice() ) );
		out.println ( "Area: " + order.getArea() );
		out.println ( "Price including toppings: " + formatMoney ( total ) );
		out.println ( "Price per square inch: " + formatMoney ( total / order.getArea() ) );
		out.println ( "Tax: " + formatMoney ( total * taxRate ) );
		
		total += total * taxRate;	
		out.println ( "Total including tax: " + formatMoney ( total ) );
	}
	
	/** Sets currency and formats input to two decimal places */
	private static String formatMoney ( double in ) { 
		return String.format ( "$%.2f", in );
	} 
}
If you run javadoc on it as per the command above, it will generate a set of html files which can be viewed in a webbrowser. For more information on javadoc, the wikipedia page is helpful.
Link to comment
Share on other sites

  • 0

It might also be a good idea to reduce the complexity of main() and delegate functionality to other routines. You could then document them individually with javadoc.

 

import java.io.PrintStream;
import java.util.Map;
import java.util.HashMap;
import java.util.Collections;

enum PizzaSize { 
	HUGE ( 34 ), LARGE ( 21 ), MEDIUM ( 13 ), SMALL ( 8 );
	
	private final int diameter;
	PizzaSize ( int diameter ) {
		this.diameter = diameter;
	}
	
	public int getDiameter() { return diameter; };		
};

class Pizza {

	private final double PI = 3.14;
		
	private PizzaSize size;
	private float 	  price;
	
	public Pizza ( PizzaSize size, float price ) {
		this.size  = size;
		this.price = price; 
	}

	public double getArea () {
	
		double radius = size.getDiameter() / 2;
		return PI * ( radius * radius );
	}
	
	public PizzaSize getSize () { return size; }
	public float getPrice () { return price; }
}

public class PizzaOrder {
	
	private static Map <String, Pizza> pizzaTable = createTable ();
	private static Map <String, Pizza> createTable () {
		Map <String, Pizza> table = new HashMap <String, Pizza> ();
		
		table.put ( "H", new Pizza ( PizzaSize.HUGE,   21 ) );
		table.put ( "L", new Pizza ( PizzaSize.LARGE,  13 ) );
		table.put ( "M", new Pizza ( PizzaSize.MEDIUM, 8  ) );
		table.put ( "S", new Pizza ( PizzaSize.SMALL,  5  ) );
		
		return Collections.unmodifiableMap ( table );
	}

	public static void main ( String[] args ) {
	
		final double	toppingPrice	= 0.89;
		final double	taxRate		= 0.095; /* % */
		
		printMenu();
												
		Pizza	order = getOrder();
		double 	total = order.getPrice();
		
		PrintStream out = System.out;
		out.println ( "You selected a " + order.getSize().toString() + " Pizza" );
		
		int toppings = getToppings();
		total += toppings * toppingPrice;
		
		out.println ( "Diameter: " + order.getSize().getDiameter() );
		out.println ( "Base price: " + formatMoney ( order.getPrice() ) );
		out.println ( "Area: " + order.getArea() );
		out.println ( "Price including toppings: " + formatMoney ( total ) );
		out.println ( "Price per square inch: " + formatMoney ( total / order.getArea() ) );
		out.println ( "Tax: " + formatMoney ( total * taxRate ) );
		
		total += total * taxRate;	
		out.println ( "Total including tax: " + formatMoney ( total ) );
	}
	
	private static void printMenu() {
		System.out.println ( "Today's menu:" );
		
		for ( Pizza pizza : pizzaTable.values() )
			System.out.println ( pizza.getSize().toString() + " PIZZA - " + formatMoney ( pizza.getPrice() ) );	
	}
	
	private static Pizza getOrder () {
		final String	defaultSize	= "S";
	
		String size = System.console().readLine ( "Please enter pizza size S/M/L/H (case insensitive). Default is " + defaultSize + ":" ).toUpperCase();
		if ( size.isEmpty() || null == pizzaTable.get ( size ) )
			size = defaultSize;
		
		return pizzaTable.get ( size );
	}

	private static int getToppings () {
		final int	maxToppings	= 5;
		final int	minToppings	= 0;
		
		int toppings = Math.min ( maxToppings, Integer.parseInt ( System.console().readLine ( "How many toppings do you want for your pizza (0-5) ?" ) ) );
		toppings = Math.max ( minToppings, toppings );
		
		return toppings;
	}
		
	private static String formatMoney ( double in ) { 
		return String.format ( "$%.2f", in );
	} 
}
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.