In: Computer Science
Lab 14 - Krazy Karl's Pizza Order App
You have been hired by Krazy Karl’s as a freelance application (app) developer. They have asked you to build an app that customers can use to order pizza. The app will ask the user for their name, pizza type, size, and number of pizzas. Then provide the user with an order confirmation, which must include the total cost.
In this lab, you will practice using String format() and switch statements. Additionally, you will write some code in main(), add to computeSize(), write calculateCost(), and add to printOrderInfo().
Step 1 - main()
Adding a do-while loop to main()
Complete the TODO section in main, where you will need to keep asking the user for a number while the number is not 1-4. Use a do while loop to keep asking the user. For each iteration of the loop simply print (do not use println)…
"(1) Small\n(2) Medium\n(3) Large\n(4) Extra-large\nPlease choose a size: "
and get the number from the user. Use numSize to store the user input each time (HINT: use the Scanner method nextInt())
Note: If you copy this line (which we suggest you do to make our auto-grader happy) and the string isn’t blue, you will need to replace the double quotes by manually typing it in. For some reason, zybooks doesn’t like it when you use fancy quotes.
Step 2 - computeSize(int)
Now that you got the size from the user as a number, we need to convert it to a char that is ‘S’, ‘M’, ‘L’ or ‘X’. To do this we will finish the computeSize() method.
Completing computeSize()
The purpose of this method is to get the number the user entered and return the matching character that is the size of the pizza. Example, if computeSize() is passed 1 your method will return ‘S’.
In the method computSize() you will write a switch statement which switches on the int size. The switch statement should return the appropriate character representing the size of pizza chosen. If the user entered 1, return S. If 2, return M. If 3, return L. If 4, return X. If its none of the 4, return ‘U’ for unexpected. Think about whether we need to use break statements in this case because of the returns in the switch statements. Create a switch statement using the parameter size (no if-statements).
The if statement would look like:
if(size == 1){ return 'S'; } else if (size == 2){ return 'M'; } else { return 'U'; }
Step 3 - calculateCost(char,int)
Now that we have the size and number of pizzas(this we did for you in askNumPizzas()), we need to calculate the cost of the meal. To do this you will need to write the method calculateCost() from scratch.
Writing the method calculateCost()
The purpose of this method is to calculate the cost of the order given the size and amount of pizzas.
First, write the method signature. This is a public and static method that returns a double. It also has two parameters, the first is a character representing pizza size and an int representing number of pizzas ordered. The return value will represent the total cost of the order.
Implementing calculateCost()
Now, inside the method body of calculateCost(), declare and initialize a double called cost to 0.0. This will represent the total cost to return at the end of the method. Create a switch statement that uses the character passed into the method.
Note: think about how this is different from the other switch statement and what needs to be considered when implementing it.
After the switch statement, cost will be multiplied by the class constant SALES_TAX.
Finally, return the total cost.
Step 4 - printOrderInfo(String, int, char, int, double)
Completing printOrderInfo()
The purpose of this method is to print out the receipt of the order including the following: name for the order, pizza type, pizza size, number of pizzas, and finally the cost of the order. Most of the method is provided for you. To complete this method, print these this statement using String.format() or printf():
“ with a *some number*% sales tax, your order total is $*some other number*\n*some name*'s pizza will be ready in *some number* minutes.\n”
Note: you’ll need to “hard code” the sales tax to be the int 7 for this print statement and we want the cost represented like an actual price so two trailing decimal points (%.2f).
Below is some detail on how to use String.format() and printf():
PROVIDED CODE:
public class KrazyKarls {
// Class constant
public static final double SALES_TAX = 1.07;
/**
* computeSize() This is a public, static method that returns a
character.
* It has one parameter, a int that represents the size.
*
* @param size An int; from main().
* @return A character; to main().
*/
public static char computeSize(int size) {
// Student TODO
// end Student TODO
return 'S';
}
/**
* calculateCost() This is a public, static method that returns
a
* double. There are two parameters, a character representing pizza
size
* and an integer representing number of pizzas ordered.
*
*
* @param size A character; from main().
* @param numPizzas An integer; from main().
* @return A double; to main().
*/
// Student TODO
// end Student TODO
/**
* printOrderInfo() This is a public, static method which does not
return
* a value to main(). It has four parameters.
*
* @param name A String; from main().
* @param pizzaType An integer; from main().
* @param size A character; from main().
* @param numPizzas An integer; from main().
* @param cost A double; from main().
*/
public static void printOrderInfo(String name, int pizzaType, char
size, int numPizzas, double cost) {
int cookTime = (int) (Math.random() * 45) + 5;
System.out.println("Ordered placed by: " + name);
String pizza = "pizza";
if(numPizzas > 1){
pizza += "s";
}
switch(pizzaType){
case 1:
System.out.print(numPizzas + " " + size + " Buffalo Chicken " +
pizza);
break;
case 2:
System.out.print(numPizzas + " "+ size + " Loaded Baked Potato " +
pizza);
break;
case 3:
System.out.print(numPizzas + " " + size + " North of the Border " +
pizza);
break;
default:
System.out.print(numPizzas + " " + size + " Vegetarian " +
pizza);
}
// Student TODO
// end Student TODO
}
/**
* main() calls all other methods in this class.
*
* @param args The String array args is not used in this
program.
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String name = askName(input);
System.out.println();
int pizzaType = choosePizzaType(input);
System.out.println();
int numSize = 0;
// Student TODO
// end Student TODO
input.close();
}
//-------------------DO NOT MODIFY CODE BELOW THIS
LINE----------------------
public static String askName(Scanner input) {
// Prompt customer for name.
System.out.println("*********************************************");
System.out.println("******* Welcome to Krazy Karl's App
*******");
System.out.println("*********************************************");
System.out.print("What is the name for the order: ");
// Assign user input to local String.
String name = input.nextLine();
// Return value of local String.
return name;
}
public static int choosePizzaType(Scanner input) {
// Prompt customer for type of pizza.
System.out.println("Specialty Pizzas...");
System.out.println("(1) Buffalo Chicken");
System.out.println("(2) Loaded Baked Potato");
System.out.println("(3) North of the Border");
System.out.println("(4) Vegetarian");
System.out.print("Please select a pizza using 1, 2, 3, or 4:
");
// Assign user input to local integer.
String pizza = input.nextLine();
// Return value of local integer.
return Integer.parseInt(pizza);
}
public static int askNumPizzas(Scanner input) {
System.out.print("Enter the number of pizzas for this order:
");
String numPizzas = input.next();
return Integer.parseInt(numPizzas);
}
}
Hi,
Please find the completed Java code below:
////////////////////////////////////
package pizza;
import java.util.Scanner;
public class KrazyKarls {
// Class constant
public static final double SALES_TAX = 1.07;
/**
* computeSize() This is a public, static method that
returns a character.
* It has one parameter, a int that represents the
size.
*
* @param size An int; from main().
* @return A character; to main().
*/
public static char computeSize(int size) {
// Student TODO
switch(size) {
case 1:
return
'S';
case 2:
return
'M';
case 3:
return
'L';
case 4:
return
'X';
default:
return
'U';
}
// end Student TODO
}
/**
* calculateCost() This is a public, static method that
returns a
* double. There are two parameters, a character
representing pizza size
* and an integer representing number of pizzas
ordered.
*
*
* @param size A character; from main().
* @param numPizzas An integer; from main().
* @return A double; to main().
*/
// Student TODO
public static double calculateCost(char size,int
numberOfPizzas) {
double totalCost=0.0;
switch(size) {
case 'S':
totalCost=8.99*numberOfPizzas;
break;
case 'M':
totalCost=12.99*numberOfPizzas;
break;
case 'L':
totalCost=15.99*numberOfPizzas;
break;
default:
totalCost=17.99*numberOfPizzas;
}
return totalCost * SALES_TAX;
}
// end Student TODO
/**
* printOrderInfo() This is a public, static method
which does not return
* a value to main(). It has four parameters.
*
* @param name A String; from main().
* @param pizzaType An integer; from main().
* @param size A character; from main().
* @param numPizzas An integer; from main().
* @param cost A double; from main().
*/
public static void printOrderInfo(String name, int
pizzaType, char size, int numPizzas, double cost) {
int cookTime = (int) (Math.random()
* 45) + 5;
System.out.println("Ordered
placed by: " + name);
String pizza = "pizza";
if(numPizzas > 1){
pizza +=
"s";
}
switch(pizzaType){
case 1:
System.out.print(numPizzas + " " + size + " Buffalo Chicken " +
pizza);
break;
case 2:
System.out.print(numPizzas + " "+ size + " Loaded Baked Potato " +
pizza);
break;
case 3:
System.out.print(numPizzas + " " + size + " North of the Border " +
pizza);
break;
default:
System.out.print(numPizzas + " " + size + " Vegetarian " +
pizza);
}
// Student TODO
// end Student TODO
}
/**
* main() calls all other methods in this class.
*
* @param args The String array args is not used in
this program.
*/
public static void main(String[] args) {
Scanner input = new
Scanner(System.in);
String name =
askName(input);
System.out.println();
int pizzaType =
choosePizzaType(input);
System.out.println();
int numSize = 0;
// Student TODO
do {
System.out.print("(1) Small\n(2) Medium\n(3) Large\n(4)
Extra-large\nPlease choose a size: ");
numSize =
input.nextInt();
}while(numSize < 0 &&
numSize > 4);
char size=
computeSize(numSize);
int numPizzas=
askNumPizzas(input);
double cost= calculateCost(size,numPizzas);
printOrderInfo(name,pizzaType,size,numPizzas,cost);
// end Student TODO
input.close();
}
//-------------------DO NOT MODIFY CODE BELOW THIS
LINE----------------------
public static String askName(Scanner input) {
// Prompt customer for name.
System.out.println("*********************************************");
System.out.println("******* Welcome
to Krazy Karl's App *******");
System.out.println("*********************************************");
System.out.print("What is the name
for the order: ");
// Assign user input to local
String.
String name =
input.nextLine();
// Return value of local
String.
return name;
}
public static int choosePizzaType(Scanner input)
{
// Prompt customer for type of
pizza.
System.out.println("Specialty
Pizzas...");
System.out.println("(1) Buffalo
Chicken");
System.out.println("(2) Loaded
Baked Potato");
System.out.println("(3) North of
the Border");
System.out.println("(4)
Vegetarian");
System.out.print("Please select a
pizza using 1, 2, 3, or 4: ");
// Assign user input to local
integer.
String pizza =
input.nextLine();
// Return value of local
integer.
return
Integer.parseInt(pizza);
}
public static int askNumPizzas(Scanner input)
{
System.out.print("Enter the number
of pizzas for this order: ");
String numPizzas =
input.next();
return
Integer.parseInt(numPizzas);
}
}
////////////////////////////////////////////////////