Pizza Order..??



Share on Google+Share on Google+

Pizza Order..??
2 Answer(s)      7 years ago
Posted in : Java Beginners
Define a class PizzaOrder class that contains a list of several types of pizza that ordered by the customer. This class also contains number of pizza been ordered. There is a method to add new pizza to the list (which increment the number of pizza been ordered) and also a method to calculate the cost of the entire order. Small pizza costs RM9.90, a medium pizza is RM13.90, and a large pizza costs RM15.90. Each topping add RM1.00 to the pizza.

Next, define a class named Pizza class contains information about a specific pizza. The variables of pepperoni, sausage, and mushrooms are Booleans that indicate whether or not these toppings are present on the pizza. The size variable is a character of value ?s?, ?m?, or ?l? to indicate small, medium, or large. There is also a Pizza constructor that initializes all of these values. The getsize() methods returns the size of the pizza and the getNumToppings() method returns a number from 0 until 3 depending on what toppings are present (e.g, if the pizza has pepperoni and mushrooms, it would be 2).
View Answers

March 2, 2010 at 6:23 AM

NOTE: Input validations and Exception Handling are NOT done.

Class Pizza

public class Pizza {

boolean pepporoni;
boolean sausage;
boolean mushrooms;
String size;

enum PizzaSize {
SMALL(9.90), MEDIUM(13.90), LARGE(15.90);
private double cost;

PizzaSize(double cost) {
this.cost = cost;

public double getCost() {
return cost;

PizzaSize pizzaSize;

public int getNumOfToppings() {
int toppings = 0;
if (pepporoni) {
toppings += 1;
if (sausage) {
toppings += 1;
if (mushrooms) {
toppings += 1;
return toppings;

public String getSize() {
return size;

public Pizza(String size, boolean pepporoni, boolean sausage,
boolean mushrooms) {
this.size = size;
this.pepporoni = pepporoni;
this.sausage = sausage;
this.mushrooms = mushrooms;

public double getCost() {
double cost = 0.0;
if (getSize().equalsIgnoreCase("s"))
cost = pizzaSize.SMALL.cost;
if (getSize().equalsIgnoreCase("m"))
cost = pizzaSize.MEDIUM.cost;
if (getSize().equalsIgnoreCase("l"))
cost = pizzaSize.LARGE.cost;
return cost;

Class PizzaOrder

import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;

public class PizzaOrder {

ArrayList<Pizza> pizzaOrders = new ArrayList<Pizza>();
static int numOfPizzas;
boolean pepporoni;
boolean mushrooms;
boolean sausage;
String sizeOfPizza = null;
double total = 0.0;
Pizza pizza;

public static void main(String[] args) {
Scanner scan = new Scanner(;
String orderedToppings = null;
int toppings = 0;
String wantMore = "n";
PizzaOrder order = new PizzaOrder();
do {
order.pepporoni = false;
order.mushrooms = false;
order.sausage = false;
System.out.println("Enter the size of Pizza (s/m/l) : ");
order.sizeOfPizza =;
.println("No.of toppings you would like to have(0 to 3): ");
toppings = scan.nextInt();
if (toppings > 0) {
.println("What toppings you would like to have? Pepporoni/Sausage/Mushrooms : (Each topping should be separated by / ");
orderedToppings =;
StringTokenizer token = new StringTokenizer(orderedToppings,
while (token.hasMoreTokens()) {
String topping = token.nextToken();
if (topping.equalsIgnoreCase("pepporoni"))
order.pepporoni = true;
if (topping.equalsIgnoreCase("sausage"))
order.sausage = true;
if (topping.equalsIgnoreCase("mushrooms"))
order.mushrooms = true;


.println("Would you like to order one more pizza? y/n : ");
wantMore =;
} while (wantMore.equals("y"));


public void printPizzaOrder() {
for (int i = 0; i < pizzaOrders.size(); i++) {
Pizza p = pizzaOrders.get(i);
int temp = p.getNumOfToppings();
System.out.println("Size of Pizza = " + p.getSize());
System.out.println("No.of Toppings: " + temp);
if (temp > 0) {
if (p.pepporoni)
if (p.sausage)
if (p.mushrooms)
double crustCost = p.getCost();
double toppingsCost = p.getNumOfToppings();
System.out.println("Cost of Pizza: $" + crustCost);
System.out.println("Cost of Toppings : $" + toppingsCost);
System.out.println("Total cost of Pizza: $"
+ (crustCost + toppingsCost));
System.out.println("No. of Pizzas ordered : " + getNoOfPizzas());
System.out.println("Total Bill = " + total);

public void addPizza() {
numOfPizzas += 1;
pizza = new Pizza(sizeOfPizza, pepporoni, sausage, mushrooms);
updateTotal(pizza.getCost() + pizza.getNumOfToppings());

public int getNoOfPizzas() {
return numOfPizzas;

public void updateTotal(double cost) {
total += cost;

public double getTotalCost() {
return total;


Enter the size of Pizza (s/m/l) :
No.of toppings you would like to have(0 to 3):
What toppings you would like to have? Pepporoni/Sausage/Mushrooms : (Each topping should be separated by /
Would you like to order one more pizza? y/n :
Enter the size of Pizza (s/m/l) :
No.of toppings you would like to have(0 to 3):
What toppings you would like to have? Pepporoni/Sausage/Mushrooms : (Each topping should be separated by /
Would you like to order one more pizza? y/n :
Size of Pizza = s
No.of Toppings: 1
Cost of Pizza: $9.9
Cost of Toppings : $1.0
Total cost of Pizza: $10.9
Size of Pizza = l
No.of Toppings: 2
Cost of Pizza: $15.9
Cost of Toppings : $2.0
Total cost of Pizza: $17.9
No. of Pizzas ordered : 2
Total Bill = 28.799999999999997


October 18, 2011 at 4:25 PM

Why i cannot run this program...please help


Java Spring Hibernate Struts Training java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient How do I resolve this Java Class not found exception? httpclient java.lang.NoClassDefFoundError Apache Commons ioutils maven dependency Read/Convert an inputStream to a String What is the meaning of Java Platform? Why Java is a platform independent language? What is the benefits of learning Core Java? Which technology should I learn after Java? What is array in java with example? How to Convert ArrayList to Array? How to substring in Java? How to format number in Java? What is instance variable in Java? How to download MySQL JDBC driver? What is Calendar class in Java? Which is the best Java tutorials for beginners? How to rename a file in Java? How to delete file in Java code? How to get day from date in Java using Calendar? How to get day of week in Java? How to calculate Date Difference in Java? How to compare date in Java? How to declare array in Java? How to calculate average of array in Java? What is Array in Java? write a java program to find the summation of all the integers entered on command line Sum of two numbers using command line arguments in Java How to create and use Array in Java? How to pass command line arguments in Java? How to create Applet Hello World? Appending String efficiently in Java How to append String in Java? How to list even numbers between 1 and 100? How to add BigDecimal in Java? What is Abstraction In Java? Which is best Beginners Java Tutorial? What is java.util package? Create list from array in Java Filter collection in Java 8 What is the best way to filter a Java Collection? Easy way to transform Collection to Array? How to convert Collection to Array in Java? What are Basic Java Language Elements? Advanced Java Tutorials in 2017 Java brief history Best Reasons to learn Java Java Example Codes and Tutorials in 2017 How do I read a large file quickly in Java? Is learning Java worthwhile?


Advertisement null