[Java]图书管理系统

系统需求

1.登录

2.身份选择:管理员和用户

3.管理员功能:查找图书、新增图书、删除图书、显示图书、退出系统。

4.用户功能:查找图书、借阅图书、归还图书、退出系统。

设计:

分别设计三个包:book包、operation包和user包。book包用来存储原始书籍,operation包用来实现各种功能,user包来区别管理员和用户的功能。最后用Main的class来连接实现。

Book

package book;

public class Book {
    private String name;
    private String author;
    private int price;
    private String type;//书的类型
    private boolean isBorrowed;//默认是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 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 {

    private Book[] bookList = new Book[10];
    private int usedSize;//当前书架上有几本书

    public BookList(){
        this.bookList[0] = new Book("三国演义","罗贯中",18,"小说");
        this.bookList[1] = new Book("西游记","吴承恩",20,"小说");
        this.bookList[2] = new Book("红楼梦","曹雪芹",45,"小说");
        this.bookList[3] = new Book("水浒传","施耐庵",34,"小说");
        this.usedSize = 4;
    }

    public int getUsedSize() {
        return usedSize;
    }

    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }
    //把书放到pos位置
    public void setBookList(int pos,Book book){
        bookList[pos] = book;
    }
    //获取pos位置的书
    public Book getBook(int pos){
        return bookList[pos];
    }
    //这里本质上可以写借阅书籍等操作
    
}

AddOperation

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOperation {

    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.setBookList(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 {

    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("借阅失败,没有这本书!");
    }
}

DelOperation

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation {

    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();
        int index = -1;
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
                index = i;
                break;
            }
        }
        //如果index == -1 说明没有这本书
        if (index == -1){
            System.out.println("没有你要删除的图书!");
            return;
        }
        //这里开始删除
        for (int i = index; i < currentSize - 1; i++) {
            //bookList[i] = bookList[i + 1];
            Book book = bookList.getBook(i + 1);
            bookList.setBookList(i,book);
        }
        bookList.setUsedSize(currentSize - 1);
    }
}

DisplayOperation

package operation;

import book.Book;
import book.BookList;

public class DisplayOperation implements IOperation {

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

ExitOperation

package operation;

import book.Book;
import book.BookList;

public class ExitOperation implements IOperation {
    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 {

    public void work(BookList bookList){

        System.out.println("查找图书!");
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入你要查找的书籍:");
        String name = scanner.nextLine();

        int size = bookList.getUsedSize();
        for (int i = 0; i < size; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
                System.out.println("找到了这本书:");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有这本书!");
    }
}

RetutnOperation

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class ReturnOperation implements IOperation {

    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("归还失败,没有这本书!");
    }
}

IOperation

package operation;

import book.BookList;

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

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 sca = new Scanner(System.in);
        int choice = sca.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(),
                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("0.退出系统");
        System.out.println("请输入你的操作:");
        Scanner sca = new Scanner(System.in);
        int choice = sca.nextInt();
        return choice;
    }
}

User

package user;

import book.Book;
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){

        this.iOperations[choice].work(bookList);
    }
}

Main

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();
        //User user = new AdminUser(name)
        //User user = new NormalUser(name)

        while (true) {
            int choice = user.menu();
            user.doOperation(choice, bookList);
        }
    }
}
  • 14
    点赞
  • 87
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值