以下是该程序的实现示例:
import java.util.Scanner;
public class BookManagementSystem {
private static final int MAX_BOOKS = 5;
private static String[] bookTitles = {"The Catcher in the Rye", "To Kill a Mockingbird", "1984", "The Great Gatsby", "One Hundred Years of Solitude"};
private static String[] bookAuthors = {"J.D. Salinger", "Harper Lee", "George Orwell", "F. Scott Fitzgerald", "Gabriel Garcia Marquez"};
private static boolean[] bookBorrowed = {false, false, true, false, true};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:");
System.out.println("1.增加");
System.out.println("2.删除");
System.out.println("3.修改");
System.out.println("4.还书");
System.out.println("5.借书");
System.out.println("6.查看现有书本");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addBook();
break;
case 2:
deleteBook();
break;
case 3:
modifyBook();
break;
case 4:
returnBook();
break;
case 5:
borrowBook();
break;
case 6:
displayBooks();
break;
default:
System.out.println("无效的选择!");
break;
}
System.out.println();
}
}
private static void addBook() {
if (bookCount() >= MAX_BOOKS) {
System.out.println("已经达到最大书本数量!");
return;
}
Scanner scanner = new Scanner(System.in);
System.out.println("请输入书名:");
String title = scanner.nextLine();
System.out.println("请输入作者:");
String author = scanner.nextLine();
System.out.println("是否借出?(y/n)");
String borrowed = scanner.nextLine();
boolean isBorrowed = borrowed.equalsIgnoreCase("y") || borrowed.equalsIgnoreCase("yes");
bookTitles[bookCount()] = title;
bookAuthors[bookCount()] = author;
bookBorrowed[bookCount()] = isBorrowed;
System.out.println("书本已经成功添加!");
}
private static void deleteBook() {
if (bookCount() <= 0) {
System.out.println("没有书本可以删除!");
return;
}
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的书本编号:");
int bookIndex = scanner.nextInt() - 1;
if (bookIndex < 0 || bookIndex >= bookCount()) {
System.out.println("无效的书本编号!");
return;
}
for (int i = bookIndex; i < bookCount() - 1; i++) {
bookTitles[i] = bookTitles[i + 1];
bookAuthors[i] = bookAuthors[i + 1];
bookBorrowed[i] = bookBorrowed[i + 1];
}
System.out.println("书本已经成功删除!");
}
private static void modifyBook() {
if (bookCount() <= 0) {
System.out.println("没有书本可以修改!");
return;
}
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改的书本编号:");
int bookIndex = scanner.nextInt() - 1;
if (bookIndex < 0 || bookIndex >= bookCount()) {
System.out.println("无效的书本编号!");
return;
}
System.out.println("请输入新的书名:");
bookTitles[bookIndex] = scanner.nextLine();
System.out.println("请输入新的作者:");
bookAuthors[bookIndex] = scanner.nextLine();
System.out.println("是否借出?(y/n)");
String borrowed = scanner.nextLine();
boolean isBorrowed = borrowed.equalsIgnoreCase("y") || borrowed.equalsIgnoreCase("yes");
bookBorrowed[bookIndex] = isBorrowed;
System.out.println("书本已经成功修改!");
}
private static void returnBook() {
if (bookCount() <= 0) {
System.out.println("没有书本可以归还!");
return;
}
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要归还的书本编号:");
int bookIndex = scanner.nextInt() - 1;
if (bookIndex < 0 || bookIndex >= bookCount()) {
System.out.println("无效的书本编号!");
return;
}
if (!bookBorrowed[bookIndex]) {
System.out.println("该书本没有借出!");
return;
}
bookBorrowed[bookIndex] = false;
System.out.println("书本已经归还!");
}
private static void borrowBook() {
if (bookCount() <= 0) {
System.out.println("没有书本可以借出!");
return;
}
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要借出的书本编号:");
int bookIndex = scanner.nextInt() - 1;
if (bookIndex < 0 || bookIndex >= bookCount()) {
System.out.println("无效的书本编号!");
return;
}
if (bookBorrowed[bookIndex]) {
System.out.println("该书本已经借出!");
return;
}
bookBorrowed[bookIndex] = true;
System.out.println("书本已经借出!");
}
private static void displayBooks() {
if (bookCount() <= 0) {
System.out.println("没有书本可以查看!");
return;
}
System.out.println("现有书本:");
for (int i = 0; i < bookCount(); i++) {
System.out.println((i + 1) + ". " + bookTitles[i] + " - " + bookAuthors[i] + " - " + (bookBorrowed[i] ? "已借出" : "未借出"));
}
}
private static int bookCount() {
for (int i = 0; i < MAX_BOOKS; i++) {
if (bookTitles[i] == null) {
return i;
}
}
return MAX_BOOKS;
}
}