146 lines
3.9 KiB
Java
146 lines
3.9 KiB
Java
/*
|
|
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
|
* Date: 2022-04-25
|
|
* Description: This is the CLI class.
|
|
*/
|
|
|
|
import java.util.InputMismatchException;
|
|
import java.util.Scanner;
|
|
|
|
public class CLI {
|
|
private static Scanner input = new Scanner(System.in);
|
|
|
|
/**
|
|
* Read one line from CLI.
|
|
*
|
|
* @param message Hint printed to screen
|
|
* @return one line input
|
|
*/
|
|
private static String readLine(String message) {
|
|
System.out.print(message);
|
|
return input.nextLine();
|
|
}
|
|
|
|
/**
|
|
* Read one positive integer.
|
|
*
|
|
* @param message HINT printed to screen
|
|
* @return one positive integer
|
|
*/
|
|
private static int readPosInt(String message) {
|
|
while (true) {
|
|
System.out.print(message);
|
|
try {
|
|
int result = input.nextInt();
|
|
input.nextLine();
|
|
if (result >= 0) {
|
|
return result;
|
|
} else {
|
|
System.out.println("Positive integers only!");
|
|
}
|
|
} catch (InputMismatchException e) {
|
|
System.out.println("You must type an integer!");
|
|
input.nextLine();
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
int book;
|
|
String username;
|
|
String menuHint = "Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): ";
|
|
String userRoleHint = "Type the user role (lender:1 borrower:2): ";
|
|
|
|
Library library = new Library("UIC Library");
|
|
while (true) {
|
|
switch (readPosInt(menuHint)) {
|
|
// total
|
|
case 1:
|
|
System.out.println("Total number of borrowed books: "
|
|
+ library.totalBorrowedBooks());
|
|
break;
|
|
// add
|
|
case 2:
|
|
switch (readPosInt(userRoleHint)) {
|
|
// lender
|
|
case 1:
|
|
username = readLine("Enter the name of the user: ");
|
|
book = readPosInt(
|
|
"Enter the initial number of borrowed books: ");
|
|
Lender lender = new Lender(username, book);
|
|
library.addUser(lender);
|
|
System.out.println("Lender \"" + username + "\" lending " + book
|
|
+ " book(s) has been added.");
|
|
break;
|
|
// borrower
|
|
case 2:
|
|
username = readLine(
|
|
"Enter the name of the user: ");
|
|
book = readPosInt(
|
|
"Enter the initial number of borrowed books: ");
|
|
try {
|
|
library.addUser(new Borrower(username, book));
|
|
System.out.println("Borrower \""
|
|
+ username
|
|
+ "\" borrowing "
|
|
+ book
|
|
+ " book(s) has been added.");
|
|
} catch (NotALenderException e) {
|
|
System.out.println("BUG! This must never happen!");
|
|
System.exit(1);
|
|
}
|
|
break;
|
|
default:
|
|
System.out.println("Unknown user role!");
|
|
break;
|
|
}
|
|
break;
|
|
// get
|
|
case 3:
|
|
username = readLine("Enter the name of the user: ");
|
|
try {
|
|
System.out.println(username + " borrows "
|
|
+ library.getBook(username) + " book(s).");
|
|
} catch (UnknownUserException e) {
|
|
System.out.println("User " + username + " unknown.");
|
|
}
|
|
break;
|
|
case 4:
|
|
username = readLine("Enter the name of the user: ");
|
|
book = readPosInt("Enter the number of books: ");
|
|
try {
|
|
library.moreBook(username, book);
|
|
} catch (NotALenderException e) {
|
|
System.out.println("BUG! This must never happen!");
|
|
} catch (UnknownUserException e) {
|
|
System.out.println("User " + username + " unknown.");
|
|
}
|
|
break;
|
|
case 5:
|
|
username = readLine("Enter the name of the user: ");
|
|
book = readPosInt("Enter the number of books: ");
|
|
try {
|
|
// decrease numbers
|
|
try {
|
|
library.moreBook(username, -book);
|
|
} catch (NotALenderException e) {
|
|
System.out.println("A borrower cannot lend "
|
|
+ (-(library.getBook(username) - book))
|
|
+ " book(s).");
|
|
}
|
|
} catch (UnknownUserException e) {
|
|
System.out.println("User " + username + " unknown.");
|
|
}
|
|
break;
|
|
case 6:
|
|
System.out.println("Goodbye!");
|
|
System.exit(0);
|
|
break;
|
|
default:
|
|
System.out.println("Unknown action!");
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|