目录
前置知识点
学习了Java的基础语法、类和对象、继承和多态以及抽象类和接口等后,我们就可以借此完成一个基础的图书信息管理系统来巩固知识。
项目部署说明
1.Java版本为17
2.开发环境:IntelliJ IDEA 2022.1.4
项目运行截图

运行程序,输入姓名并选择身份权限,我们得到了管理员权限的菜单

系统内置了三本图书,图书信息以Book{书名,作者,价格,类型,借出状态}的形式展示。


我们新增一本名为水浒传的图书并查看到它被成功加入书架。
其余的的删除图书以及普通用户权限的借阅图书和归还图书功能这里不做展示。
项目结构展示

项目结构分为三个包和一个主程序类,book包中为书和书架,ioperations包中为各种管理操作实现,user包中为用户权限,Main类作为主程序运行。
项目编写构思

首先创建出本项目使用到的三个包以及主程序类,这四个文件属于并列关系。
book包
Book类
在这个类中,我们需要给图书添加成员变量name,author,price,type和isBorrowed来描述图书。
java
public class Book {
private String name;
private String author;
private int price;
private String type;
private boolean isBorrowed;
//书名,作者,价格,类型以及借出状态
public Book(String name, String author, int price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
//构造方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isBorrowed() {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
//使用getter and setter确保项目封装性
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
((isBorrowed==true)?"已借出":" 未借出")+
//", isBorrowed=" + isBorrowed +
'}';
}//重写toString方法方便打印图书信息
}
Booklist类
这个类用于存放图书信息
java
public class BookList {
private Book[] books = new Book[10];
//创建一个Book类型的数组存放图书信息
private int usedSize;//实际放书个数
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
public BookList(){
this.books[0] = new Book("三国演义","罗贯中",10,"小说");
this.books[1] = new Book("西游记","吴承恩",59,"小说");
this.books[2] = new Book("红楼梦","曹雪芹",16,"小说");
//书架初始存放三本图书
this.usedSize = 3;
//此时实际存量为三本
}
public Book getBook(int pos) {
return books[pos];
}
public void setBook(int pos,Book book){
this.books[pos]=book;
}
public void setBooks(int pos,Book book) {
this.books[pos] = book;
}
public Book[] getBooks(){
return books;
}
}
ioperations包
IOPeration接口
这个接口提供一个work方法供所有操作方法重写
java
public interface IOPeration {
void work(BookList bookList);
}
AddOperation类
这个类用于新增图书操作
java
public class AddOperation implements IOPeration{
public void work(BookList bookList){
System.out.println("新增图书......");
//1.判满
int currentSize=bookList.getUsedSize();
if(currentSize==bookList.getBooks().length){
System.out.println("书架满了 不能放了......");
return;
}
//2.构建对象
Scanner scanner=new Scanner(System.in);
System.out.println("请输入书名:");
String name=scanner.nextLine();
System.out.println("请输入作者:");
String author=scanner.nextLine();
System.out.println("请输入书的类型:");
String type=scanner.nextLine();
System.out.println("请输入价格:");
int price = scanner.nextInt();
Book newBook=new Book(name,author,price,type);
//3.判断书架有没有这本书
for (int i = 0; i < currentSize; i++) {
Book book=bookList.getBook(i);
if(book.getName().equals(name)){
System.out.println("有这本书,不能插入......");
return;
}
}
//4.插入这本书
bookList.setBooks(currentSize,newBook);
bookList.setUsedSize(currentSize+1);
System.out.println("新增图书成功!!!");
}
}
BorrowOperation类
这个类用于借阅图书操作
java
public class BorrowOperation implements IOPeration{
public void work(BookList bookList){
System.out.println("借阅图书......");
Scanner scanner = new Scanner(System.in);
System.out.printf("请输入你借阅的书名:");
String name=scanner.nextLine();
int currentSize=bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book=bookList.getBook(i);
if(book.getName().equals(name)){
if(book.isBorrowed()==true){
System.out.println("这本书已经被借出了");
return;
}
book.setBorrowed(true);
System.out.println("借阅成功!!!");
return;
}
}
System.out.println("没有你要借阅的这本书......");
}
}
DelOperation类
这个类用于删除图书操作
java
public class DelOperation implements IOPeration{
public void work(BookList bookList){
System.out.println("删除图书......");
Scanner scanner = new Scanner(System.in);
System.out.print("请输入你要删除的书名:");
String name=scanner.nextLine();
int currentSize=bookList.getUsedSize();
int pos=-1;
int i=0;
//找到这本书
for (; i < currentSize; i++) {
Book book=bookList.getBook(i);
if(book.getName().equals(name)){
pos=i;
break;
}
if(i==currentSize){
System.out.println("没有你要删除的书!!!");
return;
}
}
for (int j = pos; j < currentSize-1; j++) {
Book book=bookList.getBook(j+1);
bookList.setBooks(j,book);
}
bookList.setBook(currentSize-1,null);
bookList.setUsedSize(currentSize-1);
System.out.println("删除成功!!!");
}
}
FindOperation类
这个类用于查找图书操作
java
public class FindOperation implements IOPeration{
public void work(BookList bookList){
System.out.println("查找图书......");
Scanner scanner = new Scanner(System.in);
System.out.printf("请输入你的书名:");
String name=scanner.nextLine();
int currentSize=bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book=bookList.getBook(i);
if(book.getName().equals(name)){
System.out.println("找到了这本书:");
System.out.println(book);
return;
}
}
System.out.println("没有你要找的这本书......");
}
}
ReturnOperation类
这个类用于归还图书操作
java
public class ReturnOperation implements IOPeration{
public void work(BookList bookList){
System.out.println("归还图书......");
Scanner scanner = new Scanner(System.in);
System.out.printf("请输入你归还的书名:");
String name=scanner.nextLine();
int currentSize=bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book=bookList.getBook(i);
if(book.getName().equals(name)){
if(book.isBorrowed()==true){
book.setBorrowed(false);
System.out.println("归还成功");
return;
}
}
}
System.out.println("没有你要归还的图书!!!");
}
}
ShowOperation类
这个类用于展示全部图书操作
java
public class ShowOperation implements IOPeration{
public void work(BookList bookList){
System.out.println("显示图书......");
int currentSize=bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
System.out.println(book);
}
}
}
ExitOperation类
这个类用于退出系统操作
java
public class ExitOperation implements IOPeration{
public void work(BookList bookList){
System.out.println("退出系统......");
System.exit(0);
}
}
以上这些操作都实现IOPeration接口并重写work方法
user包
User类
作为管理员权限AdminUser类和普通用户权限NormalUser类的父类
java
public abstract class User {
protected String name;
protected IOPeration[] ioPerations;
public User(String name){
this.name=name;
}
public abstract int menu();
public void doIoperation(int choice, BookList bookList){
ioPerations[choice].work(bookList);
}
}
AdminUser类
这个类实现管理员操作权限
java
public class AdminUser extends User{
public AdminUser(String name){
super(name);
this.ioPerations=new IOPeration[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new ShowOperation(),
};
}
public int menu(){
System.out.println("欢迎"+this.name+"来到图书管理系统");
System.out.println("******管理员菜单******");
System.out.println("1.查找图书");
System.out.println("2.新增图书");
System.out.println("3.删除图书");
System.out.println("4.显示图书");
System.out.println("0.退出系统");
System.out.println("*********************");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的操作:");
int choice = scanner.nextInt();
return choice;
}
}
NormalUser类
这个类实现普通用户权限
java
public class NormalUser extends User{
public NormalUser(String name){
super(name);
this.ioPerations = new IOPeration[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation(),
};
}
public int menu(){
System.out.println("欢迎"+this.name+"来到图书管理系统");
System.out.println("******普通用户菜单******");
System.out.println("1.查找图书");
System.out.println("2.借阅图书");
System.out.println("3.归还图书");
System.out.println("0.退出系统");
System.out.println("*********************");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的操作:");
int choice = scanner.nextInt();
return choice;
}
}
Main类
java
public class Main {
public static User login(){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的姓名:");
String name = scanner.nextLine();
System.out.println("请输入你的身份:1:管理员 2:普通用户");
int choice =scanner.nextInt();
if(choice==1){
return new AdminUser(name);
}else{
return new NormalUser(name);
}
}
public static void main(String[] args) {
BookList bookList = new BookList();
User user = login();
while (true) {
int choice = user.menu();
//根据这个返回值决定调用哪个方法
user.doIoperation(choice, bookList);
}
}
}
main函数实例化Booklist,使用login函数获取登录用户姓名和权限,然后在死循环中不断读取指令并执行操作。