java 创建一个图书馆借阅系统,有以下几个要求 1.分为管理者系统和借阅者系统 2.管理者系统可以增加书籍,删除书籍,查看所有书籍 3.借阅者系统可以借阅书籍,查看所有书籍,搜索书籍 4.书籍由书名和作者名组成 5.拥有管理员账户管理系统和借阅者账户管理系统,可以选择登录与创建账户 要求运行程序可以达到以上要求
时间: 2024-03-16 17:47:22 浏览: 93
好的,这是一个比较大的项目,需要分步骤实现。首先,我们需要创建一个图书类,用来存储每本书的信息,包括书名和作者名。以下是一个简单的图书类示例:
```java
public class Book {
private String title; // 书名
private String author; // 作者名
public Book(String title, String author) {
this.title = title;
this.author = author;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
@Override
public String toString() {
return "Book{" +
"title='" + title + '\'' +
", author='" + author + '\'' +
'}';
}
}
```
接下来,我们需要创建一个书库类,用来管理所有的书籍,包括增加书籍、删除书籍、查看所有书籍等操作。以下是一个简单的书库类示例:
```java
import java.util.ArrayList;
public class Library {
private ArrayList<Book> books = new ArrayList<>();
// 增加书籍
public void addBook(Book book) {
books.add(book);
System.out.println("成功添加书籍:" + book);
}
// 删除书籍
public void removeBook(Book book) {
if (books.remove(book)) {
System.out.println("成功删除书籍:" + book);
} else {
System.out.println("未找到该书籍:" + book);
}
}
// 查看所有书籍
public void displayAllBooks() {
System.out.println("书库中共有" + books.size() + "本书:");
for (Book book : books) {
System.out.println(book);
}
}
// 搜索书籍
public void searchBooks(String keyword) {
ArrayList<Book> result = new ArrayList<>();
for (Book book : books) {
if (book.getTitle().contains(keyword) || book.getAuthor().contains(keyword)) {
result.add(book);
}
}
if (result.isEmpty()) {
System.out.println("未找到符合条件的书籍。");
} else {
System.out.println("共找到" + result.size() + "本符合条件的书籍:");
for (Book book : result) {
System.out.println(book);
}
}
}
}
```
现在我们已经有了一个可以管理书籍的类,接下来我们需要创建管理员系统和借阅者系统。管理员系统需要实现添加书籍、删除书籍、查看所有书籍等操作,借阅者系统需要实现借阅书籍、查看所有书籍、搜索书籍等操作。
以下是一个简单的管理员系统类示例:
```java
import java.util.Scanner;
public class AdminSystem {
private Library library;
public AdminSystem(Library library) {
this.library = library;
}
public void run() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\n管理员系统:");
System.out.println("1.添加书籍");
System.out.println("2.删除书籍");
System.out.println("3.查看所有书籍");
System.out.println("0.退出");
System.out.print("请选择操作:");
int choice = scanner.nextInt();
scanner.nextLine(); // 读取换行符
switch (choice) {
case 1:
System.out.print("请输入书名:");
String title = scanner.nextLine();
System.out.print("请输入作者名:");
String author = scanner.nextLine();
library.addBook(new Book(title, author));
break;
case 2:
System.out.print("请输入书名:");
String title2 = scanner.nextLine();
System.out.print("请输入作者名:");
String author2 = scanner.nextLine();
library.removeBook(new Book(title2, author2));
break;
case 3:
library.displayAllBooks();
break;
case 0:
return;
default:
System.out.println("无效的选项!");
}
}
}
}
```
以下是一个简单的借阅者系统类示例:
```java
import java.util.Scanner;
public class BorrowerSystem {
private Library library;
public BorrowerSystem(Library library) {
this.library = library;
}
public void run() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\n借阅者系统:");
System.out.println("1.借阅书籍");
System.out.println("2.查看所有书籍");
System.out.println("3.搜索书籍");
System.out.println("0.退出");
System.out.print("请选择操作:");
int choice = scanner.nextInt();
scanner.nextLine(); // 读取换行符
switch (choice) {
case 1:
System.out.print("请输入书名:");
String title = scanner.nextLine();
System.out.print("请输入作者名:");
String author = scanner.nextLine();
library.removeBook(new Book(title, author));
System.out.println("借阅成功!");
break;
case 2:
library.displayAllBooks();
break;
case 3:
System.out.print("请输入关键词:");
String keyword = scanner.nextLine();
library.searchBooks(keyword);
break;
case 0:
return;
default:
System.out.println("无效的选项!");
}
}
}
}
```
最后,我们需要创建一个主程序来管理管理员系统和借阅者系统,实现登录与创建账户等操作。以下是一个简单的主程序示例:
```java
import java.util.HashMap;
import java.util.Scanner;
public class Main {
private static HashMap<String, String> adminAccounts = new HashMap<>();
private static HashMap<String, String> borrowerAccounts = new HashMap<>();
public static void main(String[] args) {
Library library = new Library();
AdminSystem adminSystem = new AdminSystem(library);
BorrowerSystem borrowerSystem = new BorrowerSystem(library);
adminAccounts.put("admin", "admin"); // 默认管理员账户
borrowerAccounts.put("borrower", "borrower"); // 默认借阅者账户
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\n欢迎使用图书馆借阅系统!");
System.out.println("1.管理员登录");
System.out.println("2.创建管理员账户");
System.out.println("3.借阅者登录");
System.out.println("4.创建借阅者账户");
System.out.println("0.退出");
System.out.print("请选择操作:");
int choice = scanner.nextInt();
scanner.nextLine(); // 读取换行符
switch (choice) {
case 1:
if (login(adminAccounts)) {
adminSystem.run();
} else {
System.out.println("登录失败!");
}
break;
case 2:
createAccount(adminAccounts, "管理员");
break;
case 3:
if (login(borrowerAccounts)) {
borrowerSystem.run();
} else {
System.out.println("登录失败!");
}
break;
case 4:
createAccount(borrowerAccounts, "借阅者");
break;
case 0:
System.out.println("谢谢使用!");
return;
default:
System.out.println("无效的选项!");
}
}
}
private static boolean login(HashMap<String, String> accounts) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入用户名:");
String username = scanner.nextLine();
System.out.print("请输入密码:");
String password = scanner.nextLine();
return accounts.containsKey(username) && accounts.get(username).equals(password);
}
private static void createAccount(HashMap<String, String> accounts, String type) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入新" + type + "的用户名:");
String username = scanner.nextLine();
if (accounts.containsKey(username)) {
System.out.println("该用户名已存在!");
return;
}
System.out.print("请输入新" + type + "的密码:");
String password = scanner.nextLine();
accounts.put(username, password);
System.out.println("新" + type + "账户创建成功!");
}
}
```
这个程序还有很多可以改进的地方,比如添加输入验证、持久化存储等功能,但是基本框架已经搭建好了。希望能对你有所帮助!
阅读全文