Building a Simple Pizza Order Calculator in Java

Karthikeyantse
2 min readNov 27, 2023

--

Introduction:

Java is a versatile programming language that allows developers to create a wide range of applications. In this tutorial, we’ll walk through the creation of a basic pizza order calculator using Java. This program will prompt the user to select the size of the pizza and choose additional toppings, ultimately providing a total bill for their order.

Code Explanation:

// Include necessary Java libraries
package pizza;
import java.util.Scanner;

// Define the main class
public class PizzaCalculator {

public static void main(String[] args) {

// Initialize Scanner object for user input
Scanner myObj = new Scanner(System.in);
int total = 0;

// Prompt user for pizza size
System.out.println("What Size of Pizza you want to have Enter L - Large, M - Medium, S - Small");
String size = myObj.nextLine().toUpperCase();

// Prompt user for pepperoni
System.out.println("Add pepperoni in pizza (Y or N):");
String pepperoni = myObj.nextLine().toUpperCase();

// Prompt user for cheese
System.out.println("Add cheese in pizza (Y or N):");
String cheese = myObj.nextLine().toUpperCase();

// Calculate total based on user input
if (size.equals("S")) {
total = total + 15;
} else if (size.equals("M")) {
total = total + 20;
} else if (size.equals("L")) {
total = total + 25;
}

// Add pepperoni cost if selected
if (pepperoni.equals("Y")) {
if (size.equals("S")) {
total = total + 2;
} else {
total = total + 3;
}
}

// Add cheese cost if selected
if (cheese.equals("Y")) {
total = total + 1;
}

// Display final bill
System.out.println("Thank you for choosing Java Pizza Deliveries!");
System.out.println("Your final bill is: $" + total);
}
}

Conclusion:

This simple Java program demonstrates the basics of user input, conditional statements, and variable manipulation. You can further enhance this pizza calculator by adding more features or integrating it into a larger system. Java’s versatility makes it an excellent choice for developing interactive applications.

Feel free to experiment with additional features, such as different toppings or a more sophisticated user interface. Happy coding!

--

--