本文使用 Java 实现一个简易图书管理系统
一、思路
简易图书管理系统说白了其实就是 用户 与 图书 这两个对象之间的交互
书的属性有
书名
作者
类型
价格
借阅状态
而用户可以分为
普通用户
管理员
使用数组将书统一管理起来
用户对这个数组进行操作
普通用户可以进行
查看所有书籍
借阅书籍
归还书
退出系统
而管理员可以进行普通用户的所有操作,此外
增加书籍
删除书籍
大致如此
二、实现
2.1 创建对象
2.1.1 书
public class Book {private String name;//书名private String author;//作者private double price;//价格private String type;//类型boolean IsBorrowed;//借出状态
}
书需要构造方法
以及重写 toString,方便输出
public class Book {private String name;//书名private String author;//作者private double price;//价格private String type;//类型boolean IsBorrowed;//借出状态//构造方法//书一开始的状态都是未借出//boolean类型在不赋值的情况下默认人为falsepublic Book(String name, String author, double price, String type) {this.name = name;this.author = author;this.price = price;this.type = type;}@Overridepublic String toString() {return "Book{" +"name='" + name + '\'' +", author='" + author + '\'' +", price=" + price +", type='" + type + '\'' +((IsBorrowed == true)?"已接触":"未借出") +'}';}
}
为方便管理,将所有与书有关的类放到同一个包下面去
package book;public class Book {private String name;//书名private String author;//作者private double price;//价格private String type;//类型boolean IsBorrowed;//借出状态//构造方法//书一开始的状态都是未借出//boolean类型在不赋值的情况下默认人为falsepublic Book(String name, String author, double price, String type) {this.name = name;this.author = author;this.price = price;this.type = type;}@Overridepublic String toString() {return "Book{" +"name='" + name + '\'' +", author='" + author + '\'' +", price=" + price +", type='" + type + '\'' +((IsBorrowed == true)?"已接触":"未借出") +'}';}
}
目前就是如此
其他方法在实现用户与图书交互时进行补充
2.1.2 用户
为方便管理,将用户相关类放到另一个包中
用户与管理员在属性上是相同的
那么就可以继承于一个抽象类
package user;abstract public class User {protected String name;public User(String name) {this.name = name;}
}
package user;public class NormalUser extends User {public NormalUser(String name) {super(name);}
}
package user;public class Administrator extends User{public Administrator(String name) {super(name);}
}
可能用到的方法依旧是在后文中进行补充
2.2 交互
2.2.1 操作界面
用户在登陆时会检测身份
public static User login(){System.out.println("欢迎使用图书管理系统!!!");Scanner scanner = new Scanner(System.in);System.out.println("请输入您的姓名");String name = scanner.nextLine();System.out.println("请选择你的身份");System.out.println("1.普通用户");System.out.println("2.管理员");int choice = scanner.nextInt();if(choice == 1){return new NormalUser (name);}else if(choice == 2){return new Administrator(name);}else {System.out.println("输入的值无效");}return null;}
无论是管理员还是普通用户都会有操作界面
那就可以将这个方法作为抽象方法放在 User 类中
package user;
abstract public class User {protected String name;public User(String name) {this.name = name;}abstract public void menu();
}
在子类中实现
package user;public class NormalUser extends User {public NormalUser(String name) {super(name);}@Overridepublic void menu() {System.out.println("请选择操作");System.out.println("1.显示图书");System.out.println("2.借阅图书");System.out.println("3.归还图书");System.out.println("4.查找图书");System.out.println("0.退出系统!");}
}
package user;public class Administrator extends User{public Administrator(String name) {super(name);}@Overridepublic void menu() {System.out.println("请选择操作");System.out.println("1.显示图书");System.out.println("2.借阅图书");System.out.println("3.归还图书");System.out.println("4.查找图书");System.out.println("5.增加图书");System.out.println("6.删除图书");System.out.println("0.退出系统!");}
}
测试
public static void main(String[] args) {User user = login();user.menu();}
2.2.2 书架
将书放到一个数组中方便管理
定义一个书架类
这个书架中默认会有几本书
书架有默认容量和已存书本数量
package book;public class BookList {private final int defaultcapacity = 10;//默认容量protected Book[] booklist = new Book[defaultcapacity];private int usespace;//已存书本容量public BookList() {this.booklist[0] = new Book("三体", "刘慈欣", 18.88, "小说");this.booklist[1] = new Book("西游记", "吴承恩", 17.77, "小说");this.booklist[2] = new Book("红楼梦", "曹雪芹", 16.66, "小说");usespace = 3;}public int usesize(){return this.usespace;}public int size(){return this.booklist.length;}
}
所有操作实际上都是对 booklist 这个数组进行
2.2.3 对图书操作
无论是管理员还是普通用户的操作
都可以理解成一种行为的多态
那就可以用一个接口
package behavior;import book.BookList;public interface Behavior{void work();//各种操作BookList booklist = new BookList();
}
所有的操作都是实现了这个接口的类
public class AddBehavior implements Ibehavior{@Overridepublic void work() {System.out.println("添加图书");}
}
public class BorrowedBehavior implements Ibehavior{@Overridepublic void work() {System.out.println("借阅图书");}
}
public class ExitBehavior implements Ibehavior{@Overridepublic void work() {System.out.println("退出系统!");}
}
public class RemoveBehavior implements Ibehavior{@Overridepublic void work() {System.out.println("归还图书!");}
}
public class ReturnBehavior implements Ibehavior{@Overridepublic void work() {System.out.println("归还图书");}
}
public class ShowBehavior implements Ibehavior{@Overridepublic void work() {System.out.println("展示未借阅的图书!");}
}
public class FindBehavior implements Behavior{@Overridepublic void work() {System.out.println("查找图书!");}
}
2.2.4 具体实现对图书操作
普通用户与管理员有对应的操作
可以在普通用户类与管理员类中创建一个数组
public class Administrator extends User{public Administrator(String name) {super(name);}protected Behavior[] behaviors = {new ExitBehavior(),new ShowBehavior(),new BorrowedBehavior(),new ReturnBehavior(),new FindBehavior(),new AddBehavior(),new RemoveBehavior()};@Overridepublic void menu() {System.out.println("请选择操作");System.out.println("1.显示图书");System.out.println("2.借阅图书");System.out.println("3.归还图书");System.out.println("4.查找图书");System.out.println("5.增加图书");System.out.println("6.删除图书");System.out.println("0.退出系统!");}
}
public class NormalUser extends User {public NormalUser(String name) {super(name);}
protected Behavior[] behaviors = {new ExitBehavior(),new ShowBehavior(),new BorrowedBehavior(),new ReturnBehavior(),new FindBehavior()};@Overridepublic void menu() {System.out.println("请选择操作");System.out.println("1.显示图书");System.out.println("2.借阅图书");System.out.println("3.归还图书");System.out.println("4.查找图书");System.out.println("0.退出系统!");}
}
这里就需要对应的下标值来对数组进行操作
而对应下标值可以通过 menu 方法来返回
修改一下就可以了
public class Administrator extends User{public Administrator(String name) {super(name);}protected Behavior[] behaviors = {new ExitBehavior(),new ShowBehavior(),new BorrowedBehavior(),new ReturnBehavior(),new FindBehavior(),new AddBehavior(),new RemoveBehavior()};@Overridepublic int menu() {System.out.println("请选择操作");System.out.println("1.显示图书");System.out.println("2.借阅图书");System.out.println("3.归还图书");System.out.println("4.查找图书");System.out.println("5.增加图书");System.out.println("6.删除图书");System.out.println("0.退出系统!");Scanner scanner = new Scanner(System.in);int choice = scanner.nextInt();return choice;}@Overridepublic void DoBehavior(int choice){this.behaviors[choice].work();}
}
public class NormalUser extends User {public NormalUser(String name) {super(name);}
protected Behavior[] behaviors = {new ExitBehavior(),new ShowBehavior(),new BorrowedBehavior(),new ReturnBehavior(),new FindBehavior()};@Overridepublic int menu() {System.out.println("请选择操作");System.out.println("1.显示图书");System.out.println("2.借阅图书");System.out.println("3.归还图书");System.out.println("4.查找图书");System.out.println("0.退出系统!");Scanner scanner = new Scanner(System.in);int choice = scanner.nextInt();return choice;}@Overridepublic void DoBehavior(int choice){this.behaviors[choice].work();}
}
对各项操作进行实现
2.2.4.1 退出系统(即结束程序)
public class ExitBehavior implements Behavior{@Overridepublic void work() {System.out.println("退出系统!");System.exit(0);}
}
测试
2.2.4.2 查找图书
根据书名进行对应图书查找
需要拿到书名
在 Book 与 BookList 中增加方法
public class Book {private String name;//书名private String author;//作者private double price;//价格private String type;//类型boolean IsBorrowed;//借出状态//构造方法//书一开始的状态都是未借出//boolean类型在不赋值的情况下默认人为falsepublic Book(String name, String author, double price, String type) {this.name = name;this.author = author;this.price = price;this.type = type;}@Overridepublic String toString() {return "Book{" +"name='" + name + '\'' +", author='" + author + '\'' +", price=" + price +", type='" + type + '\'' +((IsBorrowed == true)?"已接触":"未借出") +'}';}public String getName(){return this.name;}
}
package book;public class BookList {private final int defaultcapacity = 10;//默认容量protected Book[] booklist = new Book[defaultcapacity];private int usespace;//已存书本容量public BookList() {this.booklist[0] = new Book("三体", "刘慈欣", 18.88, "小说");this.booklist[1] = new Book("西游记", "吴承恩", 17.77, "小说");this.booklist[2] = new Book("红楼梦", "曹雪芹", 16.66, "小说");usespace = 3;}public int usesize(){
// 存放书本数量return this.usespace;}public int size(){
// 书架总容量return this.booklist.length;}public String getBookName(int index){
// 返回对应位置的书名return this.booklist[index].getName();}public Book getBook(int index){
// 返回对应位置的书return this.booklist[index];}
}
package behavior;import java.util.Scanner;public class FindBehavior implements Behavior{@Overridepublic void work() {System.out.println("查找图书!");Scanner scanner = new Scanner(System.in);System.out.println("请输入你要查找的书籍名称:");String name = scanner.nextLine();for (int i = 0; i < booklist.usesize(); i++) {if(name.equals(booklist.getBookName(i))){System.out.println("已找到您要查找的图书");System.out.println(booklist.getBook(i));return;}}System.out.println("没有您要查找的书籍");}
}
测试
2..2.4.3 打印图书
将书架上所有书打印出来
只展示没有被借出的图书
那就需要获得书的借出状态
public class Book {private String name;//书名private String author;//作者private double price;//价格private String type;//类型boolean IsBorrowed;//借出状态//构造方法//书一开始的状态都是未借出//boolean类型在不赋值的情况下默认人为falsepublic Book(String name, String author, double price, String type) {this.name = name;this.author = author;this.price = price;this.type = type;}@Overridepublic String toString() {return "Book{" +"name='" + name + '\'' +", author='" + author + '\'' +", price=" + price +", type='" + type + '\'' +((IsBorrowed == true)?"已接触":"未借出") +'}';}public String getName(){return this.name;}public Boolean IsBorrowed(){return this.IsBorrowed;}
}
package behavior;public class ShowBehavior implements Behavior{@Overridepublic void work() {System.out.println("展示未借阅的图书!");for (int i = 0; i < booklist.usesize(); i++) {if(!booklist.getBook(i).IsBorrowed()){System.out.println(booklist.getBook(i));}}}
}
测试
2.2.4.4 借阅与归还图书
查找到对应图书
修改该书的 IsBorrowed 属性即可
需要在 Book 中增加 set 方法
package book;public class Book {private String name;//书名private String author;//作者private double price;//价格private String type;//类型boolean IsBorrowed;//借出状态//构造方法//书一开始的状态都是未借出//boolean类型在不赋值的情况下默认人为falsepublic Book(String name, String author, double price, String type) {this.name = name;this.author = author;this.price = price;this.type = type;}@Overridepublic String toString() {return "Book{" +"name='" + name + '\'' +", author='" + author + '\'' +", price=" + price +", type='" + type + '\'' +((IsBorrowed == true)?"已接触":"未借出") +'}';}public void setBorrowed_borrow(){this.IsBorrowed = true;}public void setBorrowed_return(){this.IsBorrowed = false;}public String getName(){return this.name;}public Boolean IsBorrowed(){return this.IsBorrowed;}
}
package behavior;import java.util.Scanner;public class ReturnBehavior implements Behavior {@Overridepublic void work() {System.out.println("归还图书");System.out.println("请输入您要归还的图书名称:");Scanner scanner = new Scanner(System.in);String name = scanner.nextLine();for (int i = 0; i < booklist.usesize(); i++) {if (name.equals(booklist.getBookName(i)) && !booklist.getBook(i).IsBorrowed()){System.out.println("本系统已录入该图书");return;}if (name.equals(booklist.getBookName(i))) {booklist.getBook(i).setBorrowed_return();System.out.println("归还成功!");return;}}System.out.println("这本书不属于本系统!");}
}
package behavior;import java.util.Scanner;public class BorrowedBehavior implements Behavior {@Overridepublic void work() {System.out.println("借阅图书");System.out.println("请输入您要借阅的图书名称:");Scanner scanner = new Scanner(System.in);String name = scanner.nextLine();for (int i = 0; i < booklist.usesize(); i++) {if (name.equals(booklist.getBookName(i)) && booklist.getBook(i).IsBorrowed()){System.out.println("该图书已借出");return;}if (name.equals(booklist.getBookName(i))) {booklist.getBook(i).setBorrowed_borrow();System.out.println("借阅成功!");return;}}System.out.println("没有您要借阅的书籍");}
}
测试
2.2.4.5 增加图书
先判断书架是否是满的
再判断是否存在同名书籍
增加后需要将 usespace++
需要一个 set 方法
package book;public class BookList {private final int defaultcapacity = 10;//默认容量protected Book[] booklist = new Book[defaultcapacity];private int usespace;//已存书本容量public BookList() {this.booklist[0] = new Book("三体", "刘慈欣", 18.88, "小说");this.booklist[1] = new Book("西游记", "吴承恩", 17.77, "小说");this.booklist[2] = new Book("红楼梦", "曹雪芹", 16.66, "小说");usespace = 3;}public int usesize(){
// 存放书本数量return this.usespace;}public int size(){
// 书架总容量return this.booklist.length;}public void setUsespace(int usespace) {//改变书架书的数量this.usespace = usespace;}public void setBooklist(Book book){
// 增加书籍this.booklist[usespace++] = book;}public String getBookName(int index){
// 返回对应位置的书名return this.booklist[index].getName();}public Book getBook(int index){
// 返回对应位置的书return this.booklist[index];}
}
public class AddBehavior implements Behavior{@Overridepublic void work() {System.out.println("添加图书");if (booklist.size() == booklist.usesize()) {System.out.println("书架已满!");return;}//判断书架是否已满Scanner scanner = new Scanner(System.in);System.out.println("请输入您要添加的书籍名称:");String name = scanner.nextLine();for (int i = 0; i < booklist.usesize(); i++) {
// 判断是否存在同名书籍if (name.equals(booklist.getBookName(i))) {System.out.println("系统已收录该图书");return;}}System.out.println("请输入该书的作者");String author = scanner.nextLine();System.out.println("请输入该书的价格");double price = scanner.nextDouble();String type = scanner.nextLine();//接受回车键System.out.println("请输入该书的类型");type = scanner.nextLine();Book book = new Book(name,author,price,type);booklist.setBooklist(book);System.out.println("新增图书成功!");}
}
测试
2.2.4.6 删除图书
找到对应的图书
如果已经借出就不能删除
如果没有借出
将他所在空间置为 null
然后将他所在位置之后的图书向前移动
同样需要在 BookList 类中补充方法
package book;public class BookList {private final int defaultcapacity = 10;//默认容量protected Book[] booklist = new Book[defaultcapacity];private int usespace;//已存书本容量public BookList() {this.booklist[0] = new Book("三体", "刘慈欣", 18.88, "小说");this.booklist[1] = new Book("西游记", "吴承恩", 17.77, "小说");this.booklist[2] = new Book("红楼梦", "曹雪芹", 16.66, "小说");usespace = 3;}public int usesize(){
// 存放书本数量return this.usespace;}public int size(){
// 书架总容量return this.booklist.length;}public void setUsespace(int usespace) {//改变书架书的数量this.usespace = usespace;}public void setBooklist(Book book){
// 增加书籍(放在尾部)this.booklist[usespace++] = book;}public void setBooklist(Book book,int index){
// 增加书籍(指定位置)this.booklist[index] = book;}public void removeBook(int index){//删除对应下标位置的书this.booklist[index] = null;}public String getBookName(int index){
// 返回对应位置的书名return this.booklist[index].getName();}public Book getBook(int index){
// 返回对应位置的书return this.booklist[index];}
}
package behavior;import java.util.Scanner;public class RemoveBehavior implements Behavior {@Overridepublic void work() {System.out.println("删除图书!");Scanner scanner = new Scanner(System.in);System.out.println("请输入您要删除的书籍名称:");String name = scanner.nextLine();for (int i = 0; i < booklist.usesize(); i++) {if (name.equals(booklist.getBookName(i)) && booklist.getBook(i).IsBorrowed()) {
// 已借出不能删除System.out.println("该图书已借出");return;}if (name.equals(booklist.getBookName(i)) && !booklist.getBook(i).IsBorrowed()) {booklist.removeBook(i);//指定位置置空for (int j = i; j < booklist.usesize() - 1; j++) {//指定位置处的书向前移动booklist.setBooklist(booklist.getBook(j + 1), j);}System.out.println("删除成功!");int newspace = booklist.usesize();booklist.setUsespace(--newspace);//删除后 书架中的存储的书的数量减少return;}}System.out.println("没有您要删除的书籍");return;}
}
测试
感谢观看