在本篇博客中,我使用了JavaSE的基本语法来构建一个简易的图书管理系统。这个简易的图书管理系统我认为是一个绝佳的项目,特别适合那些希望通过实际编程来巩固Java基础知识的同学。
根据需求分析,图书管理系统大致应具备以下的基本功能:
在编写代码前,我们首先要定义所需的类及其属性。图书管理系统主要涉及两个类:Book
和 BookList
,而增删查改一些功能我将通过写一个IOperation
的接口,通过继承接口类来实现众多功能.
Book
类表示书籍,包含以下属性:
public class Book {
private String name;// 书名
private String author;// 作者
private int price;// 价格
private boolean isBorrowed;// 是否被借出
// 构造方法、getters和setters省略
}
BookList
类负责管理书籍集合,其主要属性包括:
public class BookList {
private Book[] books;
public static int BOOKLIST_ALLNUM = 5;// 当前书架可放置书的数量
private int currentBooks = 3;
// 构造方法、getters和setters省略
}
User
public abstract class User {
public abstract int menu(); //
public IOperation[] iOperations;
}
通过实现抽象方法
menu
,两个类AdminUser,NormalUser
分别继承User
类对menu方法进行重写,这样我们就可以实现在生成不同用户时,调用不同菜单! ! !
下面将具体实现IOperation
类中的各项功能。
public class AddOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("请分别输入你要添加书的书名,作者,价格:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
String auther = scanner.nextLine();
int price = scanner.nextInt();
Book newBook = new Book(name,auther,price);
for (int i = 0; i < bookList.getCurrentBooks(); i++) {
Book book = bookList.getBooks(i);
if(book.getName().equals(newBook.getName())){
System.out.println("这本书已经存在!书本信息如下:");
System.out.println(book);
return;
}
}
if(bookList.getCurrentBooks() >= BookList.BOOKLIST_ALLNUM)
BookList.BOOKLIST_ALLNUM += 5;
bookList.setBooks(bookList.getCurrentBooks(), newBook);
bookList.setCurrentBooks(bookList.getCurrentBooks() + 1);
}
}
public class DelOperation implements IOperation{
public void DelBook(BookList bookList, int del) {
if(del == (BookList.BOOKLIST_ALLNUM - 1)) {
bookList.setBooks(del, null);
bookList.setCurrentBooks(bookList.getCurrentBooks() - 1);
}
for (; del < bookList.getCurrentBooks(); del++) {
bookList.setBooks(del, bookList.getBooks(del + 1));
}
bookList.setCurrentBooks(bookList.getCurrentBooks() - 1);
}
@Override
public void work(BookList bookList) {
System.out.println("请输入你要删除的书名:");
Scanner scanner = new Scanner(System.in);
String delBook = scanner.nextLine();
for (int i = 0; i < bookList.getCurrentBooks(); i++) {
Book book = bookList.getBooks(i);
if(book.getName().equals(delBook)){
DelBook(bookList,i);
return;
}
}
System.out.println("图书馆中没有这本书!");
}
}
public class FindOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("输入你要查找的图书:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
for (int i = 0; i < bookList.getCurrentBooks(); i++) {
Book book = bookList.getBooks(i);
if(book.getName().equals(name)){
System.out.println("存在!书本信息如下:");
System.out.println(book);
return;
}
}
System.out.println("没有这本书!");
}
}
public class BorrowOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("请输入你要借阅的书名:");
Scanner scanner = new Scanner(System.in);
String borrowBook = scanner.nextLine();
for (int i = 0; i < bookList.getCurrentBooks(); i++) {
Book book = bookList.getBooks(i);
if(book.getName().equals(borrowBook)){
if(book.isBorrowed()) {
System.out.println("这本书已被借出!");
} else {
System.out.println("借阅成功!");
book.setBorrowed(true);
System.out.print("书本信息如下:");
System.out.println(book);
}
return;
}
}
System.out.println("图书馆中没有这本书!");
}
public class ReturnOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("请输入你要归还的书名:");
Scanner scanner = new Scanner(System.in);
String borrowBook = scanner.nextLine();
for (int i = 0; i < bookList.getCurrentBooks(); i++) {
Book book = bookList.getBooks(i);
if (book.getName().equals(borrowBook)) {
book.setBorrowed(true);
System.out.println("归还成功!");
book.setBorrowed(false);
System.out.print("书本信息如下:");
System.out.println(book);
return;
}
}
}
}
public class ShowOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("以下为图书馆的书本信息:");
for (int i = 0; i < bookList.getCurrentBooks(); i++) {
System.out.println(bookList.getBooks(i));
}
}
}
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系统!");
System.exit(0);
}
}
最终,我们需要对系统进行测试。可以创建BookList
的实例,并通过调用其功能来完成测试。
public class Main {
public static User login() {
System.out.println("欢迎进入图书管理系统");
System.out.println("请选择你的身份:1:普通用户 0:管理员");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
if(choice == 1){
return new NormalUser();
} else {
return new AdminUser();
}
}
public static void main(String[] args) {
User user = login();
BookList bookList = new BookList();
while (true) {
System.out.println("选择你的操作:");
int choice = user.menu();
user.iOperations[choice].work(bookList);
}
}
}
更多测试结果省略…
通过这个简易的图书管理系统项目,我们可以看到即使是仅使用JavaSE的基础语法,也足以构建出具有实际功能的应用程序。这个系统虽简单,但涵盖了面向对象编程的基本概念,如类、对象、方法,继承,多态,是初学者学习Java的良好实践。随着我们对Java知识的深入,还可以在此基础上增加更多功能,如数据持久化、图形用户界面等,以丰富和扩展你的项目。