简单的图书管理系统源代码
Test
package BookSystem;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Object o = null;
BookList bookList = new BookList();
User user = login();
while (true) {
int choice = user.menu();
user.doOperation(choice, bookList);
}
}
public static User login() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您的姓名: ");
String name = scanner.next();
System.out.println("请输入您的角色: 1 管理员, 0 普通用户");
int who = scanner.nextInt();
if (who == 1) {
return new Admin(name);
}
return new NormalUser(name);
}
}
User
package BookSystem;
import BookSystem.operation.IOperation;
abstract public class User {
protected String name;
protected IOperation[] operations;
public abstract int menu();
public void doOperation(int choice, BookList bookList) {
IOperation operation = this.operations[choice - 1];
operation.work(bookList);
}
}
NormalUser
package BookSystem;
import BookSystem.operation.*;
import java.util.Scanner;
public class NormalUser extends User {
public NormalUser(String name) {
this.name = name;
this.operations = new IOperation[] {
new FindOperation(),
new BorrowOperation(),
new ReturnOperation(),
new ExitOperation()
};
}
@Override
public int menu() {
System.out.println("=========================");
System.out.println("Hello! " + name + ", 欢迎使用咱的图书管理系统");
System.out.println(" 1. 查阅书籍信息");
System.out.println(" 2. 借阅书籍");
System.out.println(" 3. 归还书籍");
System.out.println(" 4. 退出系统");
System.out.println("=========================");
System.out.println("请输入您的选择: ");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
BookList
package BookSystem;
public class BookList {
private Book[] books = new Book[100];
private int size = 0;
public BookList(){
books[0] = new Book("朝花夕拾","鲁迅",35,"文学散文");
books[1] = new Book("三体","刘慈欣",99,"科幻小说");
books[2] = new Book("骆驼祥子","老舍",29,"文学小说");
books[3] = new Book("十宗罪","蜘蛛",108,"悬疑小说");
size = 4;
}
public Book getBook(int index) {
return books[index];
}
public void setBook(int index, Book book) {
books[index] = book;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
}
Book
package BookSystem;
public class Book {
private String name;
private String author;
private double price;
private String type;
private boolean isBorrowed = false;
public Book(String name, String author, double price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
", isBorrowed=" + isBorrowed +
'}';
}
public String getName() {
return name;
}
public boolean isBorrowed() {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
}
Admin
package BookSystem;
import BookSystem.operation.*;
import java.util.Scanner;
public class Admin extends User {
public Admin(String name) {
this.name = name;
this.operations = new IOperation[] {
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation(),
new ExitOperation()
};
}
@Override
public int menu() {
System.out.println("=========================");
System.out.println("Hello! " + name + ", 欢迎使用咱的图书管理系统");
System.out.println(" 1. 查阅书籍信息");
System.out.println(" 2. 新增书籍信息");
System.out.println(" 3. 删除书籍信息");
System.out.println(" 4. 打印书籍列表");
System.out.println(" 5. 退出系统");
System.out.println("=========================");
System.out.println("请输入您的选择: ");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
IOperation
package BookSystem.operation;
import BookSystem.BookList;
public interface IOperation {
void work(BookList bookList);
}
AddOperation
package BookSystem.operation;
import BookSystem.Book;
import BookSystem.BookList;
import java.util.Scanner;
public class AddOperation implements IOperation {
@Override
public void work(BookList bookList) {
Scanner scanner = new Scanner(System.in);
System.out.println("新增书籍");
System.out.println("请输入新的书籍的名称: ");
String name = scanner.next();
System.out.println("请输入新的书籍的作者: ");
String author = scanner.next();
System.out.println("请输入新的书籍的价格: ");
double price = scanner.nextDouble();
System.out.println("请输入新的书籍的类别: ");
String type = scanner.next();
Book newBook = new Book(name, author, price, type);
int curSize = bookList.getSize();
bookList.setBook(curSize, newBook);
bookList.setSize(curSize + 1);
System.out.println("新增书籍成功!");
}
}
DeleteOperation
package BookSystem.operation;
import BookSystem.BookList;
import java.util.Scanner;
public class DelOperation implements IOperation {
@Override
public void work(BookList bookList) {
Scanner scanner = new Scanner(System.in);
System.out.println("删除书籍");
System.out.println("请输入要删除的书籍的名字: ");
String name = scanner.next();
int i = 0;
for (; i < bookList.getSize(); i++) {
if (name.equals(bookList.getBook(i).getName())) {
break;
}
}
if (i >= bookList.getSize()) {
System.out.println("您输入的书籍 [" + name + "] 在系统中没有找到! 删除失败!");
return;
}
if (i == bookList.getSize() - 1) {
bookList.setSize(bookList.getSize() - 1);
System.out.println("删除成功");
return;
}
bookList.setBook(i, bookList.getBook(bookList.getSize() - 1));
bookList.setSize(bookList.getSize() - 1);
System.out.println("删除成功");
}
}
FindOperation
package BookSystem.operation;
import BookSystem.BookList;
import java.util.Scanner;
public class FindOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("查找书籍");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要查找的书名: ");
String name = scanner.next();
for (int i = 0; i < bookList.getSize(); i++) {
if (bookList.getBook(i).getName().contains(name)) {
System.out.println(bookList.getBook(i));
}
}
}
}
DisplayOperation
package BookSystem.operation;
import BookSystem.Book;
import BookSystem.BookList;
public class DisplayOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("显示书籍列表");
for (int i = 0; i < bookList.getSize(); i++) {
System.out.println(bookList.getBook(i));
}
}
}
ExitOperation
package BookSystem.operation;
import BookSystem.BookList;
public class ExitOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("退出程序");
System.exit(0);
}
}
BorrowOperation
package BookSystem.operation;
import BookSystem.Book;
import BookSystem.BookList;
import java.util.Scanner;
public class BorrowOperation implements IOperation {
@Override
public void work(BookList bookList) {
Scanner scanner = new Scanner(System.in);
System.out.println("借阅书籍");
System.out.println("请输入要借阅的书籍的名称: ");
String name = scanner.next();
int i = 0;
for (; i < bookList.getSize(); i++) {
if (bookList.getBook(i).getName().equals(name)) {
break;
}
}
if (i >= bookList.getSize()) {
System.out.println("未找到指定的书籍, 无法借阅!");
return;
}
Book currentBook = bookList.getBook(i);
if (currentBook.isBorrowed()) {
System.out.println("该书籍已经被借出!");
return;
}
bookList.getBook(i).setBorrowed(true);
System.out.println("借书成功!");
}
}
ReturnOperation
package BookSystem.operation;
import BookSystem.Book;
import BookSystem.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOperation {
@Override
public void work(BookList bookList) {
Scanner scanner = new Scanner(System.in);
System.out.println("归还书籍");
System.out.println("请输入要还的书籍的名称: ");
String name = scanner.next();
int i = 0;
for (; i < bookList.getSize(); i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name)) {
break;
}
}
if (i >= bookList.getSize()) {
System.out.println("书籍没有找到, 无法归还!");
return;
}
Book currentBook = bookList.getBook(i);
if (!currentBook.isBorrowed()) {
System.out.println("这本书没有借出, 无法归还!");
return;
}
currentBook.setBorrowed(false);
System.out.println("归还书籍成功! ");
return;
}
}