图书管理系统设计JAVA代码 1)借书。图书借阅信息:借阅人、借阅人所在单位、借书证号、所借书名、借阅日期、归还日期、逾期罚款等。借阅期限为一个月,逾期1天,罚款1角。2)还书。 3)图书维护:删除图书,增加图书等。图书基本信息:图书编号、书名、作者(要考虑多个作者情况)、出版社、出版日期、定价、馆藏数、借阅数等 4)读者维护:包括新增读者、更改读者信息、查找读者、读者信息删除、读者信息输出。 5)要求利用菜单的形式进行管理。 提示:建议对读者和图书都使用类进行管理。需要创建类 1.读者类 包括读者编号、读者姓名、所借图书等,实现对读者的信息的描述图书类 包括图书的编号、书名、是否借出等,实现对图书的描述。 2.读者库类 读者信息保存在一个文本文件中(以下假定为readertxt),实现利用文本文件建立读者的个人资料、按编号查找、输出或删除所有读者信息 3,图书库类 图书信息保存在一个文本文件中(以下假定为booktxt),实现对图书的维护、查找和删除等。
时间: 2023-08-31 09:39:18 浏览: 181
以下是一个简单的图书管理系统的JAVA代码,实现了借书、还书、图书维护和读者维护等功能。其中,用到了读者类、图书类、读者库类和图书库类。
读者类:
```java
public class Reader {
private String id; // 读者编号
private String name; // 读者姓名
private List<Book> books; // 所借图书列表
public Reader(String id, String name) {
this.id = id;
this.name = name;
books = new ArrayList<>();
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public List<Book> getBooks() {
return books;
}
public void borrowBook(Book book) {
books.add(book);
}
public void returnBook(Book book) {
books.remove(book);
}
}
```
图书类:
```java
public class Book {
private String id; // 图书编号
private String name; // 书名
private List<String> authors; // 作者列表
private String publisher; // 出版社
private String publishDate; // 出版日期
private double price; // 定价
private int total; // 馆藏数
private int borrowed; // 借阅数
public Book(String id, String name, List<String> authors, String publisher, String publishDate, double price, int total, int borrowed) {
this.id = id;
this.name = name;
this.authors = authors;
this.publisher = publisher;
this.publishDate = publishDate;
this.price = price;
this.total = total;
this.borrowed = borrowed;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public List<String> getAuthors() {
return authors;
}
public String getPublisher() {
return publisher;
}
public String getPublishDate() {
return publishDate;
}
public double getPrice() {
return price;
}
public int getTotal() {
return total;
}
public int getBorrowed() {
return borrowed;
}
public boolean isAvailable() {
return borrowed < total;
}
public void borrow() {
borrowed++;
}
public void returnBook() {
borrowed--;
}
}
```
读者库类:
```java
public class ReaderLibrary {
private List<Reader> readers; // 读者列表
public ReaderLibrary() {
readers = new ArrayList<>();
}
public void addReader(Reader reader) {
readers.add(reader);
}
public Reader findReaderById(String id) {
for (Reader reader : readers) {
if (reader.getId().equals(id)) {
return reader;
}
}
return null;
}
public void deleteReader(Reader reader) {
readers.remove(reader);
}
public void printReaders() {
for (Reader reader : readers) {
System.out.println("读者编号:" + reader.getId() + " 读者姓名:" + reader.getName());
}
}
}
```
图书库类:
```java
public class BookLibrary {
private List<Book> books; // 图书列表
public BookLibrary() {
books = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
}
public Book findBookById(String id) {
for (Book book : books) {
if (book.getId().equals(id)) {
return book;
}
}
return null;
}
public void deleteBook(Book book) {
books.remove(book);
}
public void printAvailableBooks() {
for (Book book : books) {
if (book.isAvailable()) {
System.out.println("图书编号:" + book.getId() + " 书名:" + book.getName() + " 作者:" + book.getAuthors() + " 出版社:" + book.getPublisher() + " 馆藏数:" + book.getTotal() + " 借阅数:" + book.getBorrowed());
}
}
}
}
```
菜单类:
```java
import java.util.Scanner;
public class Menu {
private ReaderLibrary readerLibrary;
private BookLibrary bookLibrary;
public Menu(ReaderLibrary readerLibrary, BookLibrary bookLibrary) {
this.readerLibrary = readerLibrary;
this.bookLibrary = bookLibrary;
}
public void display() {
Scanner scanner = new Scanner(System.in);
int choice = 0;
while (choice != 5) {
System.out.println("1. 借书");
System.out.println("2. 还书");
System.out.println("3. 图书维护");
System.out.println("4. 读者维护");
System.out.println("5. 退出");
System.out.print("请选择:");
choice = scanner.nextInt();
switch (choice) {
case 1:
borrowBook();
break;
case 2:
returnBook();
break;
case 3:
bookMaintenance();
break;
case 4:
readerMaintenance();
break;
case 5:
System.out.println("退出");
break;
default:
System.out.println("无效选项");
break;
}
}
scanner.close();
}
private void borrowBook() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入读者编号:");
String readerId = scanner.nextLine();
Reader reader = readerLibrary.findReaderById(readerId);
if (reader == null) {
System.out.println("读者不存在");
return;
}
System.out.print("请输入图书编号:");
String bookId = scanner.nextLine();
Book book = bookLibrary.findBookById(bookId);
if (book == null) {
System.out.println("图书不存在");
return;
}
if (!book.isAvailable()) {
System.out.println("图书已借完");
return;
}
reader.borrowBook(book);
book.borrow();
System.out.println("借书成功");
}
private void returnBook() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入读者编号:");
String readerId = scanner.nextLine();
Reader reader = readerLibrary.findReaderById(readerId);
if (reader == null) {
System.out.println("读者不存在");
return;
}
System.out.print("请输入图书编号:");
String bookId = scanner.nextLine();
Book book = null;
for (Book borrowedBook : reader.getBooks()) {
if (borrowedBook.getId().equals(bookId)) {
book = borrowedBook;
break;
}
}
if (book == null) {
System.out.println("读者未借阅该图书");
return;
}
reader.returnBook(book);
book.returnBook();
System.out.println("还书成功");
}
private void bookMaintenance() {
Scanner scanner = new Scanner(System.in);
int choice = 0;
while (choice != 4) {
System.out.println("1. 新增图书");
System.out.println("2. 删除图书");
System.out.println("3. 查看可借图书");
System.out.println("4. 返回");
System.out.print("请选择:");
choice = scanner.nextInt();
switch (choice) {
case 1:
addBook();
break;
case 2:
deleteBook();
break;
case 3:
printAvailableBooks();
break;
case 4:
System.out.println("返回");
break;
default:
System.out.println("无效选项");
break;
}
}
}
private void addBook() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入图书编号:");
String id = scanner.nextLine();
System.out.print("请输入书名:");
String name = scanner.nextLine();
System.out.print("请输入作者:");
String author = scanner.nextLine();
List<String> authors = new ArrayList<>();
authors.add(author);
System.out.print("请输入出版社:");
String publisher = scanner.nextLine();
System.out.print("请输入出版日期:");
String publishDate = scanner.nextLine();
System.out.print("请输入定价:");
double price = scanner.nextDouble();
System.out.print("请输入馆藏数:");
int total = scanner.nextInt();
Book book = new Book(id, name, authors, publisher, publishDate, price, total, 0);
bookLibrary.addBook(book);
System.out.println("新增图书成功");
}
private void deleteBook() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入图书编号:");
String id = scanner.nextLine();
Book book = bookLibrary.findBookById(id);
if (book == null) {
System.out.println("图书不存在");
return;
}
bookLibrary.deleteBook(book);
System.out.println("删除图书成功");
}
private void printAvailableBooks() {
bookLibrary.printAvailableBooks();
}
private void readerMaintenance() {
Scanner scanner = new Scanner(System.in);
int choice = 0;
while (choice != 4) {
System.out.println("1. 新增读者");
System.out.println("2. 删除读者");
System.out.println("3. 查看读者列表");
System.out.println("4. 返回");
System.out.print("请选择:");
choice = scanner.nextInt();
switch (choice) {
case 1:
addReader();
break;
case 2:
deleteReader();
break;
case 3:
printReaders();
break;
case 4:
System.out.println("返回");
break;
default:
System.out.println("无效选项");
break;
}
}
}
private void addReader() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入读者编号:");
String id = scanner.nextLine();
System.out.print("请输入读者姓名:");
String name = scanner.nextLine();
Reader reader = new Reader(id, name);
readerLibrary.addReader(reader);
System.out.println("新增读者成功");
}
private void deleteReader() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入读者编号:");
String id = scanner.nextLine();
Reader reader = readerLibrary.findReaderById(id);
if (reader == null) {
System.out.println("读者不存在");
return;
}
readerLibrary.deleteReader(reader);
System.out.println("删除读者成功");
}
private void printReaders() {
readerLibrary.printReaders();
}
}
```
主程序:
```java
public class Main {
public static void main(String[] args) {
ReaderLibrary readerLibrary = new ReaderLibrary();
BookLibrary bookLibrary = new BookLibrary();
Menu menu = new Menu(readerLibrary, bookLibrary);
menu.display();
}
}
```
这个代码只是一个简单实现,还有很多需要改进的地方,比如数据的持久化、输入输出的校验、逾期罚款的计算等。但是这个代码可以作为一个参考,帮助你了解如何用Java实现一个图书管理系统。
阅读全文