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,3 @@
public interface AdminFunction {
public void execute();
}

View File

@@ -0,0 +1,18 @@
public class AdminPrintOrderList implements AdminFunction{
private LinkedList orders;
public AdminPrintOrderList(LinkedList orders){
this.orders = orders;
}
@Override
public void execute() {
// TODO Auto-generated method stub
System.out.println("--------------------------------------");
System.out.println(orders);
System.out.println("--------------------------------------");
System.out.println("Total outstanding order:" + orders.count());
}
}

View File

@@ -0,0 +1,17 @@
public class AdminRemoveOrder implements AdminFunction{
private int memberId;
private LinkedList orders;
public AdminRemoveOrder(int memberId, LinkedList orders) {
this.memberId = memberId;
this.orders = orders;
}
@Override
public void execute() {
// TODO Auto-generated method stub
orders.remove(memberId);
}
}

View File

@@ -0,0 +1,48 @@
public class FoodOrder {
private int memberID;
private String foodOrder; // A, B, C, or D
private int priority;
//constructor
public FoodOrder(int memberID){
this.memberID = memberID;
}
public FoodOrder(int memberID, String foodOrder) {
this.memberID = memberID;
this.foodOrder = foodOrder;
priority = 2;
}
//provide methods getter, setter, toString ....
public int getMemberID() {
return this.memberID;
}
public void setMemberID(int memberID) {
this.memberID = memberID;
}
public String getFoodOrder() {
return this.foodOrder;
}
public void setFoodOrder(String foodOrder) {
this.foodOrder = foodOrder;
}
public int getPriority() {
return this.priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
public String toString(){
return "[ MemberID: " + memberID + " ordered Set " + foodOrder + " with priority " + priority + " ]";
}
}

View File

@@ -0,0 +1,10 @@
public class InvalidInputException extends Exception{
public InvalidInputException(String mes){
super(mes);
}
public InvalidInputException(){
super("Invalid input! Please input again.");
}
}

View File

@@ -0,0 +1,193 @@
class ListNode {
private Object data;
private ListNode next;
public ListNode(Object o) { data = o; next = null; }
public ListNode(Object o, ListNode nextNode)
{ data = o; next = nextNode; }
public Object getData() { return data; }
public void setData(Object o) { data = o; }
public ListNode getNext() { return next; }
public void setNext(ListNode next) { this.next = next; }
} // class ListNode
class EmptyListException extends RuntimeException {
public EmptyListException ()
{ super("List is empty"); }
} // class EmptyListException
public class LinkedList {
private ListNode head;
private ListNode tail;
private int length; // the length of the list
public LinkedList() {
head = tail = null;
length = 0;
}
public boolean isEmpty() { return head == null; }
public void addToHead(Object item) {
if (isEmpty())
head = tail = new ListNode(item);
else
head = new ListNode(item, head);
length++;
}
public void addToTail(Object item) {
if (isEmpty())
head = tail = new ListNode(item);
else {
tail.setNext(new ListNode(item));
tail = tail.getNext();
}
length++;
}
public Object removeFromHead() throws EmptyListException {
Object item = null;
if (isEmpty())
throw new EmptyListException();
item = head.getData();
if (head == tail)
head = tail = null;
else
head = head.getNext();
length--;
return item;
}
public Object removeFromTail() throws EmptyListException {
Object item = null;
if (isEmpty())
throw new EmptyListException();
item = tail.getData();
if (head == tail)
head = tail = null;
else {
ListNode current = head;
while (current.getNext() != tail)
current = current.getNext();
tail = current;
current.setNext(null);
}
length--;
return item;
}
public int count() {
return length;
}
// students need to revise toString method
public String toString() {
String str = "";
ListNode current = head;
while (current != null) {
FoodOrder foodOrder = (FoodOrder) current.getData();
str += foodOrder + "\n";
current = current.getNext();
}
return str;
}
/**
Removes a ListNode from the LinkedList with a specific Member ID.
@param targetID the Member ID of the ListNode to be removed
@throws EmptyListException if the list is empty
*/
public void remove(int targetID) throws EmptyListException {
// Throw an exception if the list is empty
if (isEmpty()) {
throw new EmptyListException();
}
// If the target node is the head node, remove it from the head
if (((FoodOrder) head.getData()).getMemberID() == targetID) {
removeFromHead();
return;
}
// Traverse the linked list to find the target node
ListNode current = head.getNext();
ListNode prev = head;
while (current != null && ((FoodOrder) current.getData()).getMemberID() != targetID) {
prev = current;
current = current.getNext();
}
// If the target node is found, remove it
if (current != null) {
prev.setNext(current.getNext());
length--;
}
}
/**
Checks if the linked list contains a FoodOrder object with the given targetID as its member ID.
@param targetID the member ID to search for in the linked list
@return true if the linked list contains a FoodOrder object with the given targetID as its member ID, false otherwise
*/
public boolean contain(int targetID) {
ListNode current = head;
while (current != null) {
if (((FoodOrder) current.getData()).getMemberID() == targetID) {
return true;
}
current = current.getNext();
}
return false;
}
/**
Adds a new FoodOrder to the LinkedList in the correct position based on its priority.
@param item the FoodOrder to add to the LinkedList
*/
public void add(Object item) {
FoodOrder foodOrderItem = (FoodOrder) item;
// if the list is empty, add the new node as the head
if (isEmpty())
addToHead(item);
// if the new node has higher priority than the head, add it as the new head
else if (foodOrderItem.getPriority() < ((FoodOrder) head.getData()).getPriority())
addToHead(item);
// if the new node has lower priority than the tail, add it as the new tail
else if (foodOrderItem.getPriority() >= ((FoodOrder) tail.getData()).getPriority())
addToTail(item);
// otherwise, find the correct position for the new node and insert it
else {
ListNode current = head.getNext();
ListNode prev = head;
while (current != null && foodOrderItem.getPriority() >= ((FoodOrder) current.getData()).getPriority()) {
prev = current;
current = current.getNext();
}
prev.setNext(new ListNode(item, current));
length++;
}
}
}

View File

@@ -0,0 +1,16 @@
public class MenuItem {
private String food;
public MenuItem(String food){
this.food = food;
}
public String getMenuItemFood(){
return this.food;
}
public String toString(){
return food;
}
}

View File

@@ -0,0 +1,5 @@
public class NoneOfOrderException extends InvalidInputException {
public NoneOfOrderException(){
super("None of order");
}
}

View File

@@ -0,0 +1,153 @@
import java.util.Scanner;
public class OrderSystem {
private static Scanner sc;
private static LinkedList orders;
private static int nextGuestID = 9000;
private static MenuItem[] menus;
private static FoodOrder currentFoodOrder;
public static void main(String[] args) {
sc = new Scanner(System.in);
orders = new LinkedList();
regFoodMenu();
while (true) {
start();
}
}
private static void regFoodMenu() {
menus = new MenuItem[4];
menus[0] = new MenuItem("Chicken Salad");
menus[1] = new MenuItem("Grilled Ribeye Steak");
menus[2] = new MenuItem("Angel Hair Pasta with Shrimp");
menus[3] = new MenuItem("Grilled Fish and Potatoes");
}
public static void start() {
try {
int memberId = inputMemberId(false);
if (memberId <= -1) {
System.err.println("Have a nice day!!!");
System.exit(1);
}
if (memberId == 9999) {
adminFunc();
} else {
currentFoodOrder = new FoodOrder(memberId);
if (memberId == 0) {
memberId = nextGuestID++;
currentFoodOrder.setMemberID(memberId);
currentFoodOrder.setPriority(3);
} else if (memberId > 8000 && memberId < 8200)
currentFoodOrder.setPriority(1);
else if (memberId > 8199 && memberId < 9000)
currentFoodOrder.setPriority(2);
inputOrder();
}
} catch (InvalidInputException e) {
System.out.println(e.getMessage());
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static void printMenu() {
System.out.println("----------------- Food Menu ----------------");
for (int dec = 65, i = 0; i < menus.length; dec++)
System.out.println("Set " + (char) dec + " : " + menus[i++]);
System.out.println("--------------------------------------------");
}
public static void printAdminMenu() {
System.out.println("----------------- Admin Function ----------------");
System.out.println("1 : Print order list");
System.out.println("2 : Remove order");
}
public static int inputMemberId(boolean isAdmin) throws InvalidInputException {
try {
if(isAdmin){
System.out.print("Enter Member ID:");
int memberId = Integer.parseInt(sc.nextLine());
if ( memberId > 8000 && memberId < 9999)
return memberId;
}else{
System.out.print("Please input your member ID [input 0 for guest]:");
int memberId = Integer.parseInt(sc.nextLine());
if (memberId <= 0 || memberId == 9999 || memberId > 8000 && memberId < 8999)
return memberId;
}
} catch (NumberFormatException e) {
System.out.println("Input Error");
System.exit(1);
}
throw new InvalidInputException();
}
public static void inputOrder() throws InvalidInputException {
printMenu();
System.out.print("Select food:");
String foodOrder = sc.nextLine().toUpperCase();
if (!isValidFoodOrderChar(foodOrder))
throw new InvalidInputException();
currentFoodOrder.setFoodOrder(foodOrder);
orders.add(currentFoodOrder);
}
private static boolean isValidFoodOrderChar(String foodOrder) {
if (foodOrder.length() > 2 || foodOrder.length() == 0)
return false;
char value = foodOrder.charAt(0);
return (value >= 65 && value <= 65 + menus.length)? true: false;
}
public static void adminFunc() throws InvalidInputException {
try {
printAdminMenu();
System.out.print(">");
int adminFuncInput = Integer.parseInt(sc.nextLine());
if (!isValidAdminFunction(adminFuncInput))
throw new InvalidInputException();
if (adminFuncInput == 1)
new AdminPrintOrderList(orders).execute();
else if (adminFuncInput == 2) {
int memberId = inputMemberId(true);
if (!orders.contain(memberId))
throw new NoneOfOrderException();
new AdminRemoveOrder(memberId, orders).execute();
} else
throw new InvalidInputException();
} catch (NumberFormatException e) {
throw new InvalidInputException();
}
}
private static boolean isValidAdminFunction(int input) {
return (input == 1 || input == 2)? true: false;
}
}

View File

@@ -0,0 +1,278 @@
# ITP4510-Assignment
ITP4510 Data Structures &amp; Algorithms: Concepts &amp; Implementation
## Scenarios
The Yummy Restaurant Group Limited is a catering company. It has grown into one of the largest catering company in Hong Kong. The Group is operating diversified services including Chinese Restaurants, Western Restaurants, Japanese Restaurants, Conveyor-belt Sushi Restaurants, Fast Food Restaurants and etc. It has over 10 brands and around 100 restaurants in 2021.
The Novel House, which located in the Clover Hotel, is belonged to Yummy Restaurant Group Limited. For the Novel House, customer can place food order by phone. The operator will enter the order information in the system. For each of the food order, a priority indicator will be assigned to it. The least priority indicator of the food order will be first delivered, however, if the food orders with the same priority occurred, the food will be delivered according to their ordering in the queue.
Now, one IT staff is assigned to develop a prototype program to simulate the ordering of the food.
## Membership
List of Priority Indicator
| Membership | Priority Indicator |
| -- | -- |
| VIP Member | 1 |
| Registered Member | 2 |
| Guest | 3 |
Member ID is a four digit number starting with “8”. Member ID of VIP members should be started from 8001 to 8199. The Member ID of registered members should be in the range of 8200 to 8999. The Member ID for a guest would be an increment number started from 9000.
## Food Menu
| Set | Food |
| -- | -- |
| A | Chicken Salad |
| B | Grilled Ribeye Steak |
| C | Angel Hair Pasta with Shrimp |
| D | Grilled Fish and Potatoes |
## Execution Sample
```
_Number_ <- present the user input
```
### 1. Process Ordering
```
Please input your member ID [input 0 for guest]:_0_ ← Order by guest
----------------- Food Menu ----------------
Set A : Chicken Salad
Set B : Grilled Ribeye Steak
Set C : Angel Hair Pasta with Shrimp
Set D : Grilled Fish and Potatoes
--------------------------------------------
Select food:_a_
Please input your member ID [input 0 for guest]:_8101_ ← Order by VIP
----------------- Food Menu ----------------
Set A : Chicken Salad
Set B : Grilled Ribeye Steak
Set C : Angel Hair Pasta with Shrimp
Set D : Grilled Fish and Potatoes
--------------------------------------------
Select food:_b_
Please input your member ID [input 0 for guest]:_8103_ ← Order by VIP
----------------- Food Menu ----------------
Set A : Chicken Salad
Set B : Grilled Ribeye Steak
Set C : Angel Hair Pasta with Shrimp
Set D : Grilled Fish and Potatoes
--------------------------------------------
Select food:A
Please input your member ID [input 0 for guest]:_8299_ ← Order by registered member
----------------- Food Menu ----------------
Set A : Chicken Salad
Set B : Grilled Ribeye Steak
Set C : Angel Hair Pasta with Shrimp
Set D : Grilled Fish and Potatoes
--------------------------------------------
Select food:_d_
Please input your member ID [input 0 for guest]:_0_ ← Order by guest
----------------- Food Menu ----------------
Set A : Chicken Salad
Set B : Grilled Ribeye Steak
Set C : Angel Hair Pasta with Shrimp
Set D : Grilled Fish and Potatoes
--------------------------------------------
Select food:_a_
Please input your member ID [input 0 for guest]:_8233_ ← Order by registered member
----------------- Food Menu ----------------
Set A : Chicken Salad
Set B : Grilled Ribeye Steak
Set C : Angel Hair Pasta with Shrimp
Set D : Grilled Fish and Potatoes
--------------------------------------------
Select food:_B_
```
### 2. Print out Order List
```
Please input your member ID [input 0 for guest]: _9999_
----------------- Admin Function ----------------
1 : Print order list
2 : Remove order
>_1_
--------------------------------------
[ MemberID: 8101 ordered Set B with priority 1 ]
[ MemberID: 8103 ordered Set A with priority 1 ]
[ MemberID: 8299 ordered Set D with priority 2 ]
[ MemberID: 8233 ordered Set B with priority 2 ]
[ MemberID: 9000 ordered Set A with priority 3 ]
[ MemberID: 9001 ordered Set A with priority 3 ]
--------------------------------------
Total outstanding order:6
```
### 3. Delete Order
```
Please input your member ID [input 0 for guest]:_9999_
----------------- Admin Function ----------------
1 : Print order list
2 : Remove order
>_2_
Enter Member ID:8299
Please input your member ID [input 0 for guest]:_9999_
----------------- Admin Function ----------------
1 : Print order list
2 : Remove order
>_1_
--------------------------------------
[ MemberID: 8101 ordered Set B with priority 1 ]
[ MemberID: 8103 ordered Set A with priority 1 ]
[ MemberID: 8233 ordered Set B with priority 2 ]
[ MemberID: 9000 ordered Set A with priority 3 ]
[ MemberID: 9001 ordered Set A with priority 3 ]
--------------------------------------
Total outstanding order:5
```
### 4. Quit Program
```
Please input your member ID [input 0 for guest]:_-1_
Have a nice day!!!
```
Type any negative number in the main menu to quit the program.
## Validation for Input
You should create an exceptional class `InvalidInputException` for the program. During the ordering stage, the program should do the validation for the following cases:
1. Checking valid range of Member ID
2. Checking selection of food
3. Deleting of order
```
Please input your member ID [input 0 for guest]:_523_
Invalid input! Please input again.
Please input your member ID [input 0 for guest]:_7569_
Invalid input! Please input again.
Please input your member ID [input 0 for guest]:_9856_
Invalid input! Please input again.
Please input your member ID [input 0 for guest]:_0_
----------------- Food Menu ----------------
Set A : Chicken Salad
Set B : Grilled Ribeye Steak
Set C : Angel Hair Pasta with Shrimp
Set D : Grilled Fish and Potatoes
--------------------------------------------
Select food:_f_
Invalid input! Please input again.
Please input your member ID [input 0 for guest]:_9999_
----------------- Admin Function ----------------
1 : Print order list
2 : Remove order
>_2_
Enter Member ID:_8231_ ← This order is not exist in the LinkedList
None of order
```
Program would be stopped when entered non-number in the main menu.
```
Please input your member ID [input 0 for guest]:_Abcd_
Input Error
```
## Given Files
1. `OrderSystem.java` *main program which should be completed*
2. `LinkedList.java` *data structure which should be amended*
3. `FoodOrder.java` *data type for the data object stored in ListNode*
## Task Specification
1. You should use the classes provided in the given files.
2. Implement the program using Java. Submit listings of all programs.
3. Handle exceptional/abnormal cases. You should create your own Exception class. Submit a brief description of all such cases (e.g. invalid input ) handled by the program. A class InvalidInputException should be implemented.
4. Program structure and in-program comments.
5. Evidence of testing. Test the program and submit the logged listing of run samples.
## Mark Allocation
<table>
<tbody><tr>
<td>
<p>0. Compilation</p>
<p>-- success compilation and execute the program</p>
</td>
<td>
<p>10%</p>
</td>
</tr>
<tr>
<td>
<p>1. Design</p>
<p>-- correct using class <span >FoodOrder</span>, LinkedList,
<span >ListNode</span></p>
<p>-- suitable data
structure</p>
</td>
<td >
<p>10%</p>
</td>
</tr>
<tr style="mso-yfti-irow:2">
<td>
<p>2. Implementation</p>
<p>-- input parameter</p>
<p>-- program simulation</p>
<p>-- result print out</p>
</td>
<td >
<p>45%</p>
</td>
</tr>
<tr>
<td>
<p>3. Selection of menu</p>
<p>-- Correct menu design</p>
</td>
<td>
<p>10%</p>
</td>
</tr>
<tr>
<td>
<p>4. Error handling</p>
<p>-- create exception class InvalidRangeInputException</p>
<p>-- handle exception (e.g.InputMismatchException>)</p>
</td>
<td>
<p>10%</p>
</td>
</tr>
<tr>
<td>
<p>5. Report</p>
<p>-- executable results (screen dumps)</p>
</td>
<td>
<p>5%</p>
</td>
</tr>
<tr>
<td>
<p>6. Coding standard</p>
<p>-- proper indentation</p>
<p>-- proper naming</p>
<p>-- consistency coding style</p>
<p>-- appropriate comments</p>
</td>
<td>
<p>10%</p>
</td>
</tr>
<tr>
<td>
<p><b>Total</b></p>
</td>
<td>
<p>100%<o:p></b></p>
</td>
</tr>
</tbody></table>
## Instructions to Students
This assignment is an individual assignment. Each student has to submit his/her own work. Plagiarism will be treated seriously. All assignments that have been found involved wholly or partly in plagiarism (no matter these assignments are from the original authors or from the plagiarists) will score ZERO marks. Further, disciplinary action will be followed.
Adequate in-program comments should be placed as appropriate. All user-defined names should be descriptive as much as possible. Marks are given based on correctness, programming quality, and style.
You are required to submit:
- Well-documented program listings and the executable results (screen dumps).
- Upload your files to Moodle including all your programs and report. It is required that your programs can be successfully compile.

View File

@@ -0,0 +1,15 @@
public class FoodOrder {
private int memberID;
private String foodOrder; // A, B, C, or D
private int priority;
//constructor
public FoodOrder(int memberID, String foodOrder) {
.......
}
//provide methods getter, setter, toString ....
}

View File

@@ -0,0 +1,119 @@
class ListNode {
private Object data;
private ListNode next;
public ListNode(Object o) { data = o; next = null; }
public ListNode(Object o, ListNode nextNode)
{ data = o; next = nextNode; }
public Object getData() { return data; }
public void setData(Object o) { data = o; }
public ListNode getNext() { return next; }
public void setNext(ListNode next) { this.next = next; }
} // class ListNode
class EmptyListException extends RuntimeException {
public EmptyListException ()
{ super("List is empty"); }
} // class EmptyListException
public class LinkedList {
private ListNode head;
private ListNode tail;
private int length; // the length of the list
public LinkedList() {
head = tail = null;
length = 0;
}
public boolean isEmpty() { return head == null; }
public void addToHead(Object item) {
if (isEmpty())
head = tail = new ListNode(item);
else
head = new ListNode(item, head);
length++;
}
public void addToTail(Object item) {
if (isEmpty())
head = tail = new ListNode(item);
else {
tail.setNext(new ListNode(item));
tail = tail.getNext();
}
length++;
}
public Object removeFromHead() throws EmptyListException {
Object item = null;
if (isEmpty())
throw new EmptyListException();
item = head.getData();
if (head == tail)
head = tail = null;
else
head = head.getNext();
length--;
return item;
}
public Object removeFromTail() throws EmptyListException {
Object item = null;
if (isEmpty())
throw new EmptyListException();
item = tail.getData();
if (head == tail)
head = tail = null;
else {
ListNode current = head;
while (current.getNext() != tail)
current = current.getNext();
tail = current;
current.setNext(null);
}
length--;
return item;
}
public int count() {
return length;
}
//students need to revise toString method
public String toString() {
String str = "[ ";
ListNode current = head;
while (current != null) {
str = str + current.getData() + " ";
current = current.getNext();
}
return str + " ]";
}
//to be completed ...
// Method remove(int) is to remove a ListNode from the LinkedList with a specific Member ID
public void remove(int targetID) throws EmptyListException {
.........
}
//to be completed ...
// Method add(Object) is to insert a new ListNode into the LinkedList in a correct position
public void add(Object item) {
.........
}
} // class LinkedList

View File

@@ -0,0 +1,25 @@
import ............
public class OrderSystem {
private static Scanner sc;
private static LinkedList orders;
private static int nextGuestID = 9000;
public static void main(String[] args) {
............
}
public static void inputOrder() throws ............ {
............
}
public static void adminFunc() ............ {
............
}
}