JavaSE图书管理系统

JavaSE图书管理系统

思路

实际上大概分为三步 ,关于书相关的放在book 包里,关于用户相关的放在User 包里,关于操作相关的我们放在Operation 包里,不同的包用来管理不同的代码,那么为什么要把操作放在一个包里呢?原因是我们分为普通用户管理员 用户,那么普通用户和管理员用户的操作是不一样的,我们放在一个类里面,不太好区分。

关于book包,operation包,user包里面的类和接口。

关于book包里,定义两个类,bookList为书架的类,book关于书的类。

关于operation包里的类都是对bookList的操作,如查找,删除等。

关于User的类,大致为管理员类和普通用户类,和普通的User类。

一.Main方法

java 复制代码
import book.BookList;
import user.AdminUser;
import user.NormaUser;
import user.User;

import java.util.Scanner;

public class Main {
    public static User login(){
        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 NormaUser(name);
        }

    }
    public static void main(String[] args) {

        BookList booklist = new BookList();//书架创建
        //user 指向哪个对象 就看返回值是哪个
        User user = login();
        //登录

        while(true) {//重复执行操作
            int choice = user.menu();
            //看user指向哪个对象,就调用哪个菜单

            System.out.println("choice: " + choice);

            //根据choice 的选择,来决定调用的是哪个方法?

            user.doOperation(choice, booklist);
        }

    }
}

二.User包

1.User类

java 复制代码
package user;

import book.BookList;
import operation.IOPeration;

public abstract class User {
    protected String name;
    protected IOPeration[] ioPerations;

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

    public abstract int menu();

    public void doOperation(int choice, BookList bookList){
        ioPerations[choice].work(bookList);//拿到对象,调用对象的方法
    }
}

2.NormaUser类

java 复制代码
package user;

import operation.*;

import java.util.Scanner;

public class NormaUser extends User {//普通用户
    public NormaUser(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("1. 查找图书");
        System.out.println("2. 借阅图书");
        System.out.println("3. 归还图书");
        System.out.println("0. 退出系统");
        System.out.println("****************************");

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的操作: ");
        int choice = sc.nextInt();
        return choice;
    }
}

3.AdminUser类

java 复制代码
package user;

import operation.*;

import java.util.Scanner;

public class AdminUser extends User {//管理员用户

    public AdminUser(String name) {
        super(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("****************************");

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的操作: ");
        int choice = sc.nextInt();
        return choice;
    }
}

三.book包

1.BookList类

java 复制代码
package book;

public class BookList {
    private Book[] books;
    private  int usedSize;//记录当前书架上 实际存放的书的数量
    public BookList(){
        this.books = new Book[10];
        this.books[0] = new Book("三国演义","罗贯中",10,"小说");
        this.books[1] = new Book("西游记","吴承恩",9,"小说");
        this.books[2] = new Book("红楼梦","曹雪芹",19,"小说");

        this.usedSize = 3;

    }

    public int getUsedSize() {
        return usedSize;
    }

    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }
    public Book  getBook(int pos){
        return books[pos];
    }
    public void setBooks(int pos,Book book){
        books[pos] = book;
    }

    public Book[] getBooks(){
        return books;
    }

}

2.Book类

java 复制代码
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) ? ",已经借出" : ",未被借出")+
                '}';
    }
}

四.operation包

因为所以操作都是对于书架的,所以我们设置一个接口

1.IOPeration接口

java 复制代码
package operation;

import book.BookList;

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

2.AddOperation类

新增图书

java 复制代码
package operation;

import book.Book;
import book.BookList;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;

import java.util.Scanner;

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("请输入类型: ");
        String type = scanner.nextLine();

        System.out.println("请输入价格: ");
        int price = scanner.nextInt();

        Book book = new Book(name,author,price,type);

        //检查数组当中有没有这本书
        int currentSize = bookList.getUsedSize();
        for(int i = 0;i<currentSize;i++){
            Book book1 = bookList.getBook(i);
            if(book1.getName().equals(name)){
                System.out.println("有这本书,不进行存放了!");
                return;
            }
        }
        //判断数组是否满了
        if(currentSize==bookList.getBooks().length){
            System.out.println("书架满了!");
        }else{
            bookList.setBooks(currentSize,book);
            bookList.setUsedSize(currentSize+1);
        }
    }
}

3.BorrowOperation类

借阅图书

java 复制代码
package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

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.getBook(i);
            if(book.getName().equals(name)){
                book.setBorrowed(true);
                System.out.println("借阅成功!");
                System.out.println(book);
                return;
            }
        }
        System.out.println("你借阅的图书不存在!!!");
    }
}

4.DelOperation类

删除图书

java 复制代码
package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

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 pos = -1;
        int currenSize = bookList.getUsedSize();
        int i = 0;
        for(;i<currenSize;i++){
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                pos = i;
                break;
            }
        }
        if(i==currenSize){
            System.out.println("没有你要删除的图书!");
        return;
        }
        //开始删除
        int j = pos;
        for(;j<currenSize-1;j++){
            Book book = bookList.getBook(j+1);
            bookList.setBooks(j,book);
            
        }
        bookList.setBooks(j,null);
        bookList.setUsedSize(currenSize-1);
    }
    
}

5.FindOperation类

查找图书

java 复制代码
package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class FindOperation implements IOPeration {
    @Override
    public void work(BookList bookList) {
        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.getBook(i);
            if(book.getName().equals(name)){
                System.out.println("找到了这本书,信息如下:");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有找到这本书!");
    }
}

6.ReturnOperation类

归还图书

java 复制代码
package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

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.getBook(i);
            if(book.getName().equals(name)){
                book.setBorrowed(false);
                System.out.println("归还成功!");
                System.out.println(book);
                return;
            }
        }
        System.out.println("你归还的图书不存在!!!");
    }
}

7.ShowOperation类

打印图书

java 复制代码
package operation;

import book.Book;
import book.BookList;

public class ShowOperation implements IOPeration {
    @Override
    public void work(BookList bookList) {
        System.out.println("打印图书!");
        int currentSize = bookList.getUsedSize();//现在有多少本书
        for(int i = 0;i<currentSize;i++){
            Book book = bookList.getBook(i);
            System.out.println(book);
        }
    }
}

8.ExitOperation类

退出系统

java 复制代码
package operation;

import book.BookList;

public class ExitOperation implements IOPeration {
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统!");
        System.exit(0);
    }
}
相关推荐
Crossoads28 分钟前
【汇编语言】端口 —— 「从端口到时间:一文了解CMOS RAM与汇编指令的交汇」
android·java·汇编·深度学习·网络协议·机器学习·汇编语言
老马啸西风29 分钟前
NLP 中文拼写检测纠正论文-02-2019-SOTA FASPell Chinese Spell Checke github 源码介绍
java
向宇it31 分钟前
【从零开始入门unity游戏开发之——C#篇26】C#面向对象动态多态——接口(Interface)、接口里氏替换原则、密封方法(`sealed` )
java·开发语言·unity·c#·游戏引擎·里氏替换原则
@菜鸟进阶记@34 分钟前
java根据Word模板实现动态填充导出
java·开发语言
卖芒果的潇洒农民36 分钟前
Lecture 6 Isolation & System Call Entry
java·开发语言
SomeB1oody1 小时前
【Rust自学】6.1. 定义枚举
开发语言·后端·rust
SomeB1oody1 小时前
【Rust自学】5.3. struct的方法(Method)
开发语言·后端·rust
Amarantine、沐风倩✨2 小时前
设计一个监控摄像头物联网IOT(webRTC、音视频、文件存储)
java·物联网·音视频·webrtc·html5·视频编解码·七牛云存储
Kisorge2 小时前
【C语言】指针数组、数组指针、函数指针、指针函数、函数指针数组、回调函数
c语言·开发语言
机智的叉烧2 小时前
前沿重器[57] | sigir24:大模型推荐系统的文本ID对齐学习
人工智能·学习·机器学习