图书管理系统-Java

目录

一、图书管理系统样式

二、图书管理系统具体实现

   2.1 book包

  2.2 user包

  2.3 Main类

  2.4 operation包


一、图书管理系统样式

管理系统效果图:

         通过效果图我们可以看到,首先要实现登录,得到用户姓名和身份,依据用户的身份不同,其菜单栏和用户可以执行的操作不同

 二、图书管理系统具体实现

        主要设置以下包和类:

                                        

        其中book包存放有关书的信息,operation包存放有关用户执行的各种操作 ,user存放有关用户的信息,Main是测试类。

   2.1 book包

                        

        Book类用来存放有关一本书的详细信息和操作方法,BookList是书架类,记录存放的书。

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;
    }
    //打印书的信息
    @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 isIsborrowed() {
        return isborrowed;
    }
    //设置书的状态
    public void setIsborrowed(boolean isborrowed) {
        this.isborrowed = isborrowed;
    }
}
public class BookList {
    //书架存放的书(许多本)
    private Book[] books;
    //当前书架上书的数量
    int NowSize;
    //初始化书架
    public BookList() {
         this.books=new Book[20];
         //最初存放三本书
         books[0]=new Book("三国演义","罗贯中",13,"小说");
         books[1]=new Book("西游记","吴承恩",15,"小说");
         books[2]=new Book("红楼梦","曹雪芹",12,"小说");
         NowSize=3;
    }
    //知书的位置(数组下标)从书架获取一本书
    public Book getBook(int pos){
        return books[pos];
    }
    //在书架相应位置放置一本书
    public void SetBook(int pos,Book book){
        books[pos]=book;
    }
    //获取当前书架上书的数量
    public int getNowSize() {
        return NowSize;
    }
    //设置当前书架上书的数量
    public void setNowSize(int nowSize) {
        NowSize = nowSize;
    }
}

    2.2 user包

                        

         User类是所有类型用户的父类,AdminUser是管理员类型的用户,NormalUser是普通用户类型的用户。

//抽象类
public abstract class User {
    //用户名字
    protected String name;
    //用户的多种操作
    protected Ioperation[] ioperations;


    //用户初始化
    public User(String name) {
        this.name = name;
    }
    //用户的菜单
    public  abstract int menu();
    //用户要执行的操作
    public void doperation(int choice, BookList bookList){
        ioperations[choice].work(bookList);
    }
}
//管理员用户
public class AdminUser extends User{
    public AdminUser(String name) {
        super(name);
        //用户的具体操作
        this.ioperations=new Ioperation[]{
                new ExitOperation(),    //退出系统
                new SearchOperation(),  //查找图书
                new AddOperation(),     //增加图书
                new DeleteOperation(),  //删除图书
                new ShowOperation(),    //查看所有图书
        };
    }
    //管理员用户菜单
    @Override
    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;
    }
}
//普通用户
public class NormalUser extends User{
    public NormalUser(String name) {
        super(name);
        //普通用户具体操作
        this.ioperations=new Ioperation[]{
                new ExitOperation(),    //退出系统
                new SearchOperation(),  //查找图书
                new BorrowOperation(),  //借用图书
                new ReturnOperation(),  //归还图书
        };
    }
    //普通用户菜单
    @Override
    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;
    }
}

 2.3 Main类

//测试类
public class Main {
    //用户登录
    public static User logn(){
        System.out.println("请输入你的名字:");
        Scanner scanner=new Scanner(System.in);
        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=logn();
        while (true){
            int choice=user.menu();
            System.out.println("choice :"+choice);
            //根据choice确定执行哪个对象的特定操作
            user.doperation(choice,bookList);
        }
    }
}

 2.4 operation包

                

         AddOperation增加图书操作,BorrowOperation借出图书操作,DeleteOperation删除图书操作,ExitOperation退出系统操作,ReturnOperation归还操作,SearchOperation查找操作,ShowOperation查看所有图书操作,所有的操作针对书架进行,因此建Ioperation一个接口,声明操作书架的方法,每个类实现这个接口。

//接口
public interface Ioperation {
    void work(BookList bookList);
}
//增加图书
public class AddOperation implements Ioperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("增加图书");
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入name:");
        String name=scanner.nextLine();
        System.out.println("请输入author:");
        String author=scanner.nextLine();
        System.out.println("请输入price:");
        int price=scanner.nextInt();
        scanner.nextLine();
        System.out.println("请输入type:");
        String type=scanner.nextLine();
        Book book=new Book(name,author,price,type);
        //判断书架是否已满
        if(bookList.getNowSize()==Max_book_count){
            System.out.println("书架满了");
            return;
        }else {
            for (int i = 0; i <bookList.getNowSize(); i++) {
                //判断书架是否已有此书
                if((bookList.getBook(i).getName()).equals(name)){
                    System.out.println("书架里已有此书,不需再添加");
                    return;
                }
            }
            //放置书
            bookList.SetBook(bookList.getNowSize(), book);
            //书的数量加1
            int count= bookList.getNowSize();
            bookList.setNowSize(++count);
            System.out.println("添加图书成功");
        }
    }
}
//借出图书
public class BorrowOperation implements Ioperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("借出图书");
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入name:");
        String name=scanner.nextLine();
        for (int i = 0; i < bookList.getNowSize(); i++) {
            //判断存在此书
            if ((bookList.getBook(i).getName()).equals(name)){
                bookList.getBook(i).setIsborrowed(true);
                System.out.println("借阅图书成功");
                return;
            }
        }
        System.out.println("借阅的书不存在");
    }
}
//删除图书
public class DeleteOperation implements Ioperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        System.out.println("请输入要删除的书名:");
        Scanner scanner=new Scanner(System.in);
        String name=scanner.nextLine();
        for (int i = 0; i < bookList.getNowSize(); i++) {
            //存在此书
            if ((bookList.getBook(i).getName()).equals(name)){
                for (int j = i; j < bookList.getNowSize()-1; j++) {
                    bookList.SetBook(j,bookList.getBook(j+1));
                }
                //最后一本书置空
                bookList.SetBook(bookList.getNowSize(),null);
                //书的数量减1
                int count=bookList.getNowSize();
                bookList.setNowSize(--count);
                System.out.println("删除图书成功");
                return;
            }
        }
        System.out.println("此书不存在");
    }
}
//归还图书
public class ReturnOperation implements Ioperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("归还图书");
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入name:");
        String name=scanner.nextLine();
        for (int i = 0; i < bookList.getNowSize(); i++) {
            //存在图书
            if ((bookList.getBook(i).getName()).equals(name)){
                bookList.getBook(i).setIsborrowed(false);
                System.out.println("归还图书成功");
                return;
            }
        }
        System.out.println("归还的图书不存在");
    }
}
//查找图书
public class SearchOperation implements Ioperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("查找图书");
        Scanner scanner=new Scanner(System.in);
        String name=scanner.nextLine();
        for (int i = 0; i <bookList.getNowSize();i++) {
            //图书存在
            if ((bookList.getBook(i).getName()).equals(name)){
                System.out.println(bookList.getBook(i));
                return;
            }
        }
        System.out.println("书库里没有此书");
    }
}
//查找所有图书
public class ShowOperation implements Ioperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("打印图书");
        for (int i = 0; i < bookList.getNowSize(); i++) {
            Book book=bookList.getBook(i);
            System.out.println(book);
        }
    }
}
//退出系统
public class ExitOperation implements Ioperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统");
        System.exit(0);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_64668629/article/details/132257099