Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This lab is focused on using Inheritance hierarchies in a Decorator design patte

ID: 3703401 • Letter: T

Question

This lab is focused on using Inheritance hierarchies in a Decorator design pattern. There is also a bit more polymorphism. . Finally, it utilizes the Factory pattern to make ice cream

Problem:

For this lab, you will implement the following decorator hierarchy:

Here are the requirements for each class, broken down:

IceCream is an interface

It must define a public method eat, which returns an IceCream object

IceCreamDecorator is an abstract class

It must implement IceCream

As in class, it must be set up to decorate other IceCream objects

It should have a toString method that returns its internal IceCream’s toString() and an eat method which returns the results of its internal IceCream

Four flavors must extend from IceCreamDecorator. I used Chocolate, Mint, Vanilla, and Butterscotch

Each decorate needs to add its flavor to the toString (example: adding Chocolate + the toString of the internal toString())

Each eat method needs

Report that that specific flavored ice cream scoop was eaten

Return the internal ice cream, removing the eaten scoop

BaseIceCream is the base class

The eat method should say the icecream is gone and return null

The toString should simply return “Icecream”

Additionally, you must write an IceCreamFactory class. It must provide the following functionality:

IceCreamFactory needs some way to store a list of supported flavors

IceCreamFactory should have a makeIceCream method which

Takes an Array or ArrayList of flavors, as Strings

Removes the unsupported flavors

Makes a new BaseIceCream

Adds only one of each supported flavor to the icecream cone, using decorators

That is, a Mint Chocolate Icecream is fine, but not a Mint Mint Icecream

Returns the new IceCream object

Finally, add a driver. This driver should have an ArrayList of IceCream, an IceCreamFactory object, and a main menu with the following operations:

Quit (Leave the Store, in my example) – exit the application

Add an icecream cone

Ask a user for a series of Strings to use as Flavors. Then, use the IceCreamFactory object to make a new ice cream cone and add it to the list of existing cones.

Eat an icecream cone

Ask the user for an index of an ice cream cone. If that cone does not exist, report the cone doesn’t exist. If the cone does, ‘eat’ the cone and reassign the cone without that scoop to the old cone’s position. If at this point the entire cone is gone, remove the null cone from the IceCream list

List the icecream cones

List all the icecream cones that currently exist

The Sample Output shows the flow for all menu options:

Sample Output:

Welcome to the ice cream shop!

---Menu---

(1) Eat an icecream cone

(2) List the icecream cones

(3) Add an icecream cone

(4) Leave the store

1

Enter the index of the ice cream cone you wish to devour

0

No such cone exists

---Menu---

(1) Eat an icecream cone

(2) List the icecream cones

(3) Add an icecream cone

(4) Leave the store

2

You have no icecream!

---Menu---

(1) Eat an icecream cone

(2) List the icecream cones

(3) Add an icecream cone

(4) Leave the store

3

Enter whatever flavors you like from the supported choices: Mint, Chocolate, Vanilla, and Butterscotch

Enter q to stop entering flavors

Mint

Mint

Mint

Mint

Chocolate

Rum

q

---Menu---

(1) Eat an icecream cone

(2) List the icecream cones

(3) Add an icecream cone

(4) Leave the store

3

Enter whatever flavors you like from the supported choices: Mint, Chocolate, Vanilla, and Butterscotch

Enter q to stop entering flavors

Chocolate

Vanilla

Banana

q

---Menu---

(1) Eat an icecream cone

(2) List the icecream cones

(3) Add an icecream cone

(4) Leave the store

2

Mint Chocolate Icecream

Vanilla Chocolate Icecream

---Menu---

(1) Eat an icecream cone

(2) List the icecream cones

(3) Add an icecream cone

(4) Leave the store

1

Enter the index of the ice cream cone you wish to devour

0

The mint scoop is eaten!

---Menu---

(1) Eat an icecream cone

(2) List the icecream cones

(3) Add an icecream cone

(4) Leave the store

1

Enter the index of the ice cream cone you wish to devour

0

The chocolate scoop is eaten!

---Menu---

(1) Eat an icecream cone

(2) List the icecream cones

(3) Add an icecream cone

(4) Leave the store

1

Enter the index of the ice cream cone you wish to devour

0

The icecream is all gone!

---Menu---

(1) Eat an icecream cone

(2) List the icecream cones

(3) Add an icecream cone

(4) Leave the store

2

Vanilla Chocolate Icecream

---Menu---

(1) Eat an icecream cone

(2) List the icecream cones

(3) Add an icecream cone

(4) Leave the store

4

You enter a sad, icecream less existence

IceCream IceCreamDecorator BaselceCream ChocolateFlavor OtherFlavor1 OtherFlavor2 OtherFlavor3

Explanation / Answer

package solution.icecreamparlour;

public class BaseIceCream implements IceCream {

@Override

public String toString() {

return "Icecream";

}

@Override

public IceCream eat() {

System.out.println("Eat base IceCream");

return this;

}

}

////////////////////////////

package solution.icecreamparlour;

public class ButterscotchDcorator extends IceCreamDecorator {

public ButterscotchDcorator(IceCream iceCream) {

super(iceCream);

// TODO Auto-generated constructor stub

}

@Override

public String toString() {

return " adding Butterscotch to " + super.toString() ;

}

@Override

public IceCream eat() {

System.out.println("Eating butterscotch scoop ");

return this.iceCream;

}

}

//////////////////////////////

package solution.icecreamparlour;

public class ChocolateDecorator extends IceCreamDecorator {

public ChocolateDecorator(IceCream iceCream) {

super(iceCream);

// TODO Auto-generated constructor stub

}

@Override

public IceCream eat() {

System.out.println("Eating Chocolate scoop ");

return this.iceCream;

}

@Override

public String toString() {

return " adding Chocolate to " + super.toString() ;

}

}

//////////////////////////////////

package solution.icecreamparlour;

public interface IceCream {

public IceCream eat();

}

///////////////////////

package solution.icecreamparlour;

public abstract class IceCreamDecorator implements IceCream {

protected IceCream iceCream;

public IceCreamDecorator(IceCream iceCream) {

this.iceCream = iceCream;

}

@Override

public String toString() {

return "New Base Icecream";

}

}

////////////////////////////////////////

package solution.icecreamparlour;

import java.util.HashSet;

import java.util.Iterator;

import java.util.List;

import java.util.Set;

public class IceCreamFactory {

String availableFlavours[] = { "BUTTERSCOTCH", "MINT", "CHOCOLATE",

"VANILLA" };

public static IceCream makeIcecream(List<String> flavours) {

IceCream newIceCream = new BaseIceCream();

if (flavours != null && flavours.size() > 0) {

Set<String> flavourSet = new HashSet<String>();

for (String s : flavours) {

flavourSet.add(s);

}

Iterator<String> iterator = flavourSet.iterator();

// check values

while (iterator.hasNext()) {

String flavour = iterator.next();

System.out.println("Value: " + flavour + " ");

newIceCream = getIcecreamDecorated(flavour, newIceCream);

}

}

return newIceCream;

}

public static IceCream getIcecreamDecorated(String flavour,

IceCream icecream) {

IceCream resultantIcecream = icecream;

switch (flavour) {

case "BUTTERSCOTCH":

System.out.println("BUTTERSCOTCH decorator ");

resultantIcecream = new ButterscotchDcorator(icecream);

break;

case "MINT":

System.out.println("MINT decorator ");

resultantIcecream = new MintDecorator(icecream);

break;

case "CHOCOLATE":

System.out.println("CHOCOLATE decorator ");

resultantIcecream = new ChocolateDecorator(icecream);

break;

case "VANILLA":

System.out.println("VANILLA decorator ");

resultantIcecream = new VanillaDecorator(icecream);

break;

default:

System.out.println("un available");

break;

}

return resultantIcecream;

}

}

////////////////////////////////////////////

package solution.icecreamparlour;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

public class IceCreamParlourDriver {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

String response = "";

IceCream cone = null;

do {

System.out.println("Welcome to the ice cream shop! ---Menu--");

System.out

.println("(1) Eat an icecream cone (2) List the icecream cones (3) Add an icecream cone (4) Leave the store");

response = sc.next();

List<String> flavours = new ArrayList<String>();

switch (response) {

case "1":

if (cone != null) {

cone.eat();

} else {

System.out.println("No such cone exists");

}

break;

case "2":

if (cone != null ) {

System.out.println("IceCream order placed with flavours: ");

for (String s : flavours) {

System.out.println(s);

}

} else {

System.out.println("You have no icecream! ");

}

break;

case "3":

System.out

.println("Enter whatever flavors you like from the supported choices: ");

System.out.println("Mint, Chocolate, Vanilla, and ButterScotch");

do {

response = sc.nextLine();

System.out.println("Enter q to stop entering flavors");

if (!"q".equals(response)) {

flavours.add(response.toUpperCase());

System.out.println("flavour added=>" + response);

}

} while (!"q".equals(response));

cone = IceCreamFactory.makeIcecream(flavours);

break;

default:

break;

}

} while (!"4".equals(response));

System.out.println("Left the store !! ");

}

}

///////////////////////////////////////////////

package solution.icecreamparlour;

public class MintDecorator extends IceCreamDecorator {

public MintDecorator(IceCream iceCream) {

super(iceCream);

// TODO Auto-generated constructor stub

}

@Override

public IceCream eat() {

System.out.println("Eating Mint scoop ");

return this.iceCream;

}

@Override

public String toString() {

return " adding Mint to " + super.toString() ;

}

}

/////////////////////////////////////

package solution.icecreamparlour;

public class VanillaDecorator extends IceCreamDecorator {

public VanillaDecorator(IceCream iceCream) {

super(iceCream);

// TODO Auto-generated constructor stub

}

@Override

public String toString() {

return " adding Vanilla to " + super.toString() ;

}

@Override

public IceCream eat() {

System.out.println("Eating Vanilla scoop ");

return this.iceCream;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote