Homework Help Question & Answers

JAVA PROGRAMMING***** Please provide screenshots of output and write a tester class to test. The ...

JAVA PROGRAMMING***** Please provide screenshots of output and write a tester class to test. The starter code has been provided below, just needs to be altered.

  • For this project you will implement the Memento Design Pattern.
  • To continue on with the food theme, you will work with Ice Cream Cones. You will need to use AdvancedIceCreamCone.java.
  • Create at least three Ice Cream Cones - one with chocolate ice cream, one with vanilla ice cream and one with strawberry ice cream. You can make your own choices for the other instance variables.
  • Save the state of these cones.
  • For the second part, ask the user which cone do they want: chocolate, vanilla, or strawberry or ....
  • Restore the ice cream cone to the state that the user selected.
  • Display the details of the ice cream cone that they chose.
/* This class is used to model the properties and behaviors of an ice cream cone.
   There are currently restriction on the construction of the cone: only one
   flavor of ice cream is allowed. */
import java.util.*;
public class AdvancedIceCreamCone {
        private int numberOfScoops;
        private String flavor;
        private String typeOfCone;
        private ArrayList<String> toppings = new ArrayList<String>();


//the default constructor creates a one scoop, vanilla ice cream cone using the regular type of cone and no toppings
        public AdvancedIceCreamCone() {
                numberOfScoops=1;
                flavor="vanilla";
                typeOfCone="regular";
        }
/*this constructor lets you create an ice cream code to your liking. It takes in three parameters:
  the number of scoops, the flavor of the ice cream and the type of cone */
        public AdvancedIceCreamCone(int ns,String flv,String cone) {
                numberOfScoops=ns;
                flavor=flv;
                typeOfCone=cone;
        }
//this method returns the number of scoops in the cone
        public int getNumberOfScoops () {
                return numberOfScoops;
        }
//this method returns the ice cream flavor
        public String getFlavor() {
                return flavor;
        }
//this method returns the type of cone
        public String getTypeOfCone() {
                return typeOfCone;
        }
//this method allows you to add one scoop of ice cream at a time
        public void addScoop() {
                numberOfScoops=numberOfScoops+1;
        }
//this method allows you to change the ice cream flavor
        public void setFlavor(String flv) {
                        flavor=flv;
        }
//this method allows you to change the type of cone
        public void setTypeOfCone(String cone) {
                typeOfCone=cone;
        }
//this method allows you to add  toppings
    public void addToppings(String top) {
                  toppings.add(top);
        }

//this method returns a String with a list of the toppings
        public ArrayList<String> getToppings () {
                return toppings;
        }

//this method overrides the inherited toString()
        public String toString() {
                return ("The number of scoops is " + numberOfScoops + ". The flavor is " +
                  flavor + ". And the type of cone is " + typeOfCone + " and the toppings are: " + getToppings());
          }

}
0 0
ReportAnswer #1

To implement the memento design, we use a helper class that stores and restores the states of the cone. The following program is implemented with two public classes (this will require two .java files).

1. AdvancedIceCreamCone.java

import java.util.*;

public class AdvancedIceCreamCone {

private int numberOfScoops;
private String flavor;
private String typeOfCone;
private ArrayList<String> toppings = new ArrayList<String>();

//the default constructor creates a one scoop, vanilla ice cream cone using the regular type of cone and no toppings
public AdvancedIceCreamCone() {
numberOfScoops = 1;
flavor = "vanilla";
typeOfCone = "regular";
}

/*this constructor lets you create an ice cream code to your liking. It takes in three parameters:
the number of scoops, the flavor of the ice cream and the type of cone */
public AdvancedIceCreamCone(int ns, String flv, String cone) {
numberOfScoops = ns;
flavor = flv;
typeOfCone = cone;
}
//this method returns the number of scoops in the cone

public int getNumberOfScoops() {
return numberOfScoops;
}
//this method returns the ice cream flavor

public String getFlavor() {
return flavor;
}
//this method returns the type of cone

public String getTypeOfCone() {
return typeOfCone;
}
//this method allows you to add one scoop of ice cream at a time

public void addScoop() {
numberOfScoops = numberOfScoops + 1;
}
//this method allows you to change the ice cream flavor

public void setFlavor(String flv) {
flavor = flv;
}
//this method allows you to change the type of cone

public void setTypeOfCone(String cone) {
typeOfCone = cone;
}
//this method allows you to add toppings

public void addToppings(String top) {
toppings.add(top);
}

//this method returns a String with a list of the toppings
public ArrayList<String> getToppings() {
return toppings;
}
  
public Memento saveToMemento()
{
System.out.println("Saving state to Memento.");
return new Memento(numberOfScoops, flavor, typeOfCone, toppings);
}
  
public void restoreFromMemento(Memento memento)
{
numberOfScoops = memento.getScoops();
flavor = memento.getFlavor();
typeOfCone = memento.getToc();
toppings = memento.getToppings();
System.out.println("State restored from Memento.");
}

//this method overrides the inherited toString()
public String toString() {
return ("The number of scoops is " + numberOfScoops + ". The flavor is "
+ flavor + ". And the type of cone is " + typeOfCone + " and the toppings are: " + getToppings());
}
  
public static class Memento {

private final int numberOfScoops;
private final String flavor;
private final String typeOfCone;
private ArrayList<String> toppings = new ArrayList<String>();

public Memento(int ns, String fl, String toc, ArrayList<String> tp) {
numberOfScoops = ns;
flavor = fl;
typeOfCone = toc;
toppings = new ArrayList<>(tp);
}

public int getScoops() {
return numberOfScoops;
}
  
public String getFlavor() {
return flavor;
}
  
public String getToc() {
return typeOfCone;
}
  
public ArrayList<String> getToppings() {
return toppings;
}
}

}

Note the new memento class added.

2. MementoDesign.java

import java.util.*;
public class MementoDesign {

public static void main(String[] args) {
  
List<AdvancedIceCreamCone.Memento> savedStates = new ArrayList<AdvancedIceCreamCone.Memento>();
  
AdvancedIceCreamCone chocolate = new AdvancedIceCreamCone(1,"chocolate","waffle");
AdvancedIceCreamCone vanilla = new AdvancedIceCreamCone();
AdvancedIceCreamCone strawberry = new AdvancedIceCreamCone(1,"strawberry","regular");
  
chocolate.addToppings("caramel");
savedStates.add(chocolate.saveToMemento());
  
vanilla.addToppings("cream");
savedStates.add(vanilla.saveToMemento());
  
strawberry.addToppings("caramel");
savedStates.add(strawberry.saveToMemento());
  
// Add another topping to chocolate
chocolate.addToppings("Cream");
System.out.println(chocolate.toString());
  
// Now restore to saved state
chocolate.restoreFromMemento(savedStates.get(0));
System.out.println(chocolate.toString());
  
// Add another topping to vanilla
vanilla.addToppings("caramel");
System.out.println(vanilla.toString());
  
// Now restore to saved state
vanilla.restoreFromMemento(savedStates.get(1));
System.out.println(vanilla.toString());
  
// Add another topping to strawberry
strawberry.addToppings("choco chips");
System.out.println(strawberry.toString());
  
// Now restore to saved state
strawberry.restoreFromMemento(savedStates.get(2));
System.out.println(strawberry.toString());
  
  
// Part 2
  
System.out.println("\nPart 2");
  
Scanner s = new Scanner(System.in);
System.out.println("\n\nEnter your ice cream cone flavor (chocolate, vanilla, or strawberry or ...): ");
String fl = s.next();
  
AdvancedIceCreamCone userCone = new AdvancedIceCreamCone(1,fl,"regular");
//Save this state
savedStates.add(userCone.saveToMemento());
  
// Now make changes
System.out.println("\nMaking changes to order");
userCone.addScoop();
userCone.addToppings("choco chips");
userCone.setTypeOfCone("waffle");
userCone.setFlavor("Mango");
  
// Print new state
System.out.println("After Modifications: " + userCone.toString());
  
// Restore to original state
userCone.restoreFromMemento(savedStates.get(3));
  
// Print again
System.out.println("Original Order:"+userCone.toString());
}
  
}

Output:

run: Saving state to Memento. Saving state to Memento. Saving state to Memento. The number of scoops is 1. The flavor is choc

There are a lot of comments to help you understand the code. All the best!

Add Homework Help Answer
Add Answer of:
JAVA PROGRAMMING***** Please provide screenshots of output and write a tester class to test. The ...
Your Answer: Your Name: What's your source?
Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
More Homework Help Questions Additional questions in this topic.
Need Online Homework Help?
Ask a Question
Get FREE EXPERT Answers
WITHIN MINUTES
Related Questions