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);
    }
}
相关推荐
wjs2024几秒前
Swift 数组
开发语言
xiaoyalian24 分钟前
R语言绘图过程中遇到图例的图块中出现字符“a“的解决方法
笔记·r语言·数据可视化
吾日三省吾码1 小时前
JVM 性能调优
java
stm 学习ing1 小时前
FPGA 第十讲 避免latch的产生
c语言·开发语言·单片机·嵌入式硬件·fpga开发·fpga
湫ccc2 小时前
《Python基础》之字符串格式化输出
开发语言·python
弗拉唐2 小时前
springBoot,mp,ssm整合案例
java·spring boot·mybatis
Red Red2 小时前
网安基础知识|IDS入侵检测系统|IPS入侵防御系统|堡垒机|VPN|EDR|CC防御|云安全-VDC/VPC|安全服务
网络·笔记·学习·安全·web安全
oi772 小时前
使用itextpdf进行pdf模版填充中文文本时部分字不显示问题
java·服务器
mqiqe3 小时前
Python MySQL通过Binlog 获取变更记录 恢复数据
开发语言·python·mysql