栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【Java】实现一个图书管理系统

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Java】实现一个图书管理系统

系统功能:

1.管理员:增添图书、删除图书、查找图书、查看所有图书

2.普通用户:查找图书、借阅图书、归还图书、查看所有图书

系统实现:

book包

在新建的book包目录下新建Book类:

  • 创建成员变量:书名name、作者author、类型type、借阅情况ifBorrow
  • 创建可以初始化成员变量和获取成员变量的Getter and Setter方法
  • 创建打印函数toString
public class Book {
    private String name;     //书名
    private String author;   //作者
    private String type;     //类型
    private boolean ifBorrow;//借阅情况

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

    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 String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public boolean isIfBorrow() {
        return ifBorrow;
    }

    public void setIfBorrow(boolean ifBorrow) {
        this.ifBorrow = ifBorrow;
    }

    //打印
    @Override
    public String toString() {
        return "Book{" +
                "name:'" + name + ''' +
                ", author:'" + author + ''' +
                ", type:'" + type + ''' +", status: "+((ifBorrow == false)?"available":"borrowed")+
                 //", ifBorrow=" + ifBorrow +
                '}';
    }
}

在book包底下创建BookList类,以表示书架:

  • 成员变量:Book类型的数组books(用于存放书籍)、书架书本数目usedSized
  • 构造方法BookList:初始化基本书籍和书本数目
  • 创建方法以设置或得到成员变量的Getter and Setter
public class BookList {

    private Book[] books = new Book[10];  //创建Book类型数组
    private int usedSize;    //已使用的书架空间(书本数目)

    //书架
    public BookList(){
        books[0] = new Book("Java","Stephen","Tools",false);
        books[1] = new Book("C programming","Johnathan","Tools",false);
        books[2] = new Book("JavaScript","Alan","Tools",false);
        usedSize = 3;
    }

    //获得现书架书本数目
    public int getUsedSize() {
        return usedSize;
    }

    //设置书架书本数目
    public void setUsedSize(int newSize){
        usedSize = newSize;
    }

    //获得书架上的某本特定的书,pos为数组下标
    public Book getBooks(int pos){
        return books[pos];
    }

    //设置书本上某本特定的书
    public void setBooks(int pos,Book book){
        books[pos] = book;
    }

}

operation包

新建一个包叫operation,把对书籍操作的类都放在operation包底下

在operation包底下创建IOperation接口,操作有关的类都要先实现IOperaton接口来进行操作

IOperation

创建一个抽象方法work,传入的参数为BookList类型的bookList(可理解成传入参数为书架,来进行对书架的操作)

public interface IOperation {
    //接口抽象方法
    void work(BookList bookList);
}

接下来先实现管理员的有关操作:增添图书、删除图书、查找图书、查看所有图书

AddOperation(增添图书)
public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {

        Scanner scanner = new Scanner(System.in);

        //输入书籍信息
        System.out.println("enter book name:");
        String name = scanner.nextLine();
        System.out.println("enter author name:");
        String author = scanner.nextLine();
        System.out.println("enter book type:");
        String type = scanner.nextLine();

        boolean ifBorrow = false;  //因为刚加进去的书一定是没有借出的,所以就直接初始化为false
        
        //新建Book类的book,并将成员变量赋值为上面输入的值
        Book book = new Book(name,author,type,ifBorrow);  

        //通过bookList引用方法getUsedSize来得到现书本数目
        int currentSized = bookList.getUsedSize();   

        //设置书本,传入参数为currentSized和book,结果就是books[currentSize] = book
        bookList.setBooks(currentSized,book);     

        //新增完一本书,书架的书就多了1
        bookList.setUsedSize(currentSized+1);

        System.out.println("add operation is done");
    }
}
DeleteOperation(删除图书)

思路:找到要删除的书籍下标,将后一本书覆盖前一本书

public class DeleteOperation implements IOperation{
    @Override
    public void work(BookList bookList) {

        Scanner scanner = new Scanner(System.in);

        //输入删除的书名
        System.out.println("enter the book name to delete:");
        String name = scanner.nextLine();

        int currentSize = bookList.getUsedSize();   //得到现有书本数目

        int delPos = 0;    //所删除的书所在的数组下标
        int size = currentSize;

        //遍历书架,得到要删除书籍的数组下标
        for(int i = 0;i 
DisplayOperation(查看所有图书) 

遍历输出即可

public class DisplayOperation implements IOperation{
    @Override
    public void work(BookList bookList) {

        int currentSize = bookList.getUsedSize();

        for(int i = 0; i 
SearchOperation(按书名查找图书) 

输入书名,遍历数组

public class SearchOperation implements IOperation{
    @Override
    public void work(BookList bookList) {

        Scanner scanner = new Scanner(System.in);

        //输入书名
        System.out.println("enter book name:");
        String name = scanner.nextLine();

        int currentSize = bookList.getUsedSize();

        //遍历
        for(int i = 0;i 

接下来是普通用户的相关操作:查找图书、借阅图书、归还图书、查看所有图书

其中查找、查看所有图书的操作和管理员是一样的

BorrowOperation(借阅图书)

输入书名,遍历数组,将图书借阅情况修改为true

public class BorrowOperation implements IOperation{
    @Override
    public void work(BookList bookList) {

        Scanner scanner = new Scanner(System.in);

        //输入书名
        System.out.println("enter the book you intend to borrow:");
        String name = scanner.nextLine();

        int currentSize = bookList.getUsedSize();

        //遍历
        for (int i = 0;i 
ReturnOperation(归还图书) 

输入书名,遍历数组,将图书借阅情况修改为true

public class ReturnOperation implements IOperation{
    @Override
    public void work(BookList bookList) {

        Scanner scanner = new Scanner(System.in);

        //输入书名
        System.out.println("enter the book you want to return:");
        String name = scanner.nextLine();

        int currentSized = bookList.getUsedSize();

        //遍历
        for(int i = 0;i 

最后加上一个退出系统的操作

ExitOperation(退出系统)

用到系统函数System.exit(),参数为0

public class ExitOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("Exit System");
        System.exit(0);  //系统函数
    }
}

user包

新建user包,在此包下创建相关类

User(用户)

成员变量为用户名name和IOperation类型的数组iOperations(用于存放操作方法)

public abstract class User {
    protected String name;   //用户姓名
    protected IOperation[] iOperations;   //关于operation的数组,数组对象根据用户而定

    //构造方法
    public User(String name) {
        this.name = name;
    }

    //不同用户不同菜单,抽象方法
    public abstract int menu();

    //通过choice来执行对书架的操作
    public void doOperation(int choice, BookList bookList){
        this.iOperations[choice].work(bookList);
    }
}
Administer(管理员)

继承User类

在构造方法Adminster中将成员变量数组iOperation初始化新建管理员所需要的方法类,注意在新建的顺序要和用户菜单的序号相对应,以便对数组成员的直接访问

如菜单中0为Exit System,那么数组下标为0的类应为ExitOperation

其中菜单的返回值为choice,即为选择,所以每次菜单显示之后,用户都应该要有操作选项的输入

public class Administer extends User{

    public Administer(String name) {
        super(name);

        //新建管理员所需要的方法对象
        this.iOperations = new IOperation[]{
                new ExitOperation(),
                new AddOperation(),
                new DeleteOperation(),
                new DisplayOperation(),
                new SearchOperation()
        };
    }

    @Override
    public int menu() {
        Scanner scanner = new Scanner(System.in);
        System.out.println();
        System.out.println("Welcome to Library Management System.");
        System.out.println();
        System.out.println("1.Add books");
        System.out.println("2.Delete books");
        System.out.println("3.Display books");
        System.out.println("4.search books");
        System.out.println("0.Exit System");
        System.out.println("please choose an option:");

        int choice = scanner.nextInt();  //选项

        return choice;
    }
}
NormalUser(普通用户)

与管理员同理

public class NormalUser extends User{
    public NormalUser(String name) {
        super(name);

        this.iOperations = new IOperation[]{
                new ExitOperation(),
                new SearchOperation(),
                new BorrowOperation(),
                new DisplayOperation(),
                new ReturnOperation()
        };

    }

    @Override
    public int menu() {
        Scanner scanner = new Scanner(System.in);
        System.out.println();
        System.out.println("Welcome to Library.");
        System.out.println();
        System.out.println("1.Search books");
        System.out.println("2.Borrow books");
        System.out.println("3.Display books");
        System.out.println("4.return books");
        System.out.println("0.Exit System");
        System.out.println("please choose an option:");
        int choice = scanner.nextInt();

        return choice;

    }
}

主函数  Login函数

登录的初始界面,用于选择登录身份(管理员或普通用户)

函数返回值为User类型,通过用户选择来新建不通过的用户类型来返回

    public static User Login(){
        System.out.println("please enter your name:");

        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        //身份选择
        System.out.println("please choose your identity:");
        System.out.println("1.Administer  2.NormalUser");

        int identity = scanner.nextInt();

        if(identity == 1){
             return new Administer(name);  //管理员
        }else{
            return new NormalUser(name);  //普通用户
        }
    }
主函数

整合逻辑即可运行

    public static void main(String[] args) {

        BookList bookList = new BookList();  //新建对象

        User user = Login();  //Login返回的是一个User对象,要定义新的user来接收这个对象

        int choice = user.menu();  //接受menu的返回值

        while(choice != 0){
            user.doOperation(choice,bookList);   //通过引用doOperation来执行进一步操作
            choice = user.menu();   //再次选择操作选项
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1000468.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号