运用JavaSE知识实现图书管理系统

目录

这篇图书管理系统是对JavaSE知识总结复习的一个小作业,检测自己对知识的掌握程度。

一.Main函数

java 复制代码
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.普通用户");
        System.out.println("输入你的身份");
        int choice = scanner.nextInt();
        if (choice == 1){
            return new Admin(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();
            user.doOperation(choice,bookList);
        }
    }
}

二.用户类

java 复制代码
public abstract class User {
    private String name;
    protected IOperation[] iOperations;
    public User(String name){

        this.name = name;
        IOperation[] iOperations;
    }
    public abstract int menu();
    public void doOperation(int choice,BookList bookList){
        IOperation iOperation = iOperations[choice];
        iOperation.work(bookList);
    }
}

三.普通用户类

java 复制代码
public class NormalUser extends User{
    public NormalUser(String name) {
        super(name);
        this.iOperations =new IOperation[]{
                new ExitOperation(),
                new FindOperation(),
                new BorrowOperation(),
                new ReturnOperation()
        };
    }

    @Override
    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 scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

四.管理员类

java 复制代码
public class Admin extends User{
    public Admin(String name) {
        super(name);
        this.iOperations =new IOperation[] {
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DeleOperation(),
                new ShouwOperation()
        };
    }

    @Override
    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 scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

五.图书类

java 复制代码
public class Book{
    //书名 作者 价格 类型 是否借出
    private String name;
    private String author;
    private int price;
    private String type;
    private Boolean isBorrowed = false;

    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 getBorrowed() {
        return isBorrowed;
    }

    public void setBorrowed(Boolean borrowed) {
        isBorrowed = borrowed;
    }

    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=" + isBorrowed +
                '}';
    }
}

六.书架类

java 复制代码
public class BookList {
    public Book[] books;
    public int usedSize;
    public BookList(){
        this.books = new Book[10];
        books[0] = new Book("三国演义","罗贯中",46,"小说");
        books[1] = new Book("水浒传","施耐庵",37,"小说");
        books[2] = new Book("红楼梦","曹雪芹",76,"小说");
        books[3] = new Book("西游记","吴承恩",88,"小说");
        usedSize = 4;
    }

    public Book getBook(int pos) {
        return books[pos];
    }

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

    public int getUsedSize() {
        return usedSize;
    }

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

七.操作类

1.操作接口

java 复制代码
public interface IOperation {
    void work(BookList bookList);
    Scanner scanner = new Scanner(System.in);
}

2.增加操作

java 复制代码
public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("请输入你要增加图书的书名");
        String name = scanner.nextLine();
        System.out.println("请输入你要添加书的作者");
        String author = scanner.nextLine();
        System.out.println("请输入你要添加书的价格");
        int price = scanner.nextInt();
        System.out.println("请输入你要添加书的类型");
        scanner.nextLine();
        String type = scanner.nextLine();
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            if(bookList.getBook(i).getName().equals(name)){
                System.out.println("该书已经存在,无法添加!");
                return;
            }
        }
        //书不存在 进行添加操作
        Book book = new Book(name,author,price,type);
        bookList.setBook(book,currentSize);
        bookList.setUsedSize(currentSize+1);
    }
}

3.删除操作

java 复制代码
public class DeleOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("请输入你要删除书的书名:");
        String name = scanner.nextLine();
        int index = -1;
        int i =0;
        int currentSize = bookList.getUsedSize();
        for ( i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)){
                index = i;
                break;
            }
        }
       if (i >= currentSize){
           System.out.println("要删除的书不存在!");
           return;
       }
        for (int j = index; j < currentSize-1; j++) {
            Book book = bookList.getBook(j+1);
            bookList.setBook(book,j);
        }
        bookList.setBook(null,currentSize);
        bookList.setUsedSize(currentSize-1);
        System.out.println("删除成功!!!");
    }
}

4.查找操作

java 复制代码
public class FindOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        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)){
                System.out.println(book);
                return;
            }
        }
        System.out.println("图书馆没有你要寻找的书!");
    }
}

5.展示操作

java 复制代码
public class ShouwOperation 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);
        }
    }
}

6.借阅操作

java 复制代码
public class BorrowOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("图书馆的书如下:");
        int currentSzie = bookList.getUsedSize();
        for (int i = 0; i < currentSzie; i++) {
            Book book = bookList.getBook(i);
            System.out.println(book.getName());
        }
        System.out.println("请输入你要外借的图书的书名:");
        String name = scanner.nextLine();
        for (int i = 0; i < currentSzie; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                if (book.getBorrowed()){
                    System.out.println("该书已被借出,换本书吧!");
                    return;
                }else {
                    book.setBorrowed(true);
                    System.out.println("借阅成功");
                    System.out.println("该书的信息如下");
                    System.out.println(book);
                    return;
                }

            }
        }
            System.out.println("你想借阅的图书不存在!");
    }
}

7.归还操作

java 复制代码
public class ReturnOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("归还图书");
        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("归还成功!");
                return;
            }
        }
        System.out.println("不存在你要归还的图书!");
    }
}

8.退出系统

java 复制代码
public class ExitOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统!");
        System.exit(0);
    }
}

总结

以上就是图书管理系统的所有知识,有些地方还是需要思考去巧妙地设计使得文章更加简洁明了。冰冻三尺,非一日之寒。水滴石穿,非一日之功。坚持把每一个知识点搞清楚,并进行总结,形成属于自己的知识框架,会让你的学习更加轻松。

相关推荐
九圣残炎11 分钟前
【从零开始的LeetCode-算法】1456. 定长子串中元音的最大数目
java·算法·leetcode
wclass-zhengge13 分钟前
Netty篇(入门编程)
java·linux·服务器
童先生15 分钟前
Go 项目中实现类似 Java Shiro 的权限控制中间件?
开发语言·go
lulu_gh_yu16 分钟前
数据结构之排序补充
c语言·开发语言·数据结构·c++·学习·算法·排序算法
Re.不晚40 分钟前
Java入门15——抽象类
java·开发语言·学习·算法·intellij-idea
老秦包你会42 分钟前
Qt第三课 ----------容器类控件
开发语言·qt
凤枭香1 小时前
Python OpenCV 傅里叶变换
开发语言·图像处理·python·opencv
雷神乐乐1 小时前
Maven学习——创建Maven的Java和Web工程,并运行在Tomcat上
java·maven
ULTRA??1 小时前
C加加中的结构化绑定(解包,折叠展开)
开发语言·c++
码农派大星。1 小时前
Spring Boot 配置文件
java·spring boot·后端