Could someone help me with this Java assignment What you nee
Could someone help me with this Java assignment?
What you need to do:
(1) Create the following classes:
• Enumerated types for WrapType, ProteinType, CheeseType, and DressingType as
follows (these should be in separate class files)
public enum CheeseType {
Swiss, Provolone, Cheddar
}
public enum DressingType {
Sweet, Tangy, Spicy
}
public enum ProteinType {
Tofu, Turkey, Salami
}
public enum WrapType {
Wheat, Roll, Lettuce
}
• Sandwich, a concrete class with the following attributes
o wrap (of type WrapEnum)
o protein (of type ProteinEnum)
o cheese (of type CheeseEnum)
o dressing (of type DressingEnum)
o oil (boolean)
o vinegar (boolean)
o a toString() method that prints out what type of wrap, protein, cheese,
dressing, salt, pepper, oil, vinegar is on the sandwich
• SandwichDecorator, an interface with a single method:
public void decorate(Sandwich s);
• SandwichFactory, a singleton which has two methods
o getInstance() a static method that returns access to the SandwichFactory
singleton
o makeSandwich(String name) a factory method that accepts a string
(name of the sandwich) and returns a sandwich of a particular style
§ Acceptable names are
• Italian
o Salami, Roll, Tangy, Provolone
• Veggie
o Tofu, Lettuce, Spicy, Cheddar
• Tuscan
o Turkey, Wheat, Sweet, Swiss
(2) Create two decorator classes that implement the SandwichDecorator interface and
modify sandwiches accordingly
• SaltAndPepperDecorator
o decorate() sets salt & pepper to TRUE for Sandwich objects
• ItalianDecoratorDecorator
o decorate() sets oil & vinegar to TRUE for Sandwich objects
(3) Create a class called SandwichShop, which should contain the following code:
public class SandwichShop {
public static void main(String[] args) {
SandwichFactory f = SandwichFactory.getInstance();
Sandwich s1 = f.makeSandwich(\"Italian\");
Sandwich s2 = f.makeSandwich(\"Veggie\");
Sandwich s3 = f.makeSandwich(\"Tuscan\");
SandwichDecorator spd = new SaltPepperDecorator();
SandwichDecorator id = new ItalianDecorator();
id.decorate(s1);
id.decorate(s3);
spd.decorate(s2);
spd.decorate(s1);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}
Solution
Hi,
Please see below the Java classes . Please comment for any queries/feedbacks.
Thanks,
Anita
Sandwich.java
public class Sandwich {
private WrapType wrap;
private ProteinType protein;
private CheeseType cheese;
private DressingType dressing;
private boolean oil;
private boolean salt;
private boolean pepper;
private boolean vinegar;
public Sandwich(WrapType wrap,ProteinType protein,CheeseType cheese,DressingType dressing){
this.wrap =wrap;
this.protein =protein;
this.cheese =cheese;
this.dressing =dressing;
}
//toString method pto print all the details
public String toString(){
String outStr =\"Wrap : \"+this.wrap+\", Protein : \"+this.protein+\", Cheese : \"+this.cheese
+\"Dressing : \"+this.dressing ;
if(oil){
outStr = outStr + \", Oil \";
}
if(vinegar){
outStr = outStr + \", Vinegar \";
}
if(salt){
outStr = outStr + \", Salt \";
}
if(pepper){
outStr = outStr + \", Pepper \";
}
return outStr;
}
public WrapType getWrap() {
return wrap;
}
public void setWrap(WrapType wrap) {
this.wrap = wrap;
}
public ProteinType getProtein() {
return protein;
}
public void setProtein(ProteinType protein) {
this.protein = protein;
}
public CheeseType getCheese() {
return cheese;
}
public void setCheese(CheeseType cheese) {
this.cheese = cheese;
}
public DressingType getDressing() {
return dressing;
}
public void setDressing(DressingType dressing) {
this.dressing = dressing;
}
public boolean isOil() {
return oil;
}
public void setOil(boolean oil) {
this.oil = oil;
}
public boolean isPepper() {
return pepper;
}
public void setPepper(boolean pepper) {
this.pepper = pepper;
}
public boolean isVinegar() {
return vinegar;
}
public void setVinegar(boolean vinegar) {
this.vinegar = vinegar;
}
public boolean isSalt() {
return salt;
}
public void setSalt(boolean salt) {
this.salt = salt;
}
}
DressingType,java
public enum DressingType {
Sweet, Tangy, Spicy
}
CheeseType.java
public enum CheeseType {
Swiss, Provolone, Cheddar
}
ProteinType.java
public enum ProteinType {
Tofu, Turkey, Salami
}
WrapType.java
public enum WrapType {
Wheat, Roll, Lettuce
}
SandwichDecorator.java
public interface SandwichDecorator {
public void decorate(Sandwich s);
}
SandwichFactory.java
public class SandwichFactory {
private static SandwichFactory sandwichFacory;
public SandwichFactory() {
}
public static SandwichFactory getInstance(){
//to create a static singleton SandwichFactory object
if(sandwichFacory == null){
sandwichFacory = new SandwichFactory();
}
return sandwichFacory;
}
public Sandwich makeSandwich(String name){
//making sandwiches based on the name
Sandwich sandwich =null;
if(\"Italian\".equalsIgnoreCase(name)){
sandwich = new Sandwich(WrapType.Roll,ProteinType.Salami,CheeseType.Provolone,DressingType.Tangy);
}
else if(\"Veggie\".equalsIgnoreCase(name)){
sandwich = new Sandwich(WrapType.Lettuce,ProteinType.Tofu,CheeseType.Cheddar,DressingType.Spicy);
}
else if(\"Tuscan\".equalsIgnoreCase(name)){
sandwich = new Sandwich(WrapType.Wheat,ProteinType.Turkey,CheeseType.Swiss,DressingType.Sweet);
}
return sandwich;
}
}
SaltAndPepperDecorator.java
public class SaltAndPepperDecorator implements SandwichDecorator {
public SaltAndPepperDecorator() {
}
public void decorate(Sandwich s) {
//Setting salt and pepper options to true
s.setSalt(true);
s.setPepper(true);
}
}
ItalianDecorator.java
public class ItalianDecorator implements SandwichDecorator {
public ItalianDecorator() {
}
public void decorate(Sandwich s) {
//Setting oil and vinegar options to true
s.setOil(true);
s.setVinegar(true);
}
}
SandwichShop.java
public class SandwichShop {
public static void main(String[] args) {
//Main class - testing all the sandwhich functionalities
SandwichFactory f = SandwichFactory.getInstance();
Sandwich s1 = f.makeSandwich(\"Italian\");
Sandwich s2 = f.makeSandwich(\"Veggie\");
Sandwich s3 = f.makeSandwich(\"Tuscan\");
SandwichDecorator spd = new SaltAndPepperDecorator();
SandwichDecorator id = new ItalianDecorator();
id.decorate(s1); //Adding italian decorator to s1
id.decorate(s3); //Adding italian decorator to s3
spd.decorate(s2); //Adding Salt and Pepper decorator to s2
spd.decorate(s1); //Adding Salt and Pepper decorator to s1
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}
Sample output:
Wrap : Roll, Protein : Salami, Cheese : ProvoloneDressing : Tangy,Oil ,Vinegar ,Salt ,Pepper
Wrap : Lettuce, Protein : Tofu, Cheese : CheddarDressing : Spicy,Salt ,Pepper
Wrap : Wheat, Protein : Turkey, Cheese : SwissDressing : Sweet,Oil ,Vinegar





