图书管理系统Part1(封装,继承,多态,接口的练习)

前言:图书管理系统是我们在学习编程方面常见的小项目,在学习完类和对象,封装,继承,多态和接口之后,我们就可以用这些思想来实现我们的图书管理系统,这里我会用两个博客来阐述具体实现方法。

博客链接:1.第二篇 part2  2.第三篇 part3​​​​​​​

​​​​​​​均已写完,欢迎连续观看。

1.业务需求

可以看到整体分为两种用户,不同用户显示不同的菜单,可以执行不同的操作,例如,借阅,归还,显示,删除,新增等。

2.整体框架的搭建

这里我们全部代码运用Idea来编写,由于我们的主要目标为练习多态,接口等思想,所以整体框架的搭建尤为重要。

首先我们在工程下新建一个Mai类,用来写main方法,在新建一个Book包,包内新建两个类,一个Book,为每本书的所有信息,BookList,为书架,以后新建一个Book类型的数组存放所有书籍。在定义一个User包,里头定义3个类,因为我们有两种用户,普通用户和管理员,分别定义两个PublicUser和AderminUser类作为子类,User类作为他们的父类,再定义一个Operation包,里头定义的类均为各种操作的方法,例如,借阅,归还,显示,删除,新增等。至此我们所有的类都创建完毕。

 其中Operation包内为将要执行的所有操作,我们把包内每个类的操作方法都命名为work,所有操作都是在bookList中完成,所以所有类都具备了接口的条件,我们可以写一个接口,且包内所有类都引用这个接口,例如:

public class AddOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("增加图书!");
    }
}


public class BorrowOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("借阅图书!");
    }
}


public class DeleteOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("删除图书!");
    }
}


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

这样我们就可灵活的运用接口以及多态。

3.Book包

book包内为我们每个具体书籍和书架的信息。

3.1Book类

 每个图书都有它的姓名,作者,价格,种类,以及是否借出的状态,首先我们先创建这些变量,然后写他的构造方法,要注意这里的状态是未借出,我们直接定义不初始化刚好为false,满足我们的要求,因为这里要体现封装,所以变量一律用private修饰,在提供相应的get以及set方法,方便以后打印,我们这里在生成一个toString方法。

public class Book {
    private String name;
    private String author;
    private int price;
    private String type;//书的类型
    private boolean isBorrowed;//默认是false

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


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

    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;
    }
}

3.2BookList类

booklist类我们用作书架,我们首先定义一个Book类型的数组,用来存放书籍,以及用来描述书籍个数的usedSize变量

public Book[] bookList=new Book[20];
    private int usedSize;
再为他提供构造方法,这里我们初始化了四本书分别为四大名著,再为他的变量提供相应的get,以及set方法,这里要注意,提供BookList的get和set方法时由于BookList类型本质为Book类型的数组,所以这里的返回值为Book类型,参数为数组下标。
public class BookList {
    public Book[] bookList=new Book[20];
    private int usedSize;

    public BookList() {
        this.bookList[0] = new Book("三国演义","罗贯中",18,"小说");
        this.bookList[1] = new Book("西游记","吴承恩",38,"小说");
        this.bookList[2] = new Book("水浒传","施耐庵",28,"小说");
        this.bookList[3] = new Book("红楼梦","曹雪芹",48,"小说");
        this.usedSize = 4;
    }
//  获取pos位置的图书
    public Book getBookList(int pos) {
        return bookList[pos];
    }
    //  修改pos位置的图书
    public void setBookList(Book book,int pos) {
        this.bookList[pos] = book;
    }

    public int getUsedSize() {
        return usedSize;
    }

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


}

剩余其他的包和类我会在后续博客继续阐述。

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

阿拉蕾wjh

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

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

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

打赏作者

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

抵扣说明:

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

余额充值