Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Pizza Order..??
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


mar
Pizza Order..??
2 Answer(s)      4 years and 10 months 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).

Advertisement
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(System.in);
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 = scan.next();
System.out
.println("No.of toppings you would like to have(0 to 3): ");
toppings = scan.nextInt();
if (toppings > 0) {
System.out
.println("What toppings you would like to have? Pepporoni/Sausage/Mushrooms : (Each topping should be separated by / ");
orderedToppings = scan.next();
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;
}

}

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

}

public void printPizzaOrder() {
for (int i = 0; i < pizzaOrders.size(); i++) {
Pizza p = pizzaOrders.get(i);
int temp = p.getNumOfToppings();
System.out.println("---------------------------------------------");
System.out.println("Size of Pizza = " + p.getSize());
System.out.println("No.of Toppings: " + temp);
if (temp > 0) {
if (p.pepporoni)
System.out.println("*Pepporoni");
if (p.sausage)
System.out.println("*Sausage");
if (p.mushrooms)
System.out.println("*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("********************************");
System.out.println("No. of Pizzas ordered : " + getNoOfPizzas());
System.out.println("Total Bill = " + total);
System.out.println("********************************");
}

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

public int getNoOfPizzas() {
return numOfPizzas;
}

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

public double getTotalCost() {
return total;
}

}

Output
--------
Enter the size of Pizza (s/m/l) :
s
No.of toppings you would like to have(0 to 3):
1
What toppings you would like to have? Pepporoni/Sausage/Mushrooms : (Each topping should be separated by /
pepporoni
Would you like to order one more pizza? y/n :
y
Enter the size of Pizza (s/m/l) :
l
No.of toppings you would like to have(0 to 3):
2
What toppings you would like to have? Pepporoni/Sausage/Mushrooms : (Each topping should be separated by /
sausage/mushrooms
Would you like to order one more pizza? y/n :
n
---------------------------------------------
Size of Pizza = s
No.of Toppings: 1
*Pepporoni
Cost of Pizza: $9.9
Cost of Toppings : $1.0
Total cost of Pizza: $10.9
---------------------------------------------
Size of Pizza = l
No.of Toppings: 2
*Sausage
*Mushrooms
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
********************************

Regards,
javaquest2010.



October 18, 2011 at 4:25 PM


Why i cannot run this program...please help



Related Tutorials/Questions & Answers:
Advertisements
Advertisements
 

 

 

DMCA.com