Java面向对象综合案例:图书管理系统
介绍
在现代社会中,图书馆是一个重要的知识资源库。为了更好地管理和利用这些资源,开发一个图书管理系统是非常有必要的。本文将通过一个基于Java面向对象的综合案例,向大家展示如何设计和实现一个简单的图书管理系统。
需求分析
在开始编写代码之前,我们首先需要明确图书管理系统的需求。这个系统需要支持以下功能:
- 添加图书:向系统中添加新的图书;
- 删除图书:从系统中删除指定的图书;
- 查找图书:根据图书名称或作者查找图书;
- 借阅图书:借阅指定的图书;
- 归还图书:归还已借阅的图书;
- 显示图书:显示系统中的所有图书。
类设计
根据需求分析,我们可以设计以下几个类:
- 图书(Book):表示一本图书,包含图书的名称、作者等属性;
- 图书馆(Library):管理所有图书,包含添加、删除、查找和显示图书的方法;
- 用户(User):表示一个用户,包含用户的姓名、已借阅的图书等属性;
- 图书管理系统(BookManagementSystem):整个图书管理系统的入口,用于初始化和操作图书馆和用户。
下面是相应类的代码示例:
// Book.java
public class Book {
private String name;
private String author;
public Book(String name, String author) {
this.name = name;
this.author = author;
}
// Getters and setters
}
// Library.java
import java.util.ArrayList;
import java.util.List;
public class Library {
private List<Book> books;
public Library() {
this.books = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
}
public void removeBook(Book book) {
books.remove(book);
}
public List<Book> searchBooks(String keyword) {
List<Book> result = new ArrayList<>();
for (Book book : books) {
if (book.getName().contains(keyword) || book.getAuthor().contains(keyword)) {
result.add(book);
}
}
return result;
}
public void displayBooks() {
for (Book book : books) {
System.out.println(book.getName() + " - " + book.getAuthor());
}
}
}
// User.java
public class User {
private String name;
private List<Book> borrowedBooks;
public User(String name) {
this.name = name;
this.borrowedBooks = new ArrayList<>();
}
public void borrowBook(Book book) {
borrowedBooks.add(book);
}
public void returnBook(Book book) {
borrowedBooks.remove(book);
}
// Getters and setters
}
// BookManagementSystem.java
import java.util.Scanner;
public class BookManagementSystem {
private Library library;
private User user;
public BookManagementSystem() {
this.library = new Library();
this.user = new User("John Doe");
}
public void run() {
Scanner scanner = new Scanner(System.in);
boolean running = true;
while (running) {
System.out.println("1. Add book");
System.out.println("2. Remove book");
System.out.println("3. Search books");
System.out.println("4. Borrow book");
System.out.println("5. Return book");
System.out.println("6. Display books");
System.out.println("0. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // consume the newline character
switch (choice) {
case 1:
System.out.print("Enter book name: ");
String name = scanner.nextLine();
System.out.print("Enter book author: ");
String author = scanner.nextLine();
Book book = new Book(name, author);
library.addBook(book);
break;
case 2:
// TODO: Implement remove book functionality
break;
case 3:
System.out.print("Enter search keyword: ");
String keyword = scanner.nextLine();
List<Book> searchResult = library.searchBooks(keyword);
for (Book result : searchResult) {
System.out.println(result.getName() + " - " + result.getAuthor());
}
break;
case 4:
// TODO: Implement borrow book functionality
break;
case 5:
// TODO: Implement return book functionality
break;
case 6:
library.displayBooks();
break;
case 0:
running = false;
break;
default:
System.out.println