图书管理系统java实现

目录

包结构

books

Book

BookList

operation

AddOperation

BorrowOperation

DelOperation

DisplayOperation

ExitOperation

FindOperation

ReturnOperation

IOperation

user

AdminUser

NormalUser

User

Main


包结构

books

Book

package books;

public class Book {
    private String name;//书名
    private String author;//作者
    private double price;//价格
    private String type;//类型
    private boolean isBorrowed;//是否借出

    public Book(String name, String author, double 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 double getPrice() {
        return price;
    }

    public void setPrice(double 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 books;

public class BookList {
    private Book[] books = new Book[10];
    private int usedSize;

    public BookList(){
        books[0] = new Book("三国演义","罗贯中",30,"小说");
        books[1] = new Book("水浒传","施耐庵",40,"小说");
        books[2] = new Book("红楼梦","曹雪芹",50,"小说");
        books[3] = new Book("西游记","吴承恩",60,"小说");

        this.usedSize = 4;
    }

    public int getUsedSize() {
        return usedSize;
    }

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

    //获取pos位置的书
    public Book getPos(int pos){
        return this.books[pos];
    }
    //设置pos下标为一本书
    public void setBook(int pos,Book book){
        this.books[pos] = book;
    }
}

operation

AddOperation

package operation;

import books.Book;
import books.BookList;

public class AddOperation implements IOperation {
    public void work(BookList bookList){
        System.out.println("新增图书:>");
        System.out.println("请输入图书的名字:>");
        String name = scanner.nextLine();

        System.out.println("请输入图书的作者:>");
        String author = scanner.nextLine();

        System.out.println("请输入图书的类型:>");
        String type = scanner.nextLine();

        System.out.println("请输入图书的价格:>");
        double price = scanner.nextDouble();

        Book book = new Book(name,author,price,type);
        int size = bookList.getUsedSize();
        bookList.setBook(size,book);

        bookList.setUsedSize(size+1);
        System.out.println("图书新增成功!:>");

    }
}

BorrowOperation

package operation;

import books.Book;
import books.BookList;

public class BorrowOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("借阅图书:>");

        System.out.println("你想借阅什么书? :>");
        String name = scanner.nextLine();

        int size = bookList.getUsedSize();
        for(int i = 0; i < size; i++){
            Book book = bookList.getPos(i);
            if(name.equals(book.getName())){
                book.setBorrowed(true);
                System.out.println("借阅成功! :>");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有你要借阅的这本书:>");

    }
}

DelOperation

package operation;

import books.Book;
import books.BookList;

public class DelOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("删除图书:>");

        System.out.println("请输入你要删除的书名:>");
        String name = scanner.nextLine();
        int currentSize = bookList.getUsedSize();
        int i;
        int index = 0;
        for (i = 0; i < currentSize; i++) {
            Book book = bookList.getPos(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.getPos(j+1);
            bookList.setBook(j,book);
        }
        bookList.setBook(currentSize,null);
        bookList.setUsedSize(currentSize-1);
        System.out.println("删除成功!:>");

    }
}

DisplayOperation

package operation;

import books.Book;
import books.BookList;

public class DisplayOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("展示图书:>");

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

ExitOperation

package operation;

import books.BookList;

public class ExitOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("退出系统:>");
        System.exit(0);
    }
}

FindOperation

package operation;

import books.Book;
import books.BookList;

public class FindOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("查找图书:>");
        String name = scanner.nextLine();
        int size = bookList.getUsedSize();
        for(int i = 0; i < size; i++){
            Book book = bookList.getPos(i);
            if(name.equals(book.getName())){
                System.out.println("找到了这本书,信息如下:>");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有找到这本书:>");
    }
}

ReturnOperation

package operation;

import books.Book;
import books.BookList;

public class ReturnOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("归还图书:>");

        System.out.println("你想归还什么书? :>");
        String name = scanner.nextLine();

        int size = bookList.getUsedSize();
        for(int i = 0; i < size; i++){
            Book book = bookList.getPos(i);
            if(name.equals(book.getName())){
                book.setBorrowed(false);
                System.out.println("归还成功! :>");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有你要归还的这本书:>");
    }
}

IOperation

package operation;

import books.BookList;

import java.util.Scanner;

public interface IOperation {
    Scanner scanner = new Scanner(System.in);
    void work(BookList bookList);
}

user

AdminUser

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 DisplayOperation()
        };
    }


    public int menu(){
        System.out.println("==========管理员菜单==========");
        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("==========普通用户菜单==========");
        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;
    }
}

User

package user;

import books.BookList;
import operation.IOperation;

import java.security.PrivateKey;

public abstract class User {
    protected String name;

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

    public abstract int menu();

    protected IOperation[] iOperations;

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

Main

import books.Book;
import books.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("请输入你的身份:>");
        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.doWork(choice, bookList);
        }
    }
}

  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

噜噜噜噜鲁先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值