【JavaEE】图书管理系统-简易版

文章目录

  • book包
    • Book类
    • BookList类
  • operation包
    • AddOperation 类
    • BorrowOperation 类
    • DelOperation类
    • DisplayOperation 类
    • ExitOperation 类
    • FindOperation 类
    • IOperation 类
    • ReturnOperation 类
  • User包
    • AdminUser类
    • NormalUser 类
    • User 类
  • Main 类

我们将先前学习的知识运用起来,写一篇图书管理系统简易版 链接

book包

Book类

编写每一本书的基本属性和方法
package Book;

/**
 * Created with Intellij IDEA.
 * Description:每一本书的属性和方法
 * User: a
 * Date: 2022-04-15
 * Time: 14:56
 */
public class Book {
    /**
     * 属性:书名,价格,作者,类型,状态(是否被借出)
     */
    private String name;
    private String author;
    private int price;
    private String type;
    private boolean isBorrow;//成员变量未初始化默认未false,即未被借出

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

    public void setBorrow(boolean borrow) {
        isBorrow = borrow;
    }

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

BookList类

对整个“书架”操作
package Book;

/**
 * Created with Intellij IDEA.
 * Description:存储书及其相关信息
 * User: a
 * Date: 2022-04-15
 * Time: 14:56
 */
public class BookList {
    //初始值
    private static int SIZE = 5;

    //书架容量
    private Book[] books = new Book[SIZE];

    //已使用的空间
    private int usedSize = 0;

    //初始化书架
    public BookList() {
        books[0] = new Book("三国演义","罗贯中",90,"小说");
        books[1] = new Book("西游记","吴承恩",78,"小说");
        books[2] = new Book("红楼梦","曹雪芹",89,"小说");
        books[3] = new Book("水浒传", "施耐庵",91,"小说");
        books[4] = new Book("推背图","袁天罡",93,"谶颂述");
        this.usedSize = 5;
    }
    //get和set方法
    public int getUsedSize() {
        return usedSize;
    }

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

    //获取pos下标书
    public Book getPos(int pos) {
        return books[pos];
    }
    //修改pos位置的书
    public void setPos(int pos, Book book) {
        books[pos] = book;
    }
    //判断是否满
    public boolean isFull(int newSize) {
        return newSize > SIZE ? true : false;
    }
    //扩容
    public void addCapacity() {
        SIZE *= 2;
        Book[] newBooks = new Book[SIZE];
        System.arraycopy(books,0,newBooks,0,usedSize);
        this.books = newBooks;
        System.out.println("扩容成功!");
    }
}

operation包

AddOperation 类

package operation;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

/**
 * Created with Intellij IDEA.
 * Description:增加图书
 * User: a
 * Date: 2022-04-15
 * Time: 14:57
 */
public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书!");
        System.out.println("请输入图书的名字:");
        Scanner scanner = new Scanner(System.in);
        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();
        if (bookList.isFull(currentSize + 1)) {
            bookList.addCapacity();
        }
        bookList.setPos(currentSize,book);

        bookList.setUsedSize(currentSize + 1);

        System.out.println("新增成功!");
    }
}

BorrowOperation 类

package operation;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

/**
 * Created with Intellij IDEA.
 * Description:借出书
 * User: a
 * Date: 2022-04-15
 * Time: 14:57
 */
public class BorrowOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅图书!");

        System.out.println("请输入你要借阅图书的名字:");
        Scanner input = new Scanner(System.in);
        String name = input.nextLine();

        int currentSize = bookList.getUsedSize();
        int i = 0;
        for (; i < currentSize; i++) {
            Book book = bookList.getPos(i);
            //找到这本书
            if (book.getName().equals(name)) {
                //判断其是否被借出
                if(book.isBorrow() == true) {
                    System.out.println("该书已被借出!");
                    return;
                } else {
                    book.setBorrow(true);
                    System.out.println("借阅成功!");
                    return;
                }
            }
        }
        if (i == bookList.getUsedSize()) {
            System.out.println("没有这本书!");
        }

    }
}

DelOperation类

package operation;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

/**
 * Created with Intellij IDEA.
 * Description:删除图书
 * User: a
 * Date: 2022-04-15
 * Time: 14:57
 */
public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书!");

        System.out.println("请输入你要删除图书的名字:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        int i = 0;
        for (; i < bookList.getUsedSize(); i++) {
            Book book = bookList.getPos(i);
            if (book.getName().equals(name)) {
                break;
            }
        }
        //没有找到
        if (i == bookList.getUsedSize()) {
            System.out.println("删除图书不存在!");
            return;
        }
        //找到了,正常退出
        for (int j = i; j < bookList.getUsedSize() - 1; j++) {
            //这里的boolList内是对象,所以下面的写法是错误的
            //bookList[j] = bookList[j + 1];

            Book book = bookList.getPos(j + 1);
            bookList.setPos(j, book);
        }
        int currentSize = bookList.getUsedSize();
        bookList.setPos(currentSize - 1,null);
        bookList.setUsedSize(currentSize - 1);
        System.out.println("删除成功!");
    }
}

DisplayOperation 类

package operation;

import Book.Book;
import Book.BookList;

/**
 * Created with Intellij IDEA.
 * Description:展示图书
 * User: a
 * Date: 2022-04-15
 * Time: 14:58
 */
public class DisplayOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("显示图书!");

        for (int i = 0; i < bookList.getUsedSize(); i++) {
            Book book = bookList.getPos(i);
            System.out.println(book);
        }
    }
}

ExitOperation 类

package operation;

import Book.BookList;

/**
 * Created with Intellij IDEA.
 * Description:退出系统
 * User: a
 * Date: 2022-04-15
 * Time: 14:58
 */
public class ExitOperation implements IOperation{

    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统");
        //销毁所有数据
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            bookList.setPos(i,null);
        }
        System.exit(0);
    }
}

FindOperation 类

package operation;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

/**
 * Created with Intellij IDEA.
 * Description:查找图书
 * User: a
 * Date: 2022-04-15
 * Time: 14:58
 */
public class FindOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("查找图书!");
        System.out.println("请输入你要查找图书的名字:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        int i = 0;
        for (; i < bookList.getUsedSize(); i++) {
            Book book = bookList.getPos(i);
            if (book.getName().equals(name)) {
                System.out.println("找到了,信息如下:");
                System.out.println(book);
            }
        }
        if (i == bookList.getUsedSize()) {
            System.out.println("图书不存在");
        }

    }
}

IOperation 类

package operation;


import Book.BookList;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: a
 * Date: 2022-04-15
 * Time: 15:00
 */
public interface IOperation {
   void work(BookList bookList);
}

ReturnOperation 类

package operation;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

/**
 * Created with Intellij IDEA.
 * Description:归还图书
 * User: a
 * Date: 2022-04-15
 * Time: 14:59
 */
public class ReturnOperation 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.getUsedSize(); i++) {
            Book book = bookList.getPos(i);
            if (book.getName().equals(name)) {
                book.setBorrow(false);
                System.out.println("归还成功");
                return;
            }
        }
        System.out.println("归还图书不存在!");
    }
}

User包

AdminUser类

package User;

import Book.BookList;
import operation.*;

import java.util.Scanner;

/**
 * Created with Intellij IDEA.
 * Description:管理员
 * User: a
 * Date: 2022-04-15
 * Time: 15:01
 */
public class AdminUser extends User{
    public AdminUser(String name) {
        super(name);

        this.iOperations = new IOperation[] {
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DelOperation(),
                new DisplayOperation()
        };
    }

    @Override
    public int menu() {
        System.out.println("Hello" + this.name);
        System.out.println("1.查找图书");
        System.out.println("2.增加图书");
        System.out.println("3.删除图书");
        System.out.println("4.显示图书");
        System.out.println("0.退出系统");
        System.out.println("请输入你的操作: ");
        Scanner input = new Scanner(System.in);
        int choice = input.nextInt();
        return choice;


    }

}

NormalUser 类

package User;

import operation.*;

import java.util.Scanner;

/**
 * Created with Intellij IDEA.
 * Description:普通用户
 * User: a
 * Date: 2022-04-15
 * Time: 15:01
 */
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("hello "+this.name);
        System.out.println("1.查找图书");
        System.out.println("2.借阅图书");
        System.out.println("3.归还图书");
        System.out.println("0.退出系统");
        System.out.println("请输入你的操作:");
        Scanner input = new Scanner(System.in);
        int choice = input.nextInt();
        return choice;
    }
}

User 类

package User;

import Book.BookList;
import operation.IOperation;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: a
 * Date: 2022-04-15
 * Time: 15:01
 */
public abstract class User {
    protected String name;

    public IOperation[] iOperations;

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

    public abstract int menu(); //根据选择动态绑定普通用户和管理员

    //用户操作权限不同,对应功能也不同
    public void doOperation(int choice, BookList bookList) {
        iOperations[choice].work(bookList);
    }
}

Main 类

import Book.BookList;
import User.User;
import User.AdminUser;
import User.NormalUser;

import java.util.Scanner;

/**
 * Created with Intellij IDEA.
 * Description:主逻辑
 * User: a
 * Date: 2022-04-15
 * Time: 15:47
 */
public class Main {
    //载入用户信息
    public static User login() {
        System.out.println("请输入姓名:");
        Scanner input = new Scanner(System.in);
        String name = input.nextLine();
        System.out.println("请输入你的身份:1 -> 管理员 2 -> 普通用户");
        int choice = input.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 = login();
        while (true) {
            int choice = user.menu();
            user.doOperation(choice,bookList);
        }
    }
}

你可能感兴趣的:(JavaSE,java,后端,开发语言)