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 Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? file location SASLError using PLAIN: not-authorized


Advertisement null