Colorado State University Logo | Summer 21: CS 150 - Culture and Coding (AUCC 3B/GT-AH3) Colorado State University Logo | Summer 21: CS 150 - Culture and Coding (AUCC 3B/GT-AH3)
Summer 21: CS 150 - Culture and Coding (AUCC 3B/GT-AH3)
Computer Science

Lab 14 - Krazy Karl's Pizza Order App

Introduction

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().

What You’ll Learn

  • More practice with methods and scope
  • Switch statements with characters and Strings
  • String.format() and System.out.printf()

Step 0 - Provided Code

You will want to look through the provided code before starting. This program has 6 methods and one main(). 3 are written for you (askName(), choosePizzaType(), askNumPizzas()), 3 have been started (computeSize(), printOrderInfo(), main()) and 1 will need to be written (calculateCost()). Try even running the program before you start coding to see what we have implemented already!

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.

Testing your do-while loop

To Test if this worked run the program. You will be asked your name, which pizza you want and what size (which is what you just implemented. If you enter a non valid number for the size your program should ask you again for a size until you enter 1 2 3 or 4. Once you enter a number 1-4 for the pizza size, your program should just quit.

To test our other methods you will need to keep adding to main() but each section will specify what to add to.

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

Testing computeSize()

Once you complete the method you must test your method in main before you continue. To test, copy the following code and put it right under your do-while loop in main() but before we close the scanner.

char size = ' ';
size = computeSize(numSize);
System.out.println("You requested a " + size + "-sized pizza.");
System.out.println();

Now, run your program and determine if the number you enter outputs the appropriate character.

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.

  • If size is equivalent to ‘S’, re-assign cost to 8.99 multiplied by the int that represents the number of pizzas, this is the second parameter that was passed in to calculateCost().
  • If size is equivalent to ‘M’, re-assign cost to 12.99 multiplied by the number of pizzas.
  • If size is equivalent to ‘L’, re-assign cost to 15.99 multiplied by the number of pizzas.
  • For the default case, re-assign cost to 17.99 multiplied by the number of pizzas.

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.

Testing calculateCost()

To test this method, copy and paste the code below in main() right after the last code we pasted but before closing the scanner.

int numPizzas = askNumPizzas(input);
System.out.println();
double cost = calculateCost(size, numPizzas);
System.out.println("Cost: " + 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():

String.format() first takes a String literal with placeholders followed by a comma. After the comma are a varying amount of arguments. These arguments correspond to the placeholders in the String literal and they can be of any type. The placeholders are marked by using the ‘%’ character. String.format() and printf() are very similar, but String.format() returns a String and printf() prints directly to the console.

String.format() Example:
int number = 3;
String str = variables;
char c = !;
String s = String.format(I can print  %d %s with this tool and make it look pretty%c, number, str, c);

The String s is now equivalent to:

I can print 3 variables with this tool and make it look pretty!
printf() Example:
int number = 3;
String str = variables;
char c = !;
System.out.printf(I can print %d %s with this tool and make it look pretty%c%n, number, str, c);

The following will be printed to the console:

I can print 3 variables with this tool and make it look pretty!

Note: We include the character ‘%n’ in the String literal of printf() so that we have a new line after our statement. The method printf() does not automatically include a newline character.

To test your method and before submission copy this code underneath the last code you copied to main() but before we close the scanner and run your program.

printOrderInfo(name, pizzaType, size, numPizzas, cost);

HINT: Since we use ‘%’ to indicate a placeholder or a “blank”, to print a ‘%’ to the console our String literal needs to include “%%”. Doubles also have particular syntax because we can decide how many decimal places to print.

Computer Science Department

279 Computer Science Building
1100 Centre Avenue
Fort Collins, CO 80523
Phone: (970) 491-5792
Fax: (970) 491-2466

Summer 21: CS 150 - Culture and Coding (AUCC 3B/GT-AH3)

Survey of computer science, formal logic, and computational thinking. Explores the historical, gender, and cultural perspectives on the role of technology in society. Includes learning a basic programming language. Students will be expected to write small programs, and construct written arguments on ways in which technology influences our modern culture. Previous computer science experience not necessary.