SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Uml And Patterns
Exercise:
Programming Projects
Question:2 | ISBN:9780132830317 | Edition: 5

Question

The following UML diagram shows the relationship between a class called PizzaOrder and a class called Pizza:














The word “in” means the parameter is used to deliver data to the method.

The 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() method returns the size of the pizza and the getNumToppings() method returns a number from

0–3 depending on what toppings are present (e.g., if the pizza has pepperoni and mushrooms, it would be 2).

The PizzaOrder class contains an array of Pizza ’s. There is a method to add a new pizza to the array (which increments numPizzas ) and also a method to calculate the cost of the entire order. A small pizza costs $8, a medium pizza is $10, and a large pizza costs $12. Each topping adds $1 to the pizza.

The arrow connecting PizzaOrder to Pizza indicates that the PizzaOrder class has a reference to the Pizza class, but not vice versa. The solid diamond on the PizzaOrder class is a UML construct that indicates that the PizzaOrder class has a collection of the Pizza class. There may be many (*) Pizza ’s for a single (one) PizzaOrder.

Given this information, write Java code that implements the Pizza and PizzaOrder classes. Also, write a test main function that creates a pizza order, adds several pizzas to it, and outputs the cost of the order.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

public class Pizza 
{
	private char size;
	private boolean sausage;
	private boolean pepperoni;
	private boolean mushrooms;
	public Pizza(char size, boolean pepperoni,boolean sausage, boolean mushrooms)
	{
		this.size = size;
		this.pepperoni = pepperoni;
		this.sausage = sausage;
		this.mushrooms = mushrooms;
	}
	public char getSize()
	{
		return size;
	}
	public int getNumToppings()
	{
		int count = 0;
		if(pepperoni)
			count++;
		if(sausage)
			count++;
		if(mushrooms)
			count++;
		
		return count;
	}
}
//PizzaOrder.java
public class PizzaOrder 
{
	private final int MAXPIZZAS = 10;	
	private Pizza[] pizzas = new Pizza[MAXPIZZAS];
	int numPizzas = 0;
	public void addPizzaToOrder(char size,boolean pepperoni, boolean sausage,boolean mushrooms)
	{
		if(numPizzas != MAXPIZZAS)
		{
			pizzas[numPizzas] = new Pizza(size, pepperoni, sausage,mushrooms);
			numPizzas++;
		}
	}
	public double calcCost()
	{
		double total = 0;
		for(int i = 0; i < numPizzas; i++)
		{
			if(pizzas[i].getSize() == 's')
				total += 8;
			else if(pizzas[i].getSize() == 'm')
				total += 10;
			else
				total += 12;
			
			total += pizzas[i].getNumToppings();
		}

		return total;
	}
}

//PizzaDemo.java
public class PizzaDemo 
{
	public static void main(String args[])
	{
		PizzaOrder po = new PizzaOrder();
		po.addPizzaToOrder('s', true, true, false);
		po.addPizzaToOrder('m', false, true, false);
		po.addPizzaToOrder('l', true, true, true);
		po.addPizzaToOrder('s', true, false, false);
		System.out.println("Cost of the order: "+po.calcCost());
	}
}

Output of the program code:

Cost of the order: 45.0

 

0 0

Discussions

Post the discussion to improve the above solution.