【迷人的爪哇】——Java实现简单图书管理系统

本文介绍了一个图书管理系统,包括用户登录、身份验证,以及使用不同操作进行图书增删查借还的功能。核心部分展示了AdminUser和NormalUser类,以及各种操作接口如AddOperation、BorrowOperation等。
摘要由CSDN通过智能技术生成

一、主函数Mian

import book.BookList;
import user.AdminUser;
import user.NormalUser;
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: -> 管理员,0: -> 普通用户");
        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 = login();
        while(true) {
            int choice = user.menu();//动态绑定

            user.doOperation(choice,bookList);
        }
    }
}

二、Book

Book:

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) ? "-已经借出了":"-未借出")+
                '}';
    }
}

书架BookList:

package book;
public class BookList {
    //最多存放8本书
    private Book[] books = new Book[8];

    private int usedSize;//实时记录,当前book的个数

    public BookList() {
        books[0] = new Book("三国演义","罗贯中",10,"历史小说");
        books[1] = new Book("西游记","吴承恩",15,"玄幻小说");
        books[2] = new Book("红楼梦","曹雪芹",20,"言情小说");
        books[3] = new Book("水浒传","施耐庵",18,"历史小说");
        usedSize = 4;
    }

    /**
     * @param pos 当前不考虑非法的,所以其一定是合法的
     * @return 一本书
     */
    public Book getBook(int pos){
        return books[pos];
    }

    /**
     *
     * @param pos 合法的
     * @param book 你要放的书
     */
    public void setBooks(int pos,Book book){
        books[pos] = book;
    }


    /**
     *
     * @return 实时获取当前书的数目
     */
    public int getUsedSize(){
        return usedSize;
    }

    /**
     *
     * @param size 修改当前书的数目
     */
    public void SetUsedSize(int size){
        usedSize = size;
    }


}

三、Operation

定义一个接口:

package operation;

import book.BookList;


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

AddOperation

package operation;

import book.Book;
import book.BookList;

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();
        bookList.setBooks(currentSize,book);
        bookList.SetUsedSize(currentSize+1);
        System.out.println("新增图书成功!");
    }
}

BorrowOperation

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("借阅成功");
                return;
            }
        }
    }
}

  

DelOperation

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 currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
                for (int j = i; j < currentSize; j++) {
                    bookList.setBooks(j,bookList.getBook(j+1));
                }
                System.out.println("删除图书成功!");
                bookList.SetUsedSize(currentSize-1);
                return;
            }
        }
    }
}

DisplayOperation

package operation;

import book.Book;
import book.BookList;


public class DisplayOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("显示图书");
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            System.out.println(bookList.getBook(i));
        }
    }
}

ExitOperation

package operation;

import book.BookList;


public class ExitOperation implements IOperation{

    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统");
        System.exit(0);
    }
}

FindOperation

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);
        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("找到了这本书!");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有你要找的这本书!");
    }
}

ReturnOperation

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("归还成功");
                return;
            }
        }
    }
}

四、user

父类:

package user;

import book.BookList;
import operation.IOperation;


public abstract class User {
    public String name;
    protected IOperation[] iOperations;
    public User(String name) {
        this.name = name;
    }

    public abstract int menu();

    public void doOperation(int choice, BookList bookList){
        this.iOperations[choice].work(bookList);
    }
}

AdminUser 

package user;

import book.BookList;
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 DisplayOperation()
        };
    }

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

NormalUser

package user;

import operation.*;

import java.util.Scanner;

public class NormalUser extends User{

    public NormalUser(String name) {
        super(name);
        this.iOperations = new IOperation[]{
                new ExitOperation(),
                new FindOperation(),
                new BorrowOperation(),
                new ReturnOperation()
        };
    }

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值