When the user of the software specifies action 2, your program must add a new user to the library. To add a new user, your program needs to ask the user three things: the role of user (an integer read using readPosInt: the integer 1 represents lender, the integer 2 represents borrower, any other integer must result in an error message "Unknown user role!" being printed and the software going immediately back to the main menu), the name of the user (a string read using readLine), and the initial number of books that the user lends (for a lender) or borrows (for a borrower). You program must then create the correct user, add it to the library, and print an information message. The program then goes back to the menu. For example (where 2, 3, 2, 1, Anna, 5, 2, 2, Bob, and 10 are inputs from the user): Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 2 Type the user role (lender:1 borrower:2): 3 Unknown user role! Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 2 Type the user role (lender:1 borrower:2): 1 Enter the name of the user: Anna Enter the initial number of borrowed books: 5 Lender "Anna" lending 5 book(s) has been added. Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 2 Type the user role (lender:1 borrower:2): 2 Enter the name of the user: Bob Enter the initial number of borrowed books: 10 Borrower "Bob" borrowing 10 book(s) has been added. Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): Note that the readPosInt method prevents the initial number of books from being negative, so the constructor for the Borrower class will never throw a NotALenderException when you create a borrower object. Nevertheless the code of the main method of your CLI class must handle this exception by printing the error message "BUG! This must never happen!" and immediately terminating the program using System.exit(1);
时间: 2023-11-27 22:49:50 浏览: 76
Here is the code implementation for adding a new user to the library based on the given specifications:
```
import java.util.Scanner;
public class CLI {
private static Library library = new Library();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int action;
do {
System.out.print("Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): ");
action = scanner.nextInt();
switch (action) {
case 1:
System.out.println("Total number of books in the library: " + library.getTotal());
break;
case 2:
addNewUser(scanner);
break;
case 3:
getBooks(scanner);
break;
case 4:
addBooks(scanner);
break;
case 5:
lessBooks(scanner);
break;
case 6:
System.out.println("Goodbye!");
break;
default:
System.out.println("Unknown action!");
}
} while (action != 6);
}
private static void addNewUser(Scanner scanner) {
System.out.print("Type the user role (lender:1 borrower:2): ");
int role = scanner.nextInt();
if (role == 1) {
System.out.print("Enter the name of the user: ");
String name = scanner.next();
System.out.print("Enter the initial number of lent books: ");
int numBooks = readPosInt(scanner);
Lender lender = new Lender(name, numBooks);
library.addUser(lender);
System.out.println("Lender \"" + name + "\" lending " + numBooks + " book(s) has been added.");
} else if (role == 2) {
System.out.print("Enter the name of the user: ");
String name = scanner.next();
System.out.print("Enter the initial number of borrowed books: ");
int numBooks = readPosInt(scanner);
Borrower borrower = new Borrower(name, numBooks);
library.addUser(borrower);
System.out.println("Borrower \"" + name + "\" borrowing " + numBooks + " book(s) has been added.");
} else {
System.out.println("Unknown user role!");
}
}
private static void getBooks(Scanner scanner) {
System.out.print("Enter the name of the borrower: ");
String name = scanner.next();
User user = library.findUser(name);
if (user != null && user instanceof Borrower) {
System.out.print("Enter the number of books borrowed: ");
int numBooks = readPosInt(scanner);
try {
library.getBorrowed((Borrower) user, numBooks);
System.out.println(numBooks + " book(s) have been borrowed by " + name);
} catch (NotEnoughBooksException e) {
System.out.println("Not enough books available to borrow!");
}
} else {
System.out.println("Borrower \"" + name + "\" not found!");
}
}
private static void addBooks(Scanner scanner) {
System.out.print("Enter the name of the lender: ");
String name = scanner.next();
User user = library.findUser(name);
if (user != null && user instanceof Lender) {
System.out.print("Enter the number of books lent: ");
int numBooks = readPosInt(scanner);
try {
library.getLent((Lender) user, numBooks);
System.out.println(numBooks + " book(s) have been lent by " + name);
} catch (NotEnoughSpaceException e) {
System.out.println("Not enough space available to lend books!");
}
} else {
System.out.println("Lender \"" + name + "\" not found!");
}
}
private static void lessBooks(Scanner scanner) {
System.out.print("Enter the name of the borrower: ");
String name = scanner.next();
User user = library.findUser(name);
if (user != null && user instanceof Borrower) {
System.out.print("Enter the number of books returned: ");
int numBooks = readPosInt(scanner);
try {
library.returnBooks((Borrower) user, numBooks);
System.out.println(numBooks + " book(s) have been returned by " + name);
} catch (NotEnoughBooksException e) {
System.out.println("Invalid number of books returned!");
}
} else {
System.out.println("Borrower \"" + name + "\" not found!");
}
}
private static int readPosInt(Scanner scanner) {
int num = scanner.nextInt();
if (num < 0) {
System.out.println("BUG! This must never happen!");
System.exit(1);
}
return num;
}
}
```
Note that the method `readPosInt` is used to read a positive integer input from the user, and it handles the case where the user enters a negative integer by printing an error message and immediately terminating the program using `System.exit(1)`.
阅读全文