Java--图书管理系统(新版详细讲解)

前言:

        对于初学者,自己写一个图书管理系统,会有效提高自己的代码能力,加深对Java中面向对象的理解,里面蕴含了Java中的类、接口、继承、多态等思想,接下来我们一起完成这一份"伟大的作品!"

注:以下的是作者自己的思想,大家可做借鉴和改动!

设计思想:

        首先Java中最核心的是什么?

        要想清楚对象之间的关系,我打算把每种对象都归类:

        1、和书有关的东西:书、书架

        2、和人有关的:管理员、普通用户

        3、链接人和书的所有功能:找书、展示所有书籍、借书、还书、结束系统等功能

我们发现大概可以分为三大类,每一类又可以再细分,细分之后还可以找到之间的关系(比如继承关系,扩展接口等等) 。

所以打算建4个包分别是:

        1、Book(实现和书有关的类)

        2、IOperation(实现和功能有关的类)

        3、User(实现和人有关的类)

        4、Main(实现和测试有关的类)

之后再细分:

和书有关的有:书架类、书类

和功能有关的有:找书类、借书类、还书类、查看所有图书类、添加书籍类、删除书籍类、退出系统类

和人相关的:管理员类、普通用户类

        在此基础上,我为了后期方便设计,我还在IOperation包中添加了一个公共接口为了让

后期所有类都可以统一(会在后期说明)

        在User包中添加一个父类,因为普通用户和管理员有共同特点!

Book包中的设计:

        现在首先设计Book包中的两个类:

Book类的设计:

        一本书得有哪些特征:

        1、书名

        2、作者

        3、出版社

        4、类型

        5、价格

        6、是否被借出

        ......

这里我就取前6种类型设计:

为了安全,我将这些变量都设计成private访问形式。(大家也可以设计成protected)

为了能够调用赋值,我给出get和set方法。

同样为了能够初始化:我给出constructor方法。(isBorrow变量不用初始化,一开始默认为false)

public class Book {
    private String name;
    private String author;
    private String publisher;
    private String type;
    private int price;
    private boolean isBorrow;

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

    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 getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    public String getType() {
        return type;
    }

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

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public boolean isBorrow() {
        return isBorrow;
    }

    public void setBorrow(boolean borrow) {
        isBorrow = borrow;
    }
    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", publisher='" + publisher + '\'' +
                ", type='" + type + '\'' +
                ", price=" + price +
                ", isBorrow=" + isBorrow +
                '}';
    }//重写打印的时候的内容
}

BookList类的设计:

        书架类的设计:

        1、书架上肯定有好书,这里可以用Book类型的数组表示

        2、书架上得记录书的数量,在这里给出num变量。

 对这两个变量给出set和get方法,并且给出构造方法进行初始化:

public class BookList {
    private Book[] books;
    private int num;

    public BookList( ) {
        this.books = new Book[10];//该书架上一共可以放10本书
        books[0] = new Book("三国演义","罗贯中","人民文学","小说",10);
        books[1] = new Book("水浒传","施耐庵","人民文学","小说",9);
        books[2] = new Book("Java","高斯林","外国文学","文学",8);
        this.num = 3;
    }

     public Book getBook(int i) {
        return books[i];
    }

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

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }
}

User包中的设计:

User类的设计:

        不管是普通人员或是管理人员共有:

        1、姓名

        2、菜单的打印

由于这个类我们不会直接使用里面的方法,可以设计为抽象类! 

public abstract class User {
    protected String name;//为了能够在子类中调用设计为protected

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public abstract int menu();
}

 AdmiUser类的设计:

一定要继承父类,然后可以重写菜单方法,到时候可以用多态打印相应的菜单!

public class AdmiUser extends User{
    public AdmiUser(String name) {
        super(name);
        this.name = name;
    }
    public 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.退出系统");
        System.out.println("************************");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

NomalUser类的设计:

和AdmiUser是一样的,只不过菜单有点区别:

public class NomalUser extends User {
    public NomalUser(String name) {
        super(name);
        this.name = name;
    }
    public int menu(){
        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);
        int choice = scanner.nextInt();
        return choice;
    }
}

Main包中的设计:

Main类的设计:

        作为程序的入口,我们应该想好,一开始的界面:

        "请输入姓名:"

        "请选择你的身份:"

        之后选择哪个就打印哪个菜单:

public class Main {
    public static User menu(){
        System.out.println("请输入你的姓名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("请选择你的身份:1.管理员  2.普通用户");
        int a = scanner.nextInt();
        while(true) {
            if (a == 1) {
                return new AdmiUser(name);
            } else if (a == 2){
                return new NomalUser(name);
            }else{
                System.out.println("选择错误,请重新选择:");
            }
        }
    }

    public static void main(String[] args) {
        BookList bookList = new BookList();
        User user = menu();
        int choice = user.menu();
        
    }
}

当有这个界面就可以了,当然这时候还差如何把这些功能和链接进去:

此时就需要一个数组,然后把这些对应功能放在对应每个用户方法里面:

到时候就可以调用相应的功能功能了:


此时User类中就需要将数组放进去,但是这些功能的类型都不一样,如果是数组的话用改保持类型一样菜可以放,这时候我一开始定义的接口就有用了,如果此时,哪些所有功能能够扩展我的接口,那么就可以用到向上转型的知识,将所有类放进同一个数组里面:

修改User包:

将上述思想运用到User类、AdmiUser类、NomalUser类中:

User类:

public abstract class User {
    protected String name;
    protected Ioperation[] ioperations;

    public Ioperation[] getIoperations() {
        return ioperations;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public abstract int menu();
}

AdminUser类:

public class AdmiUser extends User{
    public AdmiUser(String name) {
        super(name);
        this.name = name;
        this.ioperations = new Ioperation[]{new ExitOperation(),new FindOperation(),new AddOperation()
        , new DelOperation(),new ShowOperation()};
    }
    public 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.退出系统");
        System.out.println("************************");
        System.out.println("请选择你需要的功能:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

NomalUser类:

public class NomalUser extends User {
    public NomalUser(String name) {
        super(name);
        this.name = name;
        this.ioperations = new Ioperation[]{new ExitOperation(),new FindOperation(),new BorrowOperation()
                ,new ReturnOperation()
                };
    }
    public int menu(){
        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);
        int choice = scanner.nextInt();
        return choice;
    }
}

注意:

        在进行数组初始化的时候,所有的功能必须都得扩展Ioperation接口,否则会报错!!

修改Main类:

public class Main {
    public static User menu(){
        System.out.println("请输入你的姓名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("请选择你的身份:1.管理员  2.普通用户");
        int a = scanner.nextInt();
        while(true) {
            if (a == 1) {
                return new AdmiUser(name);
            } else if (a == 2){
                return new NomalUser(name);
            }else{
                System.out.println("选择错误,请重新选择:");
            }
        }
    }

    public static void main(String[] args) {
        BookList bookList = new BookList();
        User user = menu();
        int choice = user.menu();
        user.getIoperations()[choice].work(bookList);//这一点很重要
        //这个会在相关IOperation包中说明
    }
}

IOperation包中的设计:

Ioperation接口的设计:

        该接口必须有,原因在Main设计的时候说过了,单纯只有这一个包还不行,该需要有方法,之后所有的类的方法都需要进行多态实现:

        设计一个work方法,IOperation包中所有类中都需要重写该方法:

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

 注意:

        必须传入书架这个类型参数,因为我们每个功能都需要借助书架来实现。

 AddOperation类的设计:

        首先要增加书,我觉得是常规操作,直接上代码:

public class AddOperation implements Ioperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("增加书籍!");
        System.out.println("请输入要增加书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("请输入要增加书的作者:");
        String author = scanner.nextLine();
        System.out.println("请输入要增加书的出版社:");
        String publisher = scanner.nextLine();
        System.out.println("请输入要增加书的类型:");
        String type = scanner.nextLine();
        System.out.println("请输入要增加书的价格:");
        int price = scanner.nextInt();
        Book book = new Book(name,author,publisher,type,price);
        bookList.setBooks(book, bookList.getNum());
        bookList.setNum(bookList.getNum()+1);
        System.out.println("增加成功!");
    }
}

BorrowOperation类的设计:

可以根据我的代码看出我的设计思想!!

代码如下:

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

        System.out.println("借阅图书!");
        System.out.println("请输入你要借阅的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int sum = bookList.getNum();
        int i= 0;
        for (i = 0; i< sum; i++) {
            if(bookList.getBook(i).getName().equals(name)) {
                System.out.println("已找到你要借阅的书!");
                if(bookList.getBook(i).isBorrow()){
                    System.out.println("不好意思,已被借出");
                    return ;
                }else{
                    System.out.println("借书成功!");
                    bookList.getBook(i).setBorrow(true);
                }
            }

        }
        if(i >= sum){
            System.out.println("这里没有你想借阅的书");
        }

    }

}

DelOperation类的设计:

代码如下:

public class DelOperation implements Ioperation{
    @Override
    public void work(BookList bookList){

        System.out.println("删除图书!");
        System.out.println("请输入你要删除书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int sum = bookList.getNum();
        int i = 0;
        for(;i<sum;i++){
            if(bookList.getBook(i).getName().equals(name)){
                System.out.println("已找到,书的信息如下,确定删除?");
                System.out.println(bookList.getBook(i));
                System.out.println("1.确定  2.取消");
                int a = scanner.nextInt();
                if(a == 1){
                    int j = i;
                    for (j = i; j < sum-1; j++) {
                        Book book = bookList.getBook(j+1);
                        bookList.setBooks(book,j);
                    }
                    bookList.setBooks(null,j);
                    System.out.println("删除成功!");
                    break;
                }else {
                    return ;
                }
            }
        }
        if(i>=sum) {
            System.out.println("未找到你要删除的相关书籍!");
        }
    }
}

ExitOperation类的设计:

public class ExitOperation implements IOperation {
    public void work(BookList bookList){
        System.out.println("退出系统!");
        System.exit(0);
    }

}

这里调用了Systm中的exit方法,这里传一个可以让程序正常结束的值!

FindOperation类的设计:

public class FindOperation implements Ioperation{
    @Override
    public void work(BookList bookList){
        System.out.println("查找图书!");
        System.out.println("请输入你要查找树的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int currentSize = bookList.getNum();
        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("没有找到这本书,书名为"+name);
    }

}

ReturnOperation类的设计:
 

public class ReturnOperation implements Ioperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("归还图书!");
        System.out.println("请输入要归还图书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int sum = bookList.getNum();
        int i = 0;
        for (; i < sum; i++) {
            if(bookList.getBook(i).getName().equals(name)){
                if(bookList.getBook(i).isBorrow()) {
                    System.out.println("归还成功!");
                    bookList.getBook(i).setBorrow(false);
                } else{
                    System.out.println("该书不在我们地点归还!");
                }

            }
        }
        if(i>sum){
            System.out.println("该书不属于我们这里!");
        }
    }
}

ShowOperation类的设计:

        我在设计的时候,加入了一个自动按价格排序!!

也就是在Book类中外接一个Compable接口:

之后重写compareTo方法:

就可以进行自动排序啦!!

代码如下:
 

public class ShowOperation implements Ioperation {
    @Override
    public void work(BookList bookList){

        System.out.println("展示图书!");
        int num = bookList.getNum();
        for (int i = 0; i < num-1; i++) {
            for (int j = 0; j <num-1-i ; j++) {
                if (bookList.getBook(j).compareTo(bookList.getBook(j + 1)) > 0) {
                    Book book = bookList.getBook(j);
                    bookList.setBooks(bookList.getBook(j + 1), j);
                    bookList.setBooks(book, j + 1);
                }
            }
        }
        for (int i = 0; i < num; i++) {
            System.out.println(bookList.getBook(i));
        }

    }
}

后记:

  当然这是我的一个整个设计思路:

希望大家看完有所收获,受到启发!!

我还是希望大家都有自己的想法,可以设计自己的图书管理系统!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值