图书管理系统

本文描述了一个简单的Java图书管理系统,包括Book类用于表示书籍,BookList类作为书架存储书籍,User类作为用户基类,AdminUser和NormalUser分别表示管理员和普通用户。系统提供了增加、查找、删除、借出、归还和展示图书的功能。
摘要由CSDN通过智能技术生成

一,Book包

1.1 Book类

定义一个书所拥有的属性:

package Book;

public class Book {
    //书的属性 :名字,作者,类型,价格,是否被借出
    private String name;
    private String author;
    private String type;
    private int price;
    private boolean borrowed;

    public Book(String name, String author, String type, int price) {
        this.name = name;
        this.author = author;
        this.type = type;
        this.price = price;
    }

    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 String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public boolean isBorrowed() {
        return borrowed;
    }

    public void setBorrowed(boolean borrowed) {
        this.borrowed = borrowed;
    }
    //重写 toString 方法,输出 Book 的属性
    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", type='" + type + '\'' +
                ", price=" + price +
                ((borrowed)?"  已被借出":"  未被借出") +
                /*", borrowed=" + borrowed +*/
                '}';
    }
}

1.2 Booklist类

一个存储书的书架:

package Book;
//书架类
public class BookList {
    Book[] books = new Book[10];//定义可以放几本书
    private int usedSize;//已有书籍的数量
    public BookList(){
        //先存入三本书
        this.books[0] = new Book("三国演义","罗贯中","小说",10);
        this.books[1] = new Book("红楼梦","曹雪芹","小说",20);
        this.books[2] = new Book("西游记","吴承恩","小说",100);
        this.usedSize = 3;
    }
    //得到 pos 位置的书
    public Book getBook(int pos){
        return books[pos];
    }
    //令 pos 位置存储 book 书
    public void setBooks(int pos,Book book){
        books[pos] = book;
    }
    //得到已有书的数量
    public int getUsedSize() {
        return usedSize;
    }
    //改变已有书的数量
    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }
}

二,User包

2.1 User类

是一个抽象类,使用该系统的用户所拥有的共性:

package User;

import Book.BookList;
import Operation.IOperation;
//AdminUser类 和 NormalUser类 的父类
public abstract class User {
    public String name;//使用者的姓名
    public IOperation[] iOperations;//存储我们要调用的功能,通过下标去访问
    public User(String name){
        this.name = name;
    }
    public abstract int menu();//菜单

    public void doOperations(int choice, BookList bookList){
        //调用我们要实现的功能 - 增删查改....
        iOperations[choice].work(bookList);
    }
}

2.2 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 Exit(),
                new Add(),
                new Find(),
                new Del(),
                new Show()
        };
    }
    //管理者的菜单
    @Override
    public int menu() {
        System.out.println("**************************");
        System.out.println("欢迎 " + 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);
        System.out.println("请选择操作:");
        while (true){
            int choice = scanner.nextInt();
            if(choice<=4 && choice>=0)
                return choice;
            System.out.println("输入错误!");
        }
    }
}

2.3 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 Exit(),
                new Borrowed(),
                new Find(),
                new Return()
        };
    }
    //用户的菜单
    @Override
    public int menu() {
        System.out.println("**************************");
        System.out.println("欢迎 " + 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);
        System.out.println("请选择操作:");
        while (true){
            int choice = scanner.nextInt();
            if(choice<=3 && choice>=0)
                return choice;
            System.out.println("输入错误!");
        }
    }
}

2.4 Test类 - - - 测试

package User;

import Book.BookList;

import java.util.Scanner;

public class Test {
    //登录方法 - 得到我们的身份和姓名
    public static User login(){
        System.out.println("请输入姓名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("请选择身份:1-》管理员   0-》用户");
        int ret = scanner.nextInt();
        if(ret == 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.doOperations(choice,bookList);
        }
    }
}

三,Operation包

3.1 Add类 - - -增加图书

package Operation;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

public class Add 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 ret = bookList.getUsedSize();
        for (int i = 0; i < ret; i++) {
            Book book1 = bookList.getBook(i);
            if(book1.getName().equals(name)){
                System.out.println("已经有这本书!");
                return;
            }
        }

        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,type,price);

        bookList.setBooks(ret, book);
        bookList.setUsedSize(ret + 1);
        System.out.println("添加成功!");
    }
}

3.2 Del类 - - - 删除图书

package Operation;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

public class Del 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 ret = bookList.getUsedSize();
        int pos = -1;
        for (int i = 0; i < ret; i++) {
            Book book = bookList.getBook(i);
            if(name.equals(book.getName())){
                pos = i;
                break;
            }
        }
        if(pos == -1){
            System.out.println("没有该书!");
            return;
        }
        for (int i = pos; i < ret - 1; i++) {
            Book book = bookList.getBook(i + 1);
            bookList.setBooks(i,book);
        }
        bookList.setUsedSize(ret - 1);
        System.out.println("删除成功!");
    }
}

3.3 Borrowed类 - - - 借出图书

package Operation;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

public class Borrowed 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 ret = bookList.getUsedSize();
        for (int i = 0; i < ret; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                if(!book.isBorrowed()){
                    book.setBorrowed(true);
                    System.out.println("借书成功!");
                    return;
                }else {
                    System.out.println("已经借出!");
                    return;
                }
            }
        }
        System.out.println("没有这本书!");
    }
}

3.4 Return类 - - - 归还图书

package Operation;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

public class Return 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 ret = bookList.getUsedSize();
        for (int i = 0; i < ret; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                if(book.isBorrowed()){
                    book.setBorrowed(false);
                    System.out.println("还书成功!");
                    return;
                }else {
                    System.out.println("已经归还该书!");
                    return;
                }
            }
        }
        System.out.println("没有这本书!无法归还!");
    }
}

3.5 Find类 - - - 查找图书

import Book.BookList;

import java.util.Scanner;

public class Find implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("查找图书!");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int ret = bookList.getUsedSize();
        for (int i = 0; i < ret; i++) {
            if(name.equals(bookList.getBook(i).getName())){
                System.out.println(bookList.getBook(i));
                return;
            }
        }
        System.out.println("没有该书!");
    }
}

3.6 Exit类 - - - 退出系统

package Operation;

import Book.BookList;

public class Exit implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统!");
        int ret = bookList.getUsedSize();
        for (int i = 0; i < ret; i++) {
            //将已有书籍置为空
            bookList.setBooks(i,null);
        }
        //将书籍数目置为0
        bookList.setUsedSize(0);
        //退出程序 ---  JAVA用法
        System.exit(0);
    }
}

3.7 Show类 - - - 展示图书

package Operation;

import Book.Book;
import Book.BookList;

public class Show implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("展示图书!");
        int ret = bookList.getUsedSize();
        for(int i = 0; i < ret; i++){
            Book book = bookList.getBook(i);
            System.out.println(book);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一叶祇秋

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

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

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

打赏作者

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

抵扣说明:

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

余额充值