CECS 277
LAB ASSIGNMENT 7
Assigned date: 11/27
Due date: 12/6
40 points
1. Visitor pattern
Design a ordering system application.
The customer can order the following items:
Write a main method that accepts the customer orders, displays the orders and the total cost of all the orders.
2. Decorator pattern
Design a simple pizza and use three different types of toppings (pepperoni, sausage, and extra cheese) in any order the customers wants.
3. State pattern
4. Command pattern
//class Stock
public class Stock {
private String name;
private double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public void buy(int quantity){
System.out.println(“BOUGHT: “ + quantity + “x “ + this);
}
public void sell(int quantity){
System.out.println(“SOLD: “ + quantity + “x “ + this);
}
public String toString() {
return “Product [name=” + name + “, price=” + price + “]”;
}
}
a. Create two command classes that allows the customer to buy and sold stocks.
b. Write a main method to test it.
5. Adapter pattern
//interface NameInterface
interface NameInterface {
public void setName(String n);
public String getName();
}
//Adaptee class
class SimpleName implements NameInterface {
String name;
public void setName(String n) {
name = n;
}
public String getName() {
return name;
}
}
//Target interface
interface FullNameInterface {
public void setFirstName(String f);
public void setLastName(String l);
public String getFirstName();
public String getLastName();
}
a. Create an adapter to convert the interface NameInterface to FullNameInterface
b. Write a main method test the adapter.
6. Memento pattern
Design a simple calculator that allows to restores the previous additon operation of two numbers.