0% found this document useful (0 votes)
80 views

Convert The Following Code Into Sequence Diagram.: Item String Packing

The document defines interfaces and classes for items in a meal, including burgers and drinks. It defines a Meal class to hold items and calculate cost, and a MealBuilder class to construct sample veg and non-veg meals. Main creates a MealBuilder, uses it to construct sample meals, and prints out the items and total cost of each meal.

Uploaded by

Wasie Urrahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Convert The Following Code Into Sequence Diagram.: Item String Packing

The document defines interfaces and classes for items in a meal, including burgers and drinks. It defines a Meal class to hold items and calculate cost, and a MealBuilder class to construct sample veg and non-veg meals. Main creates a MealBuilder, uses it to construct sample meals, and prints out the items and total cost of each meal.

Uploaded by

Wasie Urrahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Convert the following code into sequence diagram.

public interface Item {


public String name();
public Packing packing();
public float price();
}
------------------------------------------------------------
public interface Packing {
public String pack();
}
-------------------------------------------------------------
public class Wrapper implements Packing {

@Override
public String pack() {
return "Wrapper";
}
}
--------------------------------------------------------------
public class Bottle implements Packing {

@Override
public String pack() {
return "Bottle";
}
}
---------------------------------------------------------------
public abstract class Burger implements Item {

@Override
public Packing packing() {
return new Wrapper();
}

@Override
public abstract float price();
}
---------------------------------------------------------------
public abstract class ColdDrink implements Item {

@Override
public Packing packing() {
return new Bottle();
}

@Override
public abstract float price();
}
public class VegBurger extends Burger {

@Override
public float price() {
return 25.0f;
}

@Override
public String name() {
return "Veg Burger";
}
}
-----------------------------------------------------------------
public class ChickenBurger extends Burger {

@Override
public float price() {
return 50.5f;
}

@Override
public String name() {
return "Chicken Burger";

public class Coke extends ColdDrink {

@Override
public float price() {
return 30.0f;
}

@Override
public String name() {
return "Coke";
}
}
----------------------------------------------------------------
public class Pepsi extends ColdDrink {

@Override
public float price() {
return 35.0f;
}

@Override
public String name() {
return "Pepsi";
}
}
-----------------------------------------------------------
public class Meal {
private List<Item> items = new ArrayList<Item>();

public void addItem(Item item){


items.add(item);
}

public float getCost(){


float cost = 0.0f;

for (Item item : items) {


cost += item.price();
}
return cost;
}

public void showItems(){

for (Item item : items) {


System.out.print("Item : " + item.name());
System.out.print(", Packing : " + item.packing().pack());
System.out.println(", Price : " + item.price());
}
}
}
-----------------------------------------------------------------
public class MealBuilder {

public Meal prepareVegMeal (){


Meal meal = new Meal();
meal.addItem(new VegBurger());
meal.addItem(new Coke());
return meal;
}

public Meal prepareNonVegMeal (){


Meal meal = new Meal();
meal.addItem(new ChickenBurger());
meal.addItem(new Pepsi());
return meal; }
-------------------------------------------------------------

public class BuilderPatternDemo {


public static void main(String[] args) {

MealBuilder mealBuilder = new MealBuilder();

Meal vegMeal = mealBuilder.prepareVegMeal();


System.out.println("Veg Meal");
vegMeal.showItems();
System.out.println("Total Cost: " + vegMeal.getCost());

Meal nonVegMeal = mealBuilder.prepareNonVegMeal();


System.out.println("\n\nNon-Veg Meal");
nonVegMeal.showItems();
System.out.println("Total Cost: " + nonVegMeal.getCost());
}
}
------------------------------------------------------------------

You might also like