This commit is contained in:
louiscklaw
2025-01-31 19:15:17 +08:00
parent 09adae8c8e
commit 6c60a73f30
1546 changed files with 286918 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
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<LunchSetMemento> undoList;
public Caretaker() {
undoList = new Stack();
}
public void saveLunchSet(LunchSetMemento lunchSetMemento) {
undoList.push(lunchSetMemento);
}
public void undo() {
if (undoList.isEmpty()) {
System.out.println("UndoList is Empty");
return;
}
LunchSetMemento obj = undoList.pop();
obj.restore();
}
}

View File

@@ -0,0 +1,45 @@
/*
* 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 ChineseStyleLunchSet extends LunchSet{
public ChineseStyleLunchSet(String mainDish, int price, int availableCount) {
super(mainDish, price, availableCount);
}
public static String getDrink(String drinkCode){
String drink = "";
switch(drinkCode){
case "h":
drink = "Hot Tea";
break;
case "i":
drink = "Iced Tea";
}
return drink;
}
public String getSideDish(){
return "rice, Chinese soup, Chinese tea";
}
public String getDetails(){
return "Chinese style Business Set Lunch\n" +
"main dish: " + super.getMainDish() + "\n"
+ "with " + getSideDish() + "\n" +
"price: " + super.getPrice() + "\n"
+ "available count: " + super.getAvailableCount();
}
public LunchSetChineseStyleMemento saveMemento(){
return new LunchSetChineseStyleMemento(this);
}
}

View File

@@ -0,0 +1,30 @@
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 CmdCancelOrder implements Command{
private Scanner input;
public CmdCancelOrder(Scanner input) {
this.input = input;
}
@Override
public void execute() {
System.out.println("Cancel Order");
System.out.print("Staff Number: ");
String staffNumber = input.nextLine();
StaffCanteen.getInstance().cancelOrder(staffNumber);
}
}

View File

@@ -0,0 +1,42 @@
/*
* 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
*/
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class CmdEditMenu implements Command{
private Scanner input;
public CmdEditMenu(Scanner input){
this.input = input;
}
public void execute() {
try {
System.out.println("\nEdit Menu");
System.out.print("Chinese or Western (c | w): ");
String typeLunchSet = input.nextLine();
System.out.print("Enter main dish: ");
String mainDish = input.nextLine();
System.out.print("Enter price: ");
int price = Integer.parseInt(input.nextLine());
System.out.print("Enter available count: ");
int available = Integer.parseInt(input.nextLine());
StaffCanteen.getInstance().editMenu(typeLunchSet, mainDish, price, available);
System.out.println("Menu Updated");
} catch (LunchSetStyleException ex) {
System.out.println(ex.getMessage());
}
}
}

View File

@@ -0,0 +1,17 @@
/*
* 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 CmdExit implements Command {
@Override
public void execute() {
System.exit(0);
}
}

View File

@@ -0,0 +1,18 @@
/*
* 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 CmdListOrder implements Command{
@Override
public void execute() {
StaffCanteen.getInstance().listOrder();
}
}

View File

@@ -0,0 +1,15 @@
/*
* 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 CmdOrderIsDone implements Command{
public void execute(){
StaffCanteen.getInstance().orderDone();
}
}

View File

@@ -0,0 +1,63 @@
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
* 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 CmdPlaceOrder implements Command {
private Scanner input;
public CmdPlaceOrder(Scanner input) {
this.input = input;
}
@Override
public void execute() {
try {
System.out.println("\nPlace Order");
System.out.print("Chinese or Western (c | w): ");
String menuType = input.nextLine();
if(StaffCanteen.getInstance().searchMenuAvaiable(menuType) == 0){
System.out.println("Sold Out!");
return;
}
String drinkCode;
String sideCode = null;
String staffNumber;
String officeLocation;
switch (menuType) {
case "c":
System.out.print("Oolong Tea: hot or iced (h | i): ");
drinkCode = input.nextLine();
break;
case "w":
System.out.print("side: rice, spaghetti, French fries (r | s | f): ");
sideCode = input.nextLine();
System.out.print("tea or coffee, hot or iced (ht| it | hc | ic): ");
drinkCode = input.nextLine();
break;
default:
throw new LunchSetStyleException();
}
System.out.print("Staff Number: ");
staffNumber = input.nextLine();
System.out.print("Office Location: ");
officeLocation = input.nextLine();
Order order = (menuType.equals("c")) ? StaffCanteen.getInstance().placeChineseLunchSetOrder(drinkCode, staffNumber, officeLocation)
: StaffCanteen.getInstance().placeWesternLunchSetOrder(drinkCode, sideCode, staffNumber, officeLocation);
System.out.println("Order Placed");
} catch (LunchSetStyleException ex) {
System.out.println(ex.getMessage());
}
}
}

View File

@@ -0,0 +1,15 @@
/*
* 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 CmdShowMenu implements Command{
public void execute(){
StaffCanteen.getInstance().printMenu();
}
}

View File

@@ -0,0 +1,13 @@
/*
* 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 Command {
public void execute();
}

View File

@@ -0,0 +1,134 @@
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 getCommnadDesc();
public Command createCommand();
}
class EditMenuCommandFactory implements CommandFactory{
private Scanner input;
public EditMenuCommandFactory(Scanner input){
this.input = input;
}
@Override
public String getCommnadDesc() {
return "Edit menu, ";
}
@Override
public Command createCommand() {
return new CmdEditMenu(input);
}
}
class ShowMenuCommandFactory implements CommandFactory{
@Override
public String getCommnadDesc() {
return "Show menu, ";
}
@Override
public Command createCommand() {
return new CmdShowMenu();
}
}
class PlaceOrderCommandFactory implements CommandFactory{
private Scanner input;
public PlaceOrderCommandFactory(Scanner input) {
this.input = input;
}
@Override
public String getCommnadDesc() {
return "Place order, ";
}
@Override
public Command createCommand() {
return new CmdPlaceOrder(input);
}
}
class CancelOrderCommandFactory implements CommandFactory{
private Scanner input;
public CancelOrderCommandFactory(Scanner input) {
this.input = input;
}
@Override
public String getCommnadDesc() {
return "Cancel order, ";
}
@Override
public Command createCommand() {
return new CmdCancelOrder(input);
}
}
class ListOrdersCommandFactory implements CommandFactory{
@Override
public String getCommnadDesc() {
return "List orders, ";
}
@Override
public Command createCommand() {
return new CmdListOrder();
}
}
class OrderIsDoneCommandFactory implements CommandFactory{
@Override
public String getCommnadDesc() {
return "order is Done, ";
}
@Override
public Command createCommand() {
return new CmdOrderIsDone();
}
}
class ExitCommandFactory implements CommandFactory{
@Override
public String getCommnadDesc() {
return "Quit";
}
@Override
public Command createCommand() {
return new CmdExit();
}
}

View File

@@ -0,0 +1,47 @@
import java.util.LinkedHashMap;
import java.util.Map;
/*
* 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;
public CommandHandler() {
this.mapCmds = new LinkedHashMap<String, CommandFactory>();
}
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().getCommnadDesc());
}
System.out.println();
}
public boolean run(String cmd) {
CommandFactory commandFactory = mapCmds.get(cmd);
if (commandFactory != null) {
Command command = commandFactory.createCommand();
command.execute();
return true;
}
return false;
}
}

View File

@@ -0,0 +1,54 @@
/*
* 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 LunchSet {
private String mainDish;
private int price;
private int availableCount;
public LunchSet(String mainDish, int price, int availableCount) {
this.mainDish = mainDish;
this.price = price;
this.availableCount = availableCount;
}
public String getMainDish() {
return mainDish;
}
public int getPrice() {
return price;
}
public int getAvailableCount() {
return availableCount;
}
public void setAvailableCount(int availableCount){
this.availableCount = availableCount;
}
public void setMainDish(String mainDish) {
this.mainDish = mainDish;
}
public void setPrice(int price) {
this.price = price;
}
public void reduceOneAvailableCount(){
availableCount--;
}
public abstract String getSideDish();
public abstract String getDetails();
public abstract LunchSetMemento saveMemento();
}

View File

@@ -0,0 +1,35 @@
/*
* 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 LunchSetChineseStyleMemento implements LunchSetMemento{
private String mainDish;
private int price;
private int availableCount;
private LunchSet lunchSet;
public LunchSetChineseStyleMemento(ChineseStyleLunchSet lunchSet){
this.mainDish = lunchSet.getMainDish();
this.price = lunchSet.getPrice();
this.availableCount = lunchSet.getAvailableCount();
this.lunchSet = lunchSet;
}
@Override
public LunchSet getOrig() {
return lunchSet;
}
@Override
public void restore() {
lunchSet.setAvailableCount(availableCount);
}
}

View File

@@ -0,0 +1,14 @@
/*
* 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 LunchSetMemento {
public LunchSet getOrig();
public void restore();
}

View File

@@ -0,0 +1,15 @@
/*
* 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 LunchSetStyleException extends Exception{
public LunchSetStyleException(){
super("Please input c or w");
}
}

View File

@@ -0,0 +1,36 @@
/*
* 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 LunchSetWesternStyleMemento implements LunchSetMemento{
private String mainDish;
private int price;
private int availableCount;
private LunchSet lunchSet;
public LunchSetWesternStyleMemento(WesternStyleLunchSet lunchSet) {
this.mainDish = lunchSet.getMainDish();
this.price = lunchSet.getPrice();
this.availableCount = lunchSet.getAvailableCount();
this.lunchSet = lunchSet;
}
@Override
public LunchSet getOrig() {
return lunchSet;
}
@Override
public void restore() {
lunchSet.setAvailableCount(availableCount);
}
}

View File

@@ -0,0 +1,48 @@
/*
* 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
*/
import java.util.Scanner;
public class Main {
private static Scanner input;
private static CommandHandler ch;
public static void main(String[] args){
input = new Scanner(System.in);
ch = initCommand();
run();
}
public static void run(){
while (true) {
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 initCommand() {
CommandHandler ch = new CommandHandler();
ch.regCommand("e", new EditMenuCommandFactory(input));
ch.regCommand("s", new ShowMenuCommandFactory());
ch.regCommand("p", new PlaceOrderCommandFactory(input));
ch.regCommand("c", new CancelOrderCommandFactory(input));
ch.regCommand("l", new ListOrdersCommandFactory());
ch.regCommand("d", new OrderIsDoneCommandFactory());
ch.regCommand("q", new ExitCommandFactory());
return ch;
}
}

View File

@@ -0,0 +1,35 @@
/*
* 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 Order {
private String staffNumber;
private String officeLocation;
public Order(String staffNumber, String officeLocation) {
this.staffNumber = staffNumber;
this.officeLocation = officeLocation;
}
public String getStaffNumber() {
return staffNumber;
}
public String getOfficeLocation() {
return officeLocation;
}
public String toSting(){
return getStaffNumber() + ", " + getOfficeLocation() + ", ";
}
public abstract String getOrderDetail();
}

View File

@@ -0,0 +1,25 @@
/*
* 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 OrderChineseLunchSet extends Order {
private String drinkCode;
private LunchSet lunchset;
public OrderChineseLunchSet(String drinkCode, String staffNumber, String officeLocation, LunchSet lunchSet) {
super(staffNumber, officeLocation);
this.lunchset = lunchSet;
this.drinkCode = drinkCode;
}
public String getOrderDetail(){
return "C: " + super.toSting() + lunchset.getMainDish() + ", Chinese Soup, " + ChineseStyleLunchSet.getDrink(drinkCode);
}
}

View File

@@ -0,0 +1,32 @@
/*
* 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 OrderWesternLunchSet extends Order{
private String sideCode;
private String drinkCode;
private LunchSet lunchSet;
public OrderWesternLunchSet(String drinkCode, String sideCode, String staffNumber, String officeLocation, LunchSet lunchSet) {
super(staffNumber, officeLocation);
this.sideCode = sideCode;
this.drinkCode = drinkCode;
this.lunchSet = lunchSet;
}
public String getSideCode(){
return sideCode;
}
public String getOrderDetail(){
return "W: " + super.toSting() + lunchSet.getMainDish()+ ", " + WesternStyleLunchSet.getSide(sideCode) + ", Wetern Soup, " + WesternStyleLunchSet.getDrink(drinkCode);
}
}

View File

@@ -0,0 +1,22 @@
/*
* 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
*/
import java.util.ArrayList;
import java.util.Stack;
import java.util.List;
public abstract class RecordCommand implements Command{
public RecordCommand(){
}
}

View File

@@ -0,0 +1,119 @@
/*
* 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
*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
public class StaffCanteen {
private static HashMap<String, LunchSet> menuList;
private static ArrayList<Order> orderList;
private static StaffCanteen instance = new StaffCanteen();
private static Caretaker caretaker;
private StaffCanteen() {
menuList = new HashMap<>();
orderList = new ArrayList<>();
caretaker = new Caretaker();
}
public static StaffCanteen getInstance() {
return instance;
}
public int searchMenuAvaiable(String menuCode) {
return menuList.get(menuCode).getAvailableCount();
}
public LunchSet editMenu(String styleMenu, String mainDish, int price, int availabe) throws LunchSetStyleException {
LunchSet lunchSet = null;
switch (styleMenu) {
case "c":
lunchSet = new ChineseStyleLunchSet(mainDish, price, availabe);
break;
case "w":
lunchSet = new WesternStyleLunchSet(mainDish, price, availabe);
break;
default:
throw new LunchSetStyleException();
}
menuList.put(styleMenu, lunchSet);
return lunchSet;
}
public void printMenu() {
menuList.keySet().forEach((menuKey) -> {
System.out.println();
System.out.println(menuList.get(menuKey).getDetails());
});
}
public Order placeChineseLunchSetOrder(String drinkCode, String staffNumber, String officeLocation) {
LunchSet chineseLunchset = menuList.get("c");
OrderChineseLunchSet chineseLunchSetOrder = new OrderChineseLunchSet(drinkCode, staffNumber, officeLocation, chineseLunchset);
orderList.add(chineseLunchSetOrder);
saveLunchSet(chineseLunchset);
chineseLunchset.reduceOneAvailableCount();
return chineseLunchSetOrder;
}
public Order placeWesternLunchSetOrder(String drinkCode, String sideCode, String staffNumber, String officeLocation) {
LunchSet westernLunchset = menuList.get("w");
OrderWesternLunchSet westernLunchSetOrder = new OrderWesternLunchSet(drinkCode, sideCode, staffNumber, officeLocation, westernLunchset);
orderList.add(westernLunchSetOrder);
saveLunchSet(westernLunchset);
westernLunchset.reduceOneAvailableCount();
return westernLunchSetOrder;
}
private void saveLunchSet(LunchSet lunchSet) {
caretaker.saveLunchSet(lunchSet.saveMemento());
}
public void listOrder() {
System.out.println();
System.out.println("Outstanding Orders");
orderList.forEach((order) -> {
System.out.println(order.getOrderDetail());
});
}
public Order orderDone() {
if (orderList.isEmpty()) {
System.out.println("No Order");
return null;
}
Order order = orderList.remove(0);
System.out.println("\nComplete Order");
System.out.println(order.getOrderDetail());
System.out.println("Order marked as done");
return order;
}
public void cancelOrder(String staffNumber) {
Order removeOrder = null;
Iterator<Order> i = orderList.iterator();
while (i.hasNext()) {
Order order = i.next();
if (order.getStaffNumber().equals(staffNumber)) {
removeOrder = order;
i.remove();
}
}
System.out.println(removeOrder.getOrderDetail().substring(3));
caretaker.undo();
System.out.println("Order Cancelled");
}
}

View File

@@ -0,0 +1,58 @@
/*
* 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 WesternStyleLunchSet extends LunchSet {
public WesternStyleLunchSet(String mainDish, int price, int availableCount) {
super(mainDish, price, availableCount);
}
public String getSideDish() {
return "rice/spaghetti/French fries";
}
public static String getDrink(String drinkCode) {
String drink = "";
if (drinkCode.charAt(0) == 'i') {
drink = "Iced ";
} else {
drink = "Hot ";
}
if (drinkCode.charAt(1) == 't') {
drink += "Tea";
} else {
drink += "Coffee";
}
return drink;
}
public static String getSide(String sideCode) {
if (sideCode.equals("r")) {
return "rice";
} else if (sideCode.equals("s")) {
return "spaghetti";
} else {
return "French fries";
}
}
public String getDetails() {
return "Western style Business Set Lunch\n"
+ "main dish: " + super.getMainDish() + "\n"
+ "with " + getSideDish() + "\n"
+ "price: " + super.getPrice() + "\n"
+ "available count: " + super.getAvailableCount();
}
@Override
public LunchSetWesternStyleMemento saveMemento() {
return new LunchSetWesternStyleMemento(this);
}
}