Java写一个图书馆借书还书的程序
时间: 2023-05-21 21:00:38 浏览: 97
好的,以下是一个简单的Java图书馆借书还书的程序:
```
import java.util.ArrayList;
import java.util.Scanner;
public class Library {
private static ArrayList<String> books = new ArrayList<String>();
private static ArrayList<String> borrowedBooks = new ArrayList<String>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice = 0;
while (choice != 3) {
System.out.println("请选择操作:");
System.out.println("1. 借书");
System.out.println("2. 还书");
System.out.println("3. 退出");
choice = scanner.nextInt();
switch (choice) {
case 1:
borrowBook();
break;
case 2:
returnBook();
break;
case 3:
System.out.println("谢谢使用!");
break;
default:
System.out.println("无效的选择,请重新选择!");
break;
}
}
}
private static void borrowBook() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要借的书名:");
String book = scanner.nextLine();
if (books.contains(book)) {
books.remove(book);
borrowedBooks.add(book);
System.out.println("借书成功!");
} else {
System.out.println("抱歉,该书已被借出或不存在!");
}
}
private static void returnBook() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要还的书名:");
String book = scanner.nextLine();
if (borrowedBooks.contains(book)) {
borrowedBooks.remove(book);
books.add(book);
System.out.println("还书成功!");
} else {
System.out.println("抱歉,您没有借过该书或书名有误!");
}
}
}
```
这个程序使用了两个ArrayList来存储图书馆中的书和已经被借出的书。用户可以选择借书或还书,程序会根据用户的选择进行相应的操作。
阅读全文