JAVA-实现简易图书管理系统

一.设计思路

  • 首先我们要区分管理员用户和普通用户,两者的所展示的界面不同,可以利用继承和多态来实现
  • 我们要把每本书的属性进行封装,同时为了管理所有书籍,应该设计一个书架,即存放书类的数组
  • 在图书界面的各种操作都需要通过改变书架来完成,将各个操作封装起来,设置成各自的类,而这些操作都要遵循一定的规范,而接口就是定义了一种规范,可以通过接口来实现
  • 管理员用户特有的操作有删除图书,新增图书
  • 普通用户特有的的操作有借阅图书,归还图书

二.模块代码的细节思考的演示

分别将书本类和书架类放到book包去,将管理员用户与普通用户放到user包去,将对书本各种操作放到opera包去。

1.书本类的实现(Book类)

  • 书本类的属性有书名、作者、价格、类型还有是否被借出,因为要将书本类封装,所以书本类中的属性要被要设置为private权限
  • 并提供相应的get和set方法来帮助我们获得和设置书本类的属性
  • 同时重写构造方法来初始化书本类的属性
  • 还要重写toString()方法
    代码实现如下:
public class Book {
    private String name;//书名
    private String author;//作者
    private double price;//价格
    private String type;//类型
    private boolean isBorrowed;//是否被借出

	//重写构造方法
    public Book(String name, String author, double price, String type) {
    //参数列表中没有isBorrowed是因为布尔类型默认是false,
    //对于图书来说,放进书架的书刚开始就是未被借出的
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
    }
	
	//书本类属性的get和set方法
    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;
    }

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

2.书架类的实现(BookList类)

书架类提供存放书本类数组的地方,方便后续在书架类中对书本的操作
代码实现如下:

public class BookList {
    private  static final int DEFAULT_SIZE=12;
    private Book[] books = new Book[DEFAULT_SIZE];
    private int usedSize;//记录当前books数组当中有多少多少本书

    public BookList() {
        books[0]=new Book("三国演义","罗贯中",89,"小说");
        books[1]=new Book("西游记","吴承恩",78,"小说");
        books[2]=new Book("红楼梦","曹雪芹",49,"小说");
        this.usedSize=3;
    }


    public Book getBooks(int pos) {
        return this.books[pos];
    }

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

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

    public int getUsedSize() {
        return usedSize;
    }

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

3.用户类(User类)

管理员用户和普通用户之间存在着共性,都是用户的分支,所以这两者都是用户的子类
我们首先要做的就是实现User这个父类

管理员用户和普通用户都要用到菜单界面(menu( )),管理员用户类和普通用户类继承用户类的menu( )方法再重写就行,用户类的方法不用实现,用abstract修饰,类中有抽象方法,则该类为抽象类,所以用户类也要用abstract修饰

public abstract class User {
    protected String name;

    protected IOperation[] iOperations;//IOperation为接下来要讲的的操作类

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

    public abstract int menu();

    public void doWork(int choice, BookList bookList){
        this.iOperations[choice].work(bookList);
    }
}

4.操作类的接口

我们对书架类执行的操作有增删查改,这些操作都遵循一定的规范,这些规范由接口来实现
在增删查改的操作中,接口提供自己的方法给实现接口的类,实现接口的类对方法进行重写

public interface IOperation {
	//这些操作都是对书架类的对象进行修改
    //所以work()的参数为书架类的对象
    void work(BookList bookList);
    
}

5.操作类

5.1显示操作

因为重写过book的toString()方法了,所以可以直接遍历进行输出即可,

public class ShowOpeartion implements  IOperation{
    @Override
    public void work(BookList bookList) {
        int currentSize=bookList.getUsedSize();//通过bookList.getUsedSize()来得到书架中有多少本书
        for (int i = 0; i <currentSize ; i++) {
            Book book=bookList.getBooks(i);//遍历书架上的书
            System.out.println(book);
            
        }
    }
}

5.2退出操作

System.exit(0)代表程序运行结束

public class Exitoperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统");
        System.exit(0);
        System.out.println("退出成功");
    }
}

5.3查找操作

查找图书要遍历书架

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.getUsedSize();
        for (int i = 0; i <currentSize ; i++) {
            Book book=bookList.getBooks(i);
            if(book.getName().equals(name)){
                System.out.println("找到了这本书");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有这本书");

    }
}

5.4增加操作

增加前要查看这本书是否已经在书架里了

public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书");
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入书名");
        String name=scanner.nextLine();
        System.out.println("请输入作者名");
        String author=scanner.nextLine();
        System.out.println("请输入价格");
        double price=scanner.nextDouble();
        scanner.nextLine();//这一步为了拿走上一个输入完价格后输入的回车字符
        System.out.println("请输入类型");
        String type =scanner.nextLine();
        

        Book book=new Book(name,author,price,type);
        int currentSize=bookList.getUsedSize();
        //遍历书架,看要加入的书是否已经存在
        for (int i = 0; i <currentSize ; i++) {
            Book tmp=bookList.getBooks(i);
            if(tmp.getName().equals(name)){
                System.out.println("这本书已存在书架中");
                return;
            }
        }
        bookList.setBooks(book);
        //加入一本书后,usedSzie+1
        bookList.setUsedSize(currentSize+1);
    }

5.5删除操作

public class DelOperation 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();
        int index=-1;
		//寻找要删除书本的下标
        for (int i = 0; i <currentSize ; i++) {
            Book tmp=bookList.getBooks(i);
            if(tmp.getName().equals(name)){
                index=i;
                break;
            }
        }

        //挪动数据,向前覆盖要删除的书
        for (int i = index; i < currentSize-1; i++) {

            Book book=bookList.getBooks(i+1);
            bookList.setBooks(i,book);

        }
        //因为删除的是对象,所以把最后一个置为null
        bookList.setBooks(currentSize,null);

        //修改size
        bookList.setUsedSize(currentSize-1);
        System.out.println("删除成功");

    }
}

5.6借阅操作

先要判断想借的书是否已被借出,若没被借出,则将该书的isBorrowed属性改为true

public class BorrowOperation 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.getName().equals(name)&&!book.isBorrowed()){
                book.setBorrowed(true);
                System.out.println("借阅成功");
                return;
            }
        }


    }
}

5.7归还操作

归还与借阅操作差不多,就不赘述了

public class ReturnOperation 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.getName().equals(name)&&book.isBorrowed()){
                book.setBorrowed(false);
                System.out.println("归还成功");
                return;
            }
        }
    }
}

6.管理员用户类(AdminUser类)

管理员用户类继承用户类并重写构造方法和menu( )方法
iOperations的初始化,可以利用接口的多态性来进行实现

//Uesr类
public abstract class User {
    protected String name;

    protected IOperation[] iOperations;//IOperation[]是操作数组类型

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

    public abstract int menu();
	
	//通过menu()方法返回的数字就是choice
    public void doWork(int choice, BookList bookList){
        this.iOperations[choice].work(bookList);
    }
}
public class AdminUser extends User{
    public AdminUser(String name) {
        super(name);
        //因为IOperation是接口,增删查改等操作的类对接口进行实现,所以可以通过接口数组来实现多态
        this.iOperations=new IOperation[]{
                new Exitoperation(),
                new FindOperation(),
                new AddOperation(),
                new DelOperation(),
                new ShowOpeartion()
        };

    }
	
    public int menu(){
        System.out.println("==============================");
        System.out.println("hello"+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 scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

7.普通用户类(NormalUser类)

与管理员用户类类似

public class NormalUser extends  User{
    public NormalUser(String name) {
        super(name);
        this.iOperations=new IOperation[]{
                new Exitoperation(),
                new FindOperation(),
                new BorrowOperation(),
                new ReturnOperation(),
        };

    }

    public  int menu(){
        System.out.println("==============================");
        System.out.println("hello"+name+"欢迎来到图书小练习");
        System.out.println("1.查找图书");
        System.out.println("2.借阅图书");
        System.out.println("3.归还图书");
        System.out.println("0.退出系统");
        System.out.println("==============================");
        System.out.println("请输入你的操作");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

Main类

通过身份输入来实例化login( )方法的返回对象的引用是AdminUser类型还是NormalUser类型

public class Main {
    public static User login(){
        System.out.println("请输入你的名字:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("输入你的身份: 1-》管理员 0-》普通用户");
        int choice = scanner.nextInt();
        if(choice==1){
            return new AdminUser(name);
        }else{
            return new NormalUser(name);
        }
    }

    public static void main(String[] args) {
        BookList bookList = new BookList();
        User user=login();
        while(true) {
            int choice = user.menu();
            //根据choice 和 user来确定 我到底调用哪个对象的哪个操作
            user.doWork(choice, bookList);
        }
    }

以上就是本篇文章的内容了,很感谢你能看到这里
如果觉得内容对你有帮助的话,不妨点个关注
我会继续更新更高质量的内容,我们一同学习,一同进步!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值