Java代码In the rest of this question, use the readLine and readPosInt methods every time your program needs to read a string or an integer from the user. In the empty main method of the CLI class, create a single Library object with the name "UIC Library". The main method of the CLI class must then print a menu that allows the user of your system to do six different actions that involve the library object, and your program must then read an integer from the user that indicates which action must be performed by the program (see below for the details about each action). Use the readPosInt method to print the menu (give the string for the menu as the argument of readPosInt) and to read the integer typed by the user
时间: 2023-06-15 10:04:32 浏览: 131
java 读写文本的方法
Here's the Java code for the CLI class that creates a Library object and presents a menu to the user:
```
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CLI {
public static void main(String[] args) throws IOException {
Library library = new Library("UIC Library");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.println("Menu:");
System.out.println("1. Add a book");
System.out.println("2. Remove a book");
System.out.println("3. Search for a book");
System.out.println("4. Display all books");
System.out.println("5. Check out a book");
System.out.println("6. Return a book");
System.out.println("0. Exit");
int choice = readPosInt("Enter your choice (0-6): ", br);
switch (choice) {
case 0:
System.out.println("Goodbye!");
return;
case 1:
addBook(library, br);
break;
case 2:
removeBook(library, br);
break;
case 3:
searchBook(library, br);
break;
case 4:
displayBooks(library);
break;
case 5:
checkOutBook(library, br);
break;
case 6:
returnBook(library, br);
break;
default:
System.out.println("Invalid choice, try again.");
}
}
}
private static void addBook(Library library, BufferedReader br) throws IOException {
String title = readLine("Enter the title of the book: ", br);
String author = readLine("Enter the author of the book: ", br);
String isbn = readLine("Enter the ISBN of the book: ", br);
int year = readPosInt("Enter the year of publication: ", br);
library.addBook(new Book(title, author, isbn, year));
System.out.println("Book added successfully.");
}
private static void removeBook(Library library, BufferedReader br) throws IOException {
String isbn = readLine("Enter the ISBN of the book to remove: ", br);
if (library.removeBook(isbn)) {
System.out.println("Book removed successfully.");
} else {
System.out.println("Book not found.");
}
}
private static void searchBook(Library library, BufferedReader br) throws IOException {
String query = readLine("Enter a search query: ", br);
Book[] results = library.searchBook(query);
if (results.length == 0) {
System.out.println("No results found.");
} else {
System.out.println("Search results:");
for (Book book : results) {
System.out.println(book);
}
}
}
private static void displayBooks(Library library) {
Book[] books = library.getBooks();
if (books.length == 0) {
System.out.println("No books in the library.");
} else {
System.out.println("Books in the library:");
for (Book book : books) {
System.out.println(book);
}
}
}
private static void checkOutBook(Library library, BufferedReader br) throws IOException {
String isbn = readLine("Enter the ISBN of the book to check out: ", br);
if (library.checkOutBook(isbn)) {
System.out.println("Book checked out successfully.");
} else {
System.out.println("Book not found or already checked out.");
}
}
private static void returnBook(Library library, BufferedReader br) throws IOException {
String isbn = readLine("Enter the ISBN of the book to return: ", br);
if (library.returnBook(isbn)) {
System.out.println("Book returned successfully.");
} else {
System.out.println("Book not found or not checked out.");
}
}
private static String readLine(String prompt, BufferedReader br) throws IOException {
System.out.print(prompt);
return br.readLine();
}
private static int readPosInt(String prompt, BufferedReader br) throws IOException {
while (true) {
try {
String input = readLine(prompt, br);
int num = Integer.parseInt(input);
if (num >= 0) {
return num;
} else {
System.out.println("Please enter a non-negative integer.");
}
} catch (NumberFormatException e) {
System.out.println("Please enter an integer.");
}
}
}
}
```
Note that this code assumes the existence of a Library class with the following methods:
- `addBook(Book book)`: adds a book to the library
- `removeBook(String isbn)`: removes a book from the library with the given ISBN number
- `searchBook(String query)`: returns an array of books that match the given search query
- `getBooks()`: returns an array of all the books in the library
- `checkOutBook(String isbn)`: checks out a book from the library with the given ISBN number
- `returnBook(String isbn)`: returns a book to the library with the given ISBN number
You'll need to implement these methods in order for the CLI class to work properly.
阅读全文