图书管理系统(java版)

一 整体思路

1.图书图书得先要有书吧,所有我们要有一个图书类,然后我们是一次要管理很多本图书

所以要有一个书架类,来存放多本图书。

2.测试类,用来测试图书管理系统的功能。

3.用户类,用户包括图书管理员,和图书得借阅人员,我们可以定义一个抽象的用户类,

再去定义管理员类,和普通用户类去继承这个抽象的用户类。

4.功能实现类:一个图书管理系统肯定有许多的功能,比如说查看图书,查找图书,借阅图书,删除图书等,我们可以定义一个功能的接口,在分别定义各个功能类去实现这个接口。

以上就是图书管理系统的整体思路,让我们具体实现以下图书管理系统的整体功能吧!

二 . 具体实现

1.图书类和书架类

1.1 图书类

一本书的特征有: 书名,作者,价格,书的类型以及是否被借阅

Book

public class Book {
    private String name; // 书名
    private String author; // 作者
    private double price; // 价格
    private String type; // 类型
    private boolean isBorrowed; // 是否被借出 默认false

    public Book(String name, String author, double price, String type) {
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
    }

    public Book() {
    }

    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 double getPrice() {
        return price;
    }

    public void setPrice(double 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;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ((isBorrowed==true)?"已经被借出":"未被借出")+
              /*  ", isBorrowed=" + isBorrowed +*/
                '}';
    }
}

需要注意的是该书借阅情况在toString方法中,如果isBorrowed的值为false则就打印未被借出,反之就是已被借出.利用三目运算符即可。

1.2书架类

书架类中用来存放多本书,以及书的初始化,在最开始的时候我们就可以在书架中放入几本书,

其中还包括书的数量

BookList

/**
    书架类
 */
public class BookList {
    private Book[] books=new Book[10];
    private int usedSize; // 计数器,当前实际放的书目
    public BookList() {
        // 构造方法,初始化成员
        this.books[0]=new Book("三国演义",";罗贯中",59.9,"文学小说");
        this.books[1]=new Book("西游记","吴承恩",39.9,"文学小说");
        this.books[2]=new Book("红楼梦","曹雪芹",48.9,"文学小说");
        this.usedSize=3;
    }
    public Book getBook(int pos) {
        return books[pos];
    }

    public Book[] getBooks() {
        return books;
    }

    public void setBooks(Book[] books) {
        this.books = books;
    }
public void setBooks(int pos,Book book){
        books[pos]=book;
}

    public int getUsedSize() {
        return usedSize;
    }

    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }
}

书架中默认有三本书

2.图书操作类

这其中包含查看图书,搜索图书,添加,删除图书,借阅,归还图书等

我们可以定义一个图书操作的接口,让其具体的操作区实现这个接口

IOperation 图书操作接口

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

AddOperation

/**
 * 新增图书
 */
public class AddOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("新增图书");
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入书名:");
        String name=sc.nextLine();
        System.out.println("请输入作者:");
        String author=sc.nextLine();
        System.out.println("请输入价格:");
        double price=sc.nextDouble();
        sc.nextLine();
        System.out.println("请输入类型:");
        String type=sc.nextLine();
        Book book = new Book(name, author, price, type);
        int currentSize= bookList.getUsedSize();

        // 如果书已经存在,就不能存在了
        for (int i = 0; i < currentSize; i++) {
            Book book1=bookList.getBook(i);
            if(book1.getName().equals(name)){
                System.out.println("书架已存在该书,不能重复添加");
                return;
            }
            bookList.setBooks(currentSize,book);
            bookList.setUsedSize(currentSize+1);


        }

    }

}

BorrowedOperation

/**
 * 借阅图书
 */
public class BorrowedOperation  implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅图书");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入书名:");
        String name = sc.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

public class DelOperation implements IOperation {
    public DelOperation() {
    }

    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入书名:");
        String name = sc.nextLine();
        // 找到你要删除的书
        int currentSize = bookList.getUsedSize();
        if(currentSize==0){
            System.out.println("书架已经没有书了");
        }
        int index = -1;
        for (int i = 0; i < currentSize; i++) {

            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
                index = i;
                break;
            }
            if(index==-1){
                System.out.println("没有你要删除的书");
                return;
            }
        }
        for (int i = index; i < currentSize - 1; i++) {
            Book book1 = bookList.getBook(i + 1); // 存在越界 所以要currentSize-1
            bookList.setBooks(i, book1);
        }
        bookList.setUsedSize(currentSize-1);



    }
}

ExitOperation

/***
 * 退出系统
 */
public class ExitOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统");
        int currentSize=bookList.getUsedSize();
        for(int i=0;i<currentSize;++i){
            bookList.setBooks(i,null);
        }
        System.exit(0);
    }
}

FindOperation

/***
 * 查找图书
 */
public class FindOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("查找图书");
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入你要查找的图书: ");
        String name=sc.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

/**
 * 归还图书
 */
public class ReturnOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("归还图书");
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入书名:");
        String name=sc.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;
                }else {

                    book.setBorrowed(false);
                    System.out.println("还书成功");
                    break;
                }
            }
        }
        System.out.println("没有你要还的书");
    }
}
 

ShowOperation

/***
 * 显示图书
 */
public class ShowOperation 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.getBook(i);
            System.out.println(book);
        }
    }
}

3.用户类

用户分为图书管理类,和普通用户类,我们可以定义一个抽象类,分别让这两个类去继承这个抽象类,用户类和管理员类的权限不一样,所对应的方法也有所不同

User

/**
 * 用户类
 */
public abstract class User {
    public String name;
    public IOperation[] ioPerations={
            new ExitOperation(), // 退出
            new FindOperation(), // 查找
            new AddOperation(), // 新增
            new DelOperation(),  //删除
            new ShowOperation() //显示图书
    }; //




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

    public User() {
    }
    public abstract int menu();
    public void doOperation(int choice, BookList bookList){
        ioPerations[choice].work(bookList);
    }
}

AdminUser

/**
 * 管理员
 */
public class AdminUser extends User{
    public AdminUser(String name) {
        super(name);
    }
    public AdminUser() {
    }
    public int  menu(){
        System.out.println("**************************");
        System.out.println("hello "+this.name+"欢迎来到管理员菜单");
        System.out.println("1. 查找图书");
        System.out.println("2. 新增图书");
        System.out.println("3. 删除图书");
        System.out.println("4. 显示图书");
        System.out.println("0. 退出系统");
        System.out.println("**************************");
        System.out.println("请输入你的操作: ");
        Scanner sc=new Scanner(System.in);
        int choice=sc.nextInt();
        return choice;

    }
}

NormalUser

/**
 * 普通用户
 */
public class NormalUser extends User {
    public NormalUser(String name) {
        super(name);
        this.ioPerations=new IOperation[]{
                new ExitOperation(), // 退出
                new FindOperation(), // 查找
                new BorrowedOperation() , // 借阅
                new ReturnOperation(),  // 归还
                new ShowOperation() // 显示图书
        };
    }

    public NormalUser() {
    }

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

这里的IOperation[] ioPerations 数组用来存放图书的各个操作,利用多态机制,把各个操作之间联系起来,然后利用int choice下标来调用具体的图书操作方法.

3.测试类

最后一个就是测试类了,根据输入的数字表示是普通用户,还是管理员,然后调用相应的方法

Main

public class Main {
    public static void main(String[] args) {
        BookList bookList=new BookList();
        User user=login();
        while(true){

            int choice=user.menu();
            // user是哪个对象?ret是几--->能确定:我能操作哪个对象的方法?
            // 通过这两个变量 可以确定但是怎么联系起来
        /*
            1.先让双方 存好 对应自己的操作
            2.调用对应的功能
            3.
         */
            user.doOperation(choice,bookList);
            /*System.out.println(user);
            System.out.println(choice);*/
        }

    }
    // 利用返回值得向上转型达到类型的统一性
    public static User login(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的姓名: ");
        String name=sc.nextLine();
        System.out.println("请输入你的身份: 1->管理员 0->普通用户");
        int choice=sc.nextInt();
        if(choice==1){
            return new AdminUser(name);
        }else{
            return new NormalUser(name);
        }


    }
}

三.效果展示

 

 

 

 我们再来试试普通用户类

 

 这里可以看到三国演义这本书已经借出了

 好了以上就是本次的全部内容,感谢大家的收看

  • 12
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qm_hu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值