0% found this document useful (0 votes)
9 views6 pages

Pizza Order

java

Uploaded by

jikitacwb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Pizza Order

java

Uploaded by

jikitacwb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

import java.util.

Scanner; // Needed for the Scanner class

/**

* This program allows the user to order a pizza.

*/

public class PizzaOrder {

public static void main(String[] args) {

// Create a Scanner object to read input.

Scanner keyboard = new Scanner(System.in);

// Declare variables

String firstName; // User's first name

boolean discount = false; // Flag for discount

int inches; // Size of the pizza

char crustType; // For type of crust

String crust = "New York Style"; // Default crust type

double cost = 12.99; // Default cost of the pizza

final double TAX_RATE = .07; // Sales tax rate

double tax; // Amount of tax

char choice; // User's choice

String input; // User input

String toppings = "Cheese "; // Default topping

int numberOfToppings = 0; // Number of toppings

// Prompt user and get first name.

System.out.println("Welcome to Sam and Judy's Pizza");

System.out.print("Enter your first name: ");


firstName = keyboard.nextLine();

// Task #1: Determine if user is eligible for discount (by name)

// Normalize input to lowercase for case-insensitive comparison

firstName = firstName.trim().toLowerCase();

if (firstName.equals("judy") || firstName.equals("sam")) {

discount = true;

// Prompt user and get pizza size choice.

System.out.println("Pizza Size (inches) Cost");

System.out.println(" 8 $10.99");

System.out.println(" 10 $12.99");

System.out.println(" 16 $14.99");

System.out.println(" 20 $16.99");

System.out.println("What size pizza would you like?");

System.out.print("8, 10, 16, or 20 (enter the number only): ");

inches = keyboard.nextInt();

// Task #2: Set price and size of pizza ordered based on choice

if (inches == 8) {

cost = 10.99;

} else if (inches == 10) {

cost = 12.99;

} else if (inches == 16) {

cost = 14.99;

} else if (inches == 20) {


cost = 16.99;

} else {

// If invalid size, default to 10 inches with $12.99 cost

System.out.println("Invalid choice, making a 10-inch pizza.");

inches = 10;

cost = 12.99;

// Consume the remaining newline character after integer input.

keyboard.nextLine();

// Prompt user and get crust choice.

System.out.println("What type of crust do you want?");

System.out.print("(H) Hand-tossed, (T) Thin-crust, or (N) New York Style


(enter H, T, or N): ");

input = keyboard.nextLine();

crustType = input.charAt(0);

// Task #3: Set user's crust choice on pizza ordered using a switch
statement

switch (Character.toLowerCase(crustType)) {

case 'h':

crust = "Hand-tossed";

break;

case 't':

crust = "Thin-crust";

break;
case 'n':

crust = "New York Style";

break;

default:

System.out.println("Invalid choice, making a New York Style


crust.");

break;

// 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.50 each, choose from:");

System.out.println("Pepperoni, Sausage, Onion, Mushroom");

// Ask user about each topping and add to the order if selected

System.out.print("Do you want Pepperoni? (Y/N): ");

input = keyboard.nextLine();

choice = input.charAt(0);

if (choice == 'Y' || choice == 'y') {

numberOfToppings += 1;

toppings += "Pepperoni ";

System.out.print("Do you want Sausage? (Y/N): ");

input = keyboard.nextLine();

choice = input.charAt(0);

if (choice == 'Y' || choice == 'y') {


numberOfToppings += 1;

toppings += "Sausage ";

System.out.print("Do you want Onion? (Y/N): ");

input = keyboard.nextLine();

choice = input.charAt(0);

if (choice == 'Y' || choice == 'y') {

numberOfToppings += 1;

toppings += "Onion ";

System.out.print("Do you want Mushroom? (Y/N): ");

input = keyboard.nextLine();

choice = input.charAt(0);

if (choice == 'Y' || choice == 'y') {

numberOfToppings += 1;

toppings += "Mushroom ";

// Add additional toppings cost to cost of pizza.

cost += (1.50 * numberOfToppings);

// Display order confirmation.

System.out.println();

System.out.println("Your order is as follows: ");

System.out.println(inches + " inch pizza");


System.out.println(crust + " crust");

System.out.println(toppings);

// Task #4: Apply discount if user is eligible.

if (discount) {

System.out.println("You are eligible for a $3.00 discount.");

cost -= 3; // Apply $3 discount

// Task #5: Format monetary output to 2 decimal places

System.out.printf("The cost of your order is: $%.2f\n", cost);

// Calculate and display tax and total cost.

tax = cost * TAX_RATE;

System.out.printf("The tax is: $%.2f\n", tax);

System.out.printf("The total due is: $%.2f\n", (tax + cost));

System.out.println("Your order will be ready for pickup in 45 minutes.");

// Close the scanner object

keyboard.close();

You might also like