Java实现简单的图书管理系统

学习了Java的基础语法之后,可以使用这些基础语法知识来实现一个简单的图书管理系统。在这个图书馆里系统中,设置了两种用户,分别是管理员普通用户,两种用户所实现的功能也有所区别。写图书管理系统的时候,每个人可以根据自己的想法来实现自己想要的功能

管理员的功能实现效果:查找图书

 添加图书

 删除图书和显示图书

退出系统

下面看一下普通用户功能的实现效果:查找图书

 借阅图书

归还图书 和 退出系统

 以上就是本次实现的简单的图书管理系统,管理员可以删除书架中的图书,再查阅时会发现图书真的被删掉了;也可以添加图书等。普通用户可以借阅图书,借阅成功之后,再查阅本书时发现这本书已经被借出了,再归还后,再查阅本书发现此书又被归还了。因此本次实现的简单的图书管理系统还是比较智能的,下面就看看怎么具体实现这些功能。

1.创建相应的包和类

图书管理系统中,一共有四大模块:

(1)Main类:控制中心,调用登录方法,选择用户,进入用户菜单。

(2)user用户模块:选择要操作的功能。

(3)operation操作模块:用来完成七大功能的具体实现。

(4)book书籍模块:用来存放书籍的有关信息。

2.Main类


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 ManagerUser(name);
        }else {
            return new OrdinaryUser(name);
        }

    }
    public static void main(String[] args) {
        User user = login();//向上转型
        BookList bookList = new BookList();
        while (true){
            int choice = user.menu();
            user.doOperation(choice,bookList);
        }
    }
}

3.user包-用户模块

3.1 User类-用户中心

User类作为父类,将用户的成员进行初始化,建立起用户要完成的操作

abstract public class User {
    public String name;
    public IOperation[] iOperations = new IOperation[10];

    public User(String name) {
        this.name = name;
    }

    abstract public int menu();
    public void doOperation(int pos,BookList bookList){
        this.iOperations[pos].work(bookList);
    }

}

3.2  ManagerUser类-管理员用户

在main方法中 选择调用 管理员菜单,进入管理员菜单,选择相应操作

public class ManagerUser extends User{
    public ManagerUser(String name) {
        super(name);
        this.iOperations = new IOperation[]{ new Exit(),
                                    new FindBook(),
                                    new ShowBook(),
                                    new DeletBook(),
                                    new AddBook()
        };
    }
    @Override
    public int menu() {
        System.out.println("***********************");
        System.out.println(this.name+",欢迎来到管理员菜单!");
        System.out.println("请输入你的操作:0.退出系统" +
                " 1.查找图书 " +
                " 2.显示图书 " +
                " 3.删除图书 " +
                " 4.添加图书 ");
        System.out.println("***********************");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;

    }
}

 3.3 OdinaryUser类-普通用户

在main方法中选择调用普通用户菜单,进入普通用户菜单,选择相应操作

public class OrdinaryUser extends User{

    public OrdinaryUser(String name) {
        super(name);
        this.iOperations = new IOperation[]{ new Exit(),
                new FindBook(),
                new BorrowBook(),
                new ReturnBook(),
        };
    }

    @Override
    public int menu() {
        System.out.println("***********************");
        System.out.println(this.name+",欢迎来到普通用户菜单!");
        System.out.println("请输入你的操作:0.退出系统" +
                " 1.查找图书 " +
                " 2.借阅图书 " +
                " 3.归还图书 ");
        System.out.println("***********************");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

 4.operation包-操作模块

4.1 AddBook类-添加图书功能

public class AddBook implements IOperation{
    @Override
    public void work(BookList bookList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("添加图书!");
        System.out.println("请输入书名:");
        String name = scanner.nextLine();
        int currentSize = bookList.getUsedSize();//当前有几本书
        for (int i = 0; i < currentSize; i++) {
            Book book1 = bookList.getBooks(i);//获取当前下标的书
            if(book1.name.equals(name)){
                System.out.println("书架中已有此书,不可添加!");
                return;
            }
        }//没有同名的书,继续输入书籍信息
        System.out.println("请输入作者:");
        String author = scanner.nextLine();
        System.out.println("请输入类型:");
        String type = scanner.nextLine();
        System.out.println("请输入价格:");
        int price = scanner.nextInt();
        Book book = new Book(name,author,type,price);
        bookList.setBooks(book,currentSize);
        System.out.println("添加成功!");
        System.out.println(book);
        bookList.setUsedSize(currentSize+1);//添加图书成功,书架中书的数量加1
    }
}

4.2 BorrowBook类-借阅图书功能

public class BorrowBook implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅图书!");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书名:");
        String name = scanner.nextLine();
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBooks(i);
            if (book.name.equals(name)) {//在书架中寻找该书名,寻找到即借阅成功
                book.isBorrowed = true;//该书的状态调整为 已被借出
                System.out.println("借阅成功!");
                System.out.println(book);//显示该书,确定该书 被 借阅成功
                return;//找到该书结束该功能,否则继续向下执行
            }
        }
        System.out.println("书架中没有这本书,借阅失败!");

    }
}

4.3 DeletBook类-删除图书功能

public class DeleteBook implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书!");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书名:");
        String name = scanner.nextLine();
        int index = -1;
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBooks(i);//获取此下标的书本
            if(book.name.equals(name)){//若找到该书
                index = i;//记录该书的下标
                break;
            }
        }
          if(index == -1){
            System.out.println("书架中没有这本书,删除失败!");
            return;
        }
        for (int i = index; i < currentSize; i++) {
            Book book = bookList.getBooks(i+1);
            bookList.setBooks(book,i);
        }
            System.out.println("删除成功!");
            bookList.setUsedSize(currentSize-1);

    }
}

4.4 Exit类-退出系统

public class Exit implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统!");
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBooks(i);
            book = null;//把书架置为空
        }
        System.exit(0);

    }
}

4.5 FindBook类-查找图书功能

public class FindBook implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("查找图书!");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书名:");
        String name = scanner.nextLine();
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBooks(i);
            if(book.name.equals(name)){//找到同名的书即查找到
                System.out.println("找到了这本书!");
                System.out.println(book);//显示该书
                return;//找到了就终止该功能,否则没找到
            }
        }
        System.out.println("书架中没有这本书!");

    }
}

4.6 ReturnBook类-归还图书功能

public class ReturnBook implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("归还图书!");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书名:");
        String name = scanner.nextLine();
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBooks(i);
            if(book.name.equals(name)){
                System.out.println("归还成功!");
                book.isBorrowed = false;//归还成功,修改书的状态
                System.out.println(book);//确定图书状态是否被归还成功
                return;
            }
        }
        System.out.println("书架中没有这本书,归还失败!");

    }
}

4.7 ShowBook类-显示图书功能

public class ShowBook implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("显示图书!");
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBooks(i);
            System.out.println(book);
        }
    }
}

4.8 IOperation接口-建立实现功能的标准 

public interface IOperation {
    void work(BookList bookList);
}

5. book包-书籍模块

5.1 Book类-初始化书籍信息

public class Book {
    //定义书的属性
    public String name;//书名
    public String author;//作者
    public String type;//类型
    public int price;//价格
    public  boolean isBorrowed;//状态(是否被借出)

    public Book(String name, String author, String type, int price) {
       //构造方法,初始化成员变量
        this.name = name;
        this.author = author;
        this.type = type;
        this.price = price;
    }

    @Override
    public String toString() {
        //重写toString方法
        return "Book{" +
                "书名:'" + name + '\'' +
                ", 作者:'" + author + '\'' +
                ", 类型:'" + type + '\'' +
                ", 价格:'" + price + '\'' +
                ((this.isBorrowed)?", 已被借出!":", 未被借出!") +
                '}';
    }
}

5.2 BookList类-存放书籍

public class BookList {
    Book[] books = new Book[10];
    public int usedSize;

    public BookList() {
        this.books[0] = new Book("大话数据结构","程杰","学习",50);
        this.books[1] = new Book("明兰传","关心则乱","小说",47);
        this.usedSize = 2;//记录当前书架有几本书
    }

    public Book getBooks(int pos) {//获取书籍
        return books[pos];
    }

    public void setBooks(Book books,int pos) {//设置书籍
        this.books[pos] = books;
    }

    public int getUsedSize() {//获取书架中书的数量
        return usedSize;
    }

    public void setUsedSize(int usedSize) {//更新书架中书的数量
        this.usedSize = usedSize;
    }
}

以上则是本次图书管理系统的全部模块的代码。

  • 4
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值