update,
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
/**
|
||||
*
|
||||
* @author jerrykwok
|
||||
*/
|
||||
public class Assignment {
|
||||
private static Scanner input;
|
||||
private static ArrayList<CoffeeProduct> productList;
|
||||
private static StateManager stateMang;
|
||||
private static CommandHandler ch;
|
||||
|
||||
public static void main(String[] args) {
|
||||
input = new Scanner(System.in);
|
||||
productList = createList();
|
||||
stateMang = createStateManager();
|
||||
ch = createCommand();
|
||||
run();
|
||||
}
|
||||
|
||||
public static void run(){
|
||||
while (true) {
|
||||
System.out.println("Coffee Inventory Management System");
|
||||
ch.printCommandList();
|
||||
String cmd = input.nextLine();
|
||||
boolean result = ch.run(cmd);
|
||||
if (!result) {
|
||||
System.out.println("No Command Match");
|
||||
}
|
||||
System.out.println("");
|
||||
}
|
||||
}
|
||||
|
||||
public static CommandHandler createCommand() {
|
||||
CommandHandler ch = new CommandHandler(stateMang);
|
||||
ch.regCommand("a", new AddProductCommandFactory(input, productList));
|
||||
ch.regCommand("v", new ViewProductCommandFactory(input, productList));
|
||||
ch.regCommand("c", new CollectProductCommandFactory(input, productList, stateMang));
|
||||
ch.regCommand("s", new SendCoffeeProductCommandFactory(input, productList, stateMang));
|
||||
ch.regCommand("u", new UndoLastCommandFactory(stateMang));
|
||||
ch.regCommand("r", new RedoLastCommandFactory(stateMang));
|
||||
ch.regCommand("sl", new ViewUndoRedoListCommandFactory(stateMang));
|
||||
ch.regCommand("x", new ExitCommandFactory());
|
||||
return ch;
|
||||
}
|
||||
|
||||
public static StateManager createStateManager() {
|
||||
return new StateManager();
|
||||
}
|
||||
|
||||
private static ArrayList<CoffeeProduct> createList(){
|
||||
return new ArrayList<CoffeeProduct>();
|
||||
}
|
||||
}
|
@@ -0,0 +1,51 @@
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
/**
|
||||
*
|
||||
* @author jerrykwok
|
||||
*/
|
||||
public class Caretaker {
|
||||
|
||||
private Stack<CoffeeProductMemento> undoList;
|
||||
private Stack<CoffeeProductMemento> redoList;
|
||||
|
||||
public Caretaker() {
|
||||
undoList = new Stack();
|
||||
redoList = new Stack();
|
||||
}
|
||||
|
||||
public void saveCoffeeProduct(CoffeeProductMemento xcoffeeProd) {
|
||||
redoList.clear();
|
||||
undoList.push(xcoffeeProd);
|
||||
}
|
||||
|
||||
public void undo() {
|
||||
if (undoList.isEmpty()) {
|
||||
System.out.println("UndoList is Empty");
|
||||
return;
|
||||
}
|
||||
CoffeeProductMemento obj = undoList.pop();
|
||||
|
||||
redoList.push(obj.getOrig().saveMemento());
|
||||
|
||||
obj.restore();
|
||||
}
|
||||
|
||||
public void redo() {
|
||||
if (redoList.isEmpty()) {
|
||||
System.out.println("RedoList is Empty");
|
||||
return;
|
||||
}
|
||||
CoffeeProductMemento obj = redoList.pop();
|
||||
undoList.push(obj.getOrig().saveMemento());
|
||||
obj.restore();
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @author jerrykwok
|
||||
*/
|
||||
public class CoffeeCandy extends CoffeeProduct {
|
||||
|
||||
private int noOfCandy;
|
||||
private int caloriesPerCandy;
|
||||
|
||||
public CoffeeCandy(String name, int productID, int noOfCandy, int caloriesPerCandy) {
|
||||
super(name, productID);
|
||||
this.noOfCandy = noOfCandy;
|
||||
this.caloriesPerCandy = caloriesPerCandy;
|
||||
}
|
||||
|
||||
public int getNoOfCandy() {
|
||||
return noOfCandy;
|
||||
}
|
||||
|
||||
public int getCaloriesPerCandy() {
|
||||
return caloriesPerCandy;
|
||||
}
|
||||
|
||||
public void setNoOfCandy(int noOfCandy) {
|
||||
this.noOfCandy = noOfCandy;
|
||||
}
|
||||
|
||||
public void setCaloriesPerCandy(int caloriesPerCandy) {
|
||||
this.caloriesPerCandy = caloriesPerCandy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return super.toString() + "Number of candies per package: " + getNoOfCandy() + "\n" + "Calories Per candy: " + getCaloriesPerCandy();
|
||||
}
|
||||
|
||||
public String printList() {
|
||||
return super.printList() +"\t"+ getQty()+ "\t\t" + getNoOfCandy() + " candy per package (" + getCaloriesPerCandy() + " calories each)";
|
||||
}
|
||||
|
||||
public CoffeeProductMemento saveMemento(){
|
||||
return new CoffeeCandyMemento(this);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @author jerrykwok
|
||||
*/
|
||||
public abstract class CoffeeProduct {
|
||||
|
||||
private String name;
|
||||
private int productID;
|
||||
private int qty;
|
||||
|
||||
public CoffeeProduct(String name, int productID) {
|
||||
this.name = name;
|
||||
this.productID = productID;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getProductID() {
|
||||
return productID;
|
||||
}
|
||||
|
||||
public int getQty() {
|
||||
return qty;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setProductID(int productID) {
|
||||
this.productID = productID;
|
||||
}
|
||||
|
||||
public void setQty(int qty) {
|
||||
this.qty = qty;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ID: " + getProductID() + "\nName: " + getName() + "\nQuantity: " + getQty() + "\n";
|
||||
}
|
||||
|
||||
public String printList() {
|
||||
return getProductID() + "\t" + getName() + "\t" ;
|
||||
}
|
||||
|
||||
public abstract CoffeeProductMemento saveMemento();
|
||||
|
||||
}
|
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @author jerrykwok
|
||||
*/
|
||||
public interface CoffeeProductMemento {
|
||||
public CoffeeProduct getOrig();
|
||||
public void restore();
|
||||
}
|
||||
|
||||
class CoffeeCandyMemento implements CoffeeProductMemento{
|
||||
private int noOfCandy;
|
||||
private int caloriesPerCandy;
|
||||
private int qty;
|
||||
private CoffeeCandy mcoffeeCandy;
|
||||
|
||||
public CoffeeCandyMemento(CoffeeCandy mcoffeeCandy) {
|
||||
this.noOfCandy = mcoffeeCandy.getNoOfCandy();
|
||||
this.caloriesPerCandy = mcoffeeCandy.getCaloriesPerCandy();
|
||||
this.qty = mcoffeeCandy.getQty();
|
||||
this.mcoffeeCandy = mcoffeeCandy;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public CoffeeProduct getOrig() {
|
||||
return mcoffeeCandy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void restore() {
|
||||
mcoffeeCandy.setCaloriesPerCandy(caloriesPerCandy);
|
||||
mcoffeeCandy.setNoOfCandy(noOfCandy);
|
||||
mcoffeeCandy.setQty(qty);
|
||||
}
|
||||
|
||||
|
||||
public String print() {
|
||||
return "CoffeeCandyMemento{" + "noOfCandy=" + noOfCandy + ", caloriesPerCandy=" + caloriesPerCandy + ", mcoffeeCandy=" + mcoffeeCandy + '}';
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
class CoffeeProwderMemento implements CoffeeProductMemento{
|
||||
|
||||
private double wegiht;
|
||||
private int qty;
|
||||
private CoffeeProwder mcoffeeProwder;
|
||||
|
||||
public CoffeeProwderMemento(CoffeeProwder mcoffeeProwder) {
|
||||
this.wegiht = mcoffeeProwder.getWegiht();
|
||||
this.qty = mcoffeeProwder.getQty();
|
||||
this.mcoffeeProwder = mcoffeeProwder;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public CoffeeProduct getOrig() {
|
||||
return mcoffeeProwder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void restore() {
|
||||
mcoffeeProwder.setWegiht(this.wegiht);
|
||||
mcoffeeProwder.setQty(qty);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @author jerrykwok
|
||||
*/
|
||||
public class CoffeeProwder extends CoffeeProduct {
|
||||
|
||||
private double wegiht;
|
||||
|
||||
public CoffeeProwder(String name, int productID, double wegiht) {
|
||||
super(name, productID);
|
||||
this.wegiht = wegiht;
|
||||
}
|
||||
|
||||
public double getWegiht() {
|
||||
return wegiht;
|
||||
}
|
||||
|
||||
public void setWegiht(double wegiht) {
|
||||
this.wegiht = wegiht;
|
||||
}
|
||||
|
||||
|
||||
public String toString() {
|
||||
return super.toString() + "Wegiht: " + getWegiht() + "\n";
|
||||
}
|
||||
|
||||
public String printList() {
|
||||
return super.printList() + "\t\t" + getQty() + "\t\t" + (int)(getWegiht()) + "g";
|
||||
}
|
||||
|
||||
public CoffeeProductMemento saveMemento(){
|
||||
return new CoffeeProwderMemento(this);
|
||||
}
|
||||
|
||||
}
|
423
_resources/it114105/itp4507/Assignment/19-20/src/Command.java
Normal file
423
_resources/it114105/itp4507/Assignment/19-20/src/Command.java
Normal file
@@ -0,0 +1,423 @@
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
import java.util.Stack;
|
||||
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
/**
|
||||
*
|
||||
* @author jerrykwok
|
||||
*/
|
||||
public abstract class Command {
|
||||
|
||||
public abstract boolean execute();
|
||||
}
|
||||
|
||||
interface UndoAllowCommand {
|
||||
|
||||
public void redo();
|
||||
public void undo();
|
||||
}
|
||||
|
||||
class AddProductCommand extends Command implements UndoAllowCommand {
|
||||
|
||||
private ArrayList productList;
|
||||
private Scanner input;
|
||||
CoffeeProduct coffeeProduct;
|
||||
|
||||
|
||||
public AddProductCommand(Scanner input, ArrayList productList) {
|
||||
this.input = input;
|
||||
this.productList = productList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute() {
|
||||
System.out.println("Enter Coffee type (cc=Coffee Candy/cp=Coffee Powder):");
|
||||
String type = input.nextLine();
|
||||
switch (type) {
|
||||
case "cc":
|
||||
|
||||
System.out.println("Enter product id, name, number of candy and calories per candy:");
|
||||
String ccValue = input.nextLine();
|
||||
String[] ccParameters = ccValue.split(", ");
|
||||
if(ccParameters.length==1)
|
||||
ccParameters = ccValue.split(",");
|
||||
if (ccParameters.length != 4) {
|
||||
System.out.println("Invalid parameter length");
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
System.out.println();
|
||||
int productId = Integer.parseInt(ccParameters[0]);
|
||||
String name = ccParameters[1];
|
||||
int noOfCandy = Integer.parseInt(ccParameters[2]);
|
||||
int caloriesPerCandy = Integer.parseInt(ccParameters[3]);
|
||||
if (checkProducrID(productList, productId)) {
|
||||
System.out.println("The id: " + productId + "is already exist");
|
||||
return false;
|
||||
}
|
||||
coffeeProduct = creatCoffeeProduct(name, productId, noOfCandy, caloriesPerCandy);
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.println("Invalid parameter: productID, Number of candy and Calories must be Integer");
|
||||
return false;
|
||||
}
|
||||
|
||||
break;
|
||||
case "cp":
|
||||
System.out.println("Enter product Id , name and weight(g):");
|
||||
String cpValue = input.nextLine();
|
||||
String[] cpParameters = cpValue.split(", ");
|
||||
if(cpParameters.length==1)
|
||||
cpParameters = cpValue.split(",");
|
||||
if (cpParameters.length != 3) {
|
||||
System.out.println("Invalid parameter length");
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
int productId = Integer.parseInt(cpParameters[0]);
|
||||
String name = cpParameters[1];
|
||||
int wegiht = Integer.parseInt(cpParameters[2]);
|
||||
if (checkProducrID(productList, productId)) {
|
||||
System.out.println("The id: " + productId + "is already exist");
|
||||
return false;
|
||||
}
|
||||
coffeeProduct = creatCoffeeProduct(name, productId, wegiht);
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.println("Invalid parameter: productID, Number of candy and Calories must be Integer");
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
System.out.println("Invalid Coffee Prodcut Type");
|
||||
return false;
|
||||
}
|
||||
productList.add(coffeeProduct);
|
||||
System.out.println("New product record created.\n");
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void redo() {
|
||||
|
||||
productList.add(coffeeProduct);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void undo() {
|
||||
productList.remove(coffeeProduct);
|
||||
}
|
||||
|
||||
|
||||
public static CoffeeProduct creatCoffeeProduct(String name, int productID, int noOfCandy, int caloriesPerCandy) {
|
||||
return new CoffeeCandy(name, productID, noOfCandy, caloriesPerCandy);
|
||||
}
|
||||
|
||||
public static CoffeeProduct creatCoffeeProduct(String name, int productID, int wegiht) {
|
||||
return new CoffeeProwder(name, productID, wegiht);
|
||||
}
|
||||
|
||||
public boolean checkProducrID(ArrayList<CoffeeProduct> productList, int id) {
|
||||
boolean invalid = false;
|
||||
for (CoffeeProduct product : productList) {
|
||||
if (product.getProductID() == id) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Add " + coffeeProduct.getProductID() +" " + coffeeProduct.getName();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
class ViewProductCommand extends Command {
|
||||
|
||||
private ArrayList<CoffeeProduct> productList;
|
||||
private Scanner input;
|
||||
|
||||
public ViewProductCommand(Scanner input, ArrayList productList) {
|
||||
this.input = input;
|
||||
this.productList = productList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute() {
|
||||
System.out.println("Enter product Id. (* to show all):");
|
||||
String id = input.nextLine();
|
||||
System.out.println("");
|
||||
if (id.equals("*")) {
|
||||
System.out.println("Coffee Product information");
|
||||
System.out.println("ID\tName\t\t\t\tQuantity\tOther Info");
|
||||
for (CoffeeProduct product : productList) {
|
||||
System.out.println(product.printList());
|
||||
}
|
||||
} else {
|
||||
|
||||
try {
|
||||
int productId = Integer.parseInt(id);
|
||||
System.out.println("Product information");
|
||||
boolean succe = false;
|
||||
for (CoffeeProduct product : productList) {
|
||||
if (product.getProductID() == productId) {
|
||||
System.out.println(product);
|
||||
succe = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!succe) {
|
||||
System.out.println("Not this ID: " + productId + " Product");
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.println("Invalid id, id must be integer only");
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
class CollectProductCommand extends Command implements UndoAllowCommand {
|
||||
|
||||
private ArrayList<CoffeeProduct> productList;
|
||||
private Scanner input;
|
||||
private StateManager state;
|
||||
private CoffeeProduct coffeeProduct;
|
||||
private int productQty;
|
||||
|
||||
public CollectProductCommand(Scanner input, ArrayList<CoffeeProduct> productList, StateManager state) {
|
||||
this.productList = productList;
|
||||
this.input = input;
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute() {
|
||||
int productId;
|
||||
|
||||
try {
|
||||
System.out.println("Enter product id:");
|
||||
String id = input.nextLine();
|
||||
productId = Integer.parseInt(id);
|
||||
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.println("Invalid id, id must be integer only");
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
System.out.println("Quantity to receive:");
|
||||
String qty = input.nextLine();
|
||||
productQty = Integer.parseInt(qty);
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.println("Invalid Quantity, Quantity must be integer only");
|
||||
return false;
|
||||
}
|
||||
boolean invalid = true;
|
||||
for (CoffeeProduct product : productList) {
|
||||
if (product.getProductID() == productId) {
|
||||
coffeeProduct = product;
|
||||
state.getCaretaker().saveCoffeeProduct(product.saveMemento());
|
||||
product.setQty(product.getQty() + productQty);
|
||||
System.out.println("Received " + productQty + " packs of " + product.getName() + ". Current quantity is " + product.getQty() + ".");
|
||||
invalid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (invalid) {
|
||||
System.out.println("No such Coffee Product");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void redo() {
|
||||
state.getCaretaker().redo();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void undo() {
|
||||
state.getCaretaker().undo();
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
return "Received " + productQty + " " +coffeeProduct.getName() + " ("+coffeeProduct.getProductID()+")";
|
||||
}
|
||||
}
|
||||
|
||||
class SendCoffeeProductCommand extends Command implements UndoAllowCommand {
|
||||
|
||||
private ArrayList<CoffeeProduct> productList;
|
||||
private Scanner input;
|
||||
private StateManager state;
|
||||
private CoffeeProduct coffeeProduct;
|
||||
private int shipQty;
|
||||
|
||||
public SendCoffeeProductCommand(Scanner input, ArrayList<CoffeeProduct> productList, StateManager state) {
|
||||
this.productList = productList;
|
||||
this.input = input;
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute() {
|
||||
int productId;
|
||||
|
||||
try {
|
||||
System.out.println("Enter product id:");
|
||||
String id = input.nextLine();
|
||||
productId = Integer.parseInt(id);
|
||||
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.println("Invalid id, id must be integer only");
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
System.out.println("Quantity to ship:");
|
||||
String qty = input.nextLine();
|
||||
shipQty = Integer.parseInt(qty);
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.println("Invalid Quantity, Quantity must be integer only");
|
||||
return false;
|
||||
}
|
||||
boolean succe = true;
|
||||
for (CoffeeProduct product : productList) {
|
||||
if (product.getProductID() == productId) {
|
||||
if ((product.getQty() - shipQty) < 0) {
|
||||
System.out.println("Invalid quantity (current balance is less than required quantity). Try again!!!");
|
||||
return false;
|
||||
}
|
||||
coffeeProduct = product;
|
||||
state.getCaretaker().saveCoffeeProduct(product.saveMemento());
|
||||
product.setQty(product.getQty() - shipQty);
|
||||
System.out.println("Shipped " + shipQty +" packs of Premium Coffee Candy. Current quantity is "+ product.getQty()+".");
|
||||
succe = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (succe) {
|
||||
System.out.println("No such Coffee Product");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void redo() {
|
||||
state.getCaretaker().redo();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void undo() {
|
||||
state.getCaretaker().undo();
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
return "Shipped " + shipQty + " " + coffeeProduct.getName() + " (" + coffeeProduct.getProductID()+ ")";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class UndoLastCommand extends Command {
|
||||
|
||||
private StateManager state;
|
||||
|
||||
public UndoLastCommand(StateManager state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute() {
|
||||
if (state.getUndoList().isEmpty()) {
|
||||
System.out.println("Nothing to undo");
|
||||
} else {
|
||||
Stack<UndoAllowCommand> UndoList = state.getUndoList();
|
||||
UndoAllowCommand undoCmd = UndoList.pop();
|
||||
state.getRedoList().push(undoCmd);
|
||||
undoCmd.undo();
|
||||
System.out.println("undo completed.");
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class RedoLastCommand extends Command {
|
||||
|
||||
private StateManager state;
|
||||
|
||||
public RedoLastCommand(StateManager state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute() {
|
||||
if (state.getRedoList().isEmpty()) {
|
||||
System.out.println("Nothing to Redo");
|
||||
} else {
|
||||
Stack<UndoAllowCommand> redoList = state.getRedoList();
|
||||
UndoAllowCommand redoCmd = redoList.pop();
|
||||
state.getUndoList().push(redoCmd);
|
||||
redoCmd.redo();
|
||||
System.out.println("Redo completed.");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
class ViewUndoRedoListCommand extends Command {
|
||||
|
||||
private StateManager state;
|
||||
|
||||
public ViewUndoRedoListCommand(StateManager state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute() {
|
||||
System.out.println("Undo List:");
|
||||
if (state.getUndoList().isEmpty()) {
|
||||
System.out.println("Empty");
|
||||
} else {
|
||||
state.getUndoList().forEach(System.out::println);
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
System.out.println("Redo List:");
|
||||
if (state.getRedoList().isEmpty()) {
|
||||
System.out.println("Empty");
|
||||
} else {
|
||||
state.getRedoList().forEach(System.out::println);
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class ExitCommand extends Command {
|
||||
|
||||
@Override
|
||||
public boolean execute() {
|
||||
System.out.println("Thanks for using Coffee Inventory Management System!!");
|
||||
System.exit(0);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,181 @@
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
/**
|
||||
*
|
||||
* @author jerrykwok
|
||||
*/
|
||||
public interface CommandFactory {
|
||||
|
||||
public String getCommandName();
|
||||
|
||||
public Command createCommand();
|
||||
}
|
||||
|
||||
class AddProductCommandFactory implements CommandFactory {
|
||||
|
||||
private Scanner sc;
|
||||
private ArrayList productList;
|
||||
|
||||
public AddProductCommandFactory(Scanner sc, ArrayList productList) {
|
||||
this.sc = sc;
|
||||
this.productList = productList;
|
||||
}
|
||||
|
||||
public String getCommandName() {
|
||||
return "add product, ";
|
||||
}
|
||||
|
||||
public Command createCommand() {
|
||||
return new AddProductCommand(sc, productList);
|
||||
}
|
||||
}
|
||||
|
||||
class ViewProductCommandFactory implements CommandFactory {
|
||||
|
||||
private Scanner sc;
|
||||
private ArrayList productList;
|
||||
|
||||
public ViewProductCommandFactory(Scanner sc, ArrayList productList) {
|
||||
this.sc = sc;
|
||||
this.productList = productList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCommandName() {
|
||||
return "view products, ";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Command createCommand() {
|
||||
return new ViewProductCommand(sc, productList);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class CollectProductCommandFactory implements CommandFactory {
|
||||
|
||||
private Scanner sc;
|
||||
private ArrayList productList;
|
||||
private StateManager state;
|
||||
|
||||
public CollectProductCommandFactory(Scanner sc, ArrayList productList, StateManager state) {
|
||||
this.sc = sc;
|
||||
this.productList = productList;
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCommandName() {
|
||||
return "collect product, ";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Command createCommand() {
|
||||
return new CollectProductCommand(sc, productList, state);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class SendCoffeeProductCommandFactory implements CommandFactory {
|
||||
|
||||
private Scanner sc;
|
||||
private ArrayList productList;
|
||||
private StateManager state;
|
||||
|
||||
public SendCoffeeProductCommandFactory(Scanner sc, ArrayList productList, StateManager state) {
|
||||
this.sc = sc;
|
||||
this.productList = productList;
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCommandName() {
|
||||
return "ship product, \n";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Command createCommand() {
|
||||
return new SendCoffeeProductCommand(sc, productList, state);
|
||||
}
|
||||
}
|
||||
|
||||
class UndoLastCommandFactory implements CommandFactory {
|
||||
|
||||
private StateManager state;
|
||||
|
||||
public UndoLastCommandFactory(StateManager state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCommandName() {
|
||||
return "undo, ";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Command createCommand() {
|
||||
return new UndoLastCommand(state);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class ViewUndoRedoListCommandFactory implements CommandFactory {
|
||||
|
||||
private StateManager state;
|
||||
|
||||
public ViewUndoRedoListCommandFactory(StateManager state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCommandName() {
|
||||
return "show list undo/redo, ";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Command createCommand() {
|
||||
return new ViewUndoRedoListCommand(state);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class RedoLastCommandFactory implements CommandFactory {
|
||||
|
||||
private StateManager state;
|
||||
|
||||
public RedoLastCommandFactory(StateManager state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCommandName() {
|
||||
return "redo, ";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Command createCommand() {
|
||||
return new RedoLastCommand(state);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class ExitCommandFactory implements CommandFactory {
|
||||
|
||||
@Override
|
||||
public String getCommandName() {
|
||||
return "exit system";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Command createCommand() {
|
||||
return new ExitCommand();
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,59 @@
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
/**
|
||||
*
|
||||
* @author jerrykwok
|
||||
*/
|
||||
public class CommandHandler {
|
||||
|
||||
private LinkedHashMap<String, CommandFactory> mapCmds;
|
||||
private StateManager state;
|
||||
|
||||
public CommandHandler(StateManager state) {
|
||||
this.mapCmds = new LinkedHashMap<String, CommandFactory>();
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public void regCommand(String key, CommandFactory cmdFactory) {
|
||||
mapCmds.put(key, cmdFactory);
|
||||
}
|
||||
|
||||
public void printCommandList() {
|
||||
System.out.print("Please enter command: [");
|
||||
System.out.print(String.join(" | ", mapCmds.keySet()));
|
||||
System.out.println("]");
|
||||
for (Map.Entry<String, CommandFactory> mapCmd : mapCmds.entrySet()) {
|
||||
System.out.print(mapCmd.getKey() + " = " + mapCmd.getValue().getCommandName());
|
||||
}
|
||||
System.out.println("\n");
|
||||
}
|
||||
|
||||
public boolean run(String cmd) {
|
||||
CommandFactory commandFactory = mapCmds.get(cmd);
|
||||
if (commandFactory != null) {
|
||||
Command command = commandFactory.createCommand();
|
||||
boolean result = command.execute();
|
||||
if (result) {
|
||||
switch (cmd) {
|
||||
case "a":
|
||||
case "c":
|
||||
case "s":
|
||||
state.getUndoList().add(command);
|
||||
state.getRedoList().clear();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,36 @@
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @author jerrykwok
|
||||
*/
|
||||
public class StateManager {
|
||||
private Stack redoList;
|
||||
private Stack undoList;
|
||||
private Caretaker caretaker;
|
||||
|
||||
|
||||
public StateManager() {
|
||||
undoList = new Stack<>();
|
||||
redoList = new Stack<>();
|
||||
caretaker = new Caretaker();
|
||||
}
|
||||
|
||||
public Caretaker getCaretaker(){
|
||||
return caretaker;
|
||||
}
|
||||
public Stack getUndoList() {
|
||||
return undoList;
|
||||
}
|
||||
|
||||
public Stack getRedoList() {
|
||||
return redoList;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user