java实现基本图书管理系统

图书管理系统概述

本系统主要由四部分组成,分别为书与书架、使用者、功能实现、测试功能。该系统可以根据使用者的不同提供相应的权限,针对管理员该系统提供退出系统、查找图书、新增图书、删除图书、显示图书的权限,针对普通用户该系统提供退出系统、查找图书、借阅图书、归还图书的权限。

图书管理系统的具体实现

图书部分我们根据书的特征定义不同的成员变量,代码如下。在定义成员变量时我们使用private来修饰因为这不仅能体现封装的思想,同时也能达到保证数据合法安全的目的。

package book;

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

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

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

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

当我们完成图书部分的基本操作后我们可以根据图书的基本特征来将其存放在书架上,在这里我们使用数组去存放我们的图书,并且对我们想要存放的图书进行赋值,这里的Book[10]中的10代表书架的容量,而usedSize代表已经存放的图书数量。

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

在完成对书与书架的基本操作后接下来我们要对用户进行定义,用户分为两种分别为图书管理员,普通用户。这两种用户有着共同的特性所以我们可以采用继承的方式抽取两者的共性

public abstract class User {
    protected String name;
   
    //为了在子类中初始化父类的成员
    public User(String name){
        this.name=name;
    }
}

接下来我们在两个用户中分别写他们对应的权限

普通用户:

 public int menu(){
        System.out.println("欢迎"+name+"来到图书系统");
        System.out.println("********普通用户菜单*******");
        System.out.println("0.退出系统");
        System.out.println("1.查找图书");
        System.out.println("2.借阅图书");
        System.out.println("3.归还图书");
        System.out.println("***********************");
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入你的选择");
        int choice= scanner.nextInt();
        return choice;
    }

管理员:

public int menu(){
        System.out.println("欢迎"+name+"来到图书系统");
        System.out.println("********管理员菜单*******");
        System.out.println("0.退出系统");
        System.out.println("1.查找图书");
        System.out.println("2.新增图书");
        System.out.println("3.删除图书");
        System.out.println("4.显示图书");
        System.out.println("***********************");
        System.out.println("请输入你的选择");
        Scanner scanner=new Scanner(System.in);
        int choice= scanner.nextInt();
        return choice;
    }

在完成这些后我们需要考虑如何精确地为使用者提供相应的权限

public class Main {
    public static User lojin(){
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入你的姓名:");
        String name=scanner.nextLine();
        System.out.println("请输入你的身份:1.管理员  2.普通用户");
        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=lojin();
        while(true){
            int choice=user.menu();
            //根据这个返回值看调用哪个对象的哪个方法
            user.doIoperation(choice,bookList);
        }
    }
}

在测试使用的这一部分我们完成登录方面的相关代码书写,这里我们运用了多态的思想,通过传入不同的对象来打印我们想要得到的权限菜单。同时我们要注意在调用menu()时因为父类无法直接访问子类的方法,我们需要在管理员和普通用户的父类(User)中提前定义好抽象方法(这里我们用到了向上转型和动态绑定)。

public abstract class User {
    protected String name;
   
    //为了在子类中初始化父类的成员
    public User(String name){
        this.name=name;
    }
​    public abstract int menu();
}

在完成登录界面的书写后,接下来我们需要考虑该系统的功能实现,在这里我们可以定义一个接口以便于权限的实现与调用

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

在完成接口的书写后我们需要进行书写相应的实现权限的代码

退出系统

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

展示图书

public class ShowOperation implements IOPeration{
    public void work(BookList bookList){
        System.out.println("显示图书...");
        int currentSize= bookList.getUsedSize();
        for (int i = 0; i <currentSize; i++) {
            System.out.println(bookList.getBooks(i));
        }
    }
}

查找图书

public class FindOperation implements IOPeration{
    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++) {
           if(name.equals(bookList.getBooks(i).getName())){
               System.out.println("找到了");
               System.out.println(bookList.getBooks(i));
               return;
           }
        }
        System.out.println("没找到你想要的图书");
    }
}

新增图书

public class AddOperation implements IOPeration{
    public void work(BookList bookList){
        int currentSize= bookList.getUsedSize();
        if(currentSize==bookList.getBooks().length){
            System.out.println("书架已满,无法完成插入操作");
            return;
        }
        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("输入这本书的类型:");
        String type= scanner.nextLine();
        System.out.println("输入本书的价格: ");
        int price=scanner.nextInt();
        Book newbook=new Book(name,author,price,type);
        bookList.setBooks(bookList.getUsedSize(), newbook);
        bookList.setUsedSize(bookList.getUsedSize()+1);
        System.out.println("插入成功");
    }
}

删除图书

public class DelOperation implements IOPeration{
    public void work(BookList bookList){
        System.out.println("删除图书...");
        System.out.println("输入你想删除图书:");
        Scanner scanner=new Scanner(System.in);
        String name= scanner.nextLine();
        int pos=-1;
        int currentSize= bookList.getUsedSize();
        int i = 0;
        for (; i <currentSize; i++) {
            if(name.equals(bookList.getBooks(i).getName())){
                pos=i;
                break;
            }
        }
        if(i==currentSize) {
            System.out.println("没找到你想要删除的图书");
        }
        for (int j = pos; j <currentSize-1; j++) {
            bookList.setBooks(pos,bookList.getBooks(j+1));
        }
        bookList.setUsedSize(currentSize-1);
        System.out.println("删除成功");
        }
}

借阅图书

public class BorrowOperation implements IOPeration{
    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++) {
            if(name.equals(bookList.getBooks(i).getName())){
               if(bookList.getBooks(i).isBorrowed()==false){
                   System.out.println("借阅成功");
                   bookList.getBooks(i).setBorrowed(true);
                   return;
               }
            }
        }
        System.out.println("此书已被借走或此书不存在");
    }
}

归还图书

public class ReturnOperation implements IOPeration{
    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++) {
            if(name.equals(bookList.getBooks(i).getName())){
                if(bookList.getBooks(i).isBorrowed()==true){
                    System.out.println("归还成功");
                    bookList.getBooks(i).setBorrowed(false);
                    return;
                }
            }
        }
        System.out.println("此书已被归还");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值