图书管理系统

作者简介: zoro-1,目前大一,正在学习Java,数据结构等

作者主页: zoro-1的主页

欢迎大家点赞 👍 收藏 ⭐ 加关注哦!💖💖

图书管理系统

book包

Book

java 复制代码
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=" + isBorrowed +
                '}';
    }
}

BookList

java 复制代码
public class BookList {
    private Book[] books ;
    private int usedSize;//记录当前书架上 实际存放的书的 数量
    private static final int DEFAULT_CAPACITY = 10;

    public BookList() {
        this.books = new Book[DEFAULT_CAPACITY];
        //放好书!!
        this.books[0] = new Book("三国演义","罗贯中",10,"小说");
        this.books[1] = new Book("西游记","吴承恩",9,"小说");
        this.books[2] = new Book("红楼梦","曹雪芹",19,"小说");

        this.usedSize = 3;
    }

    public int getUsedSize() {
        return usedSize;
    }

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

    public Book getBook(int pos) {
        return books[pos];
    }

    public void setBooks(int pos,Book book) {
        books[pos] = book;
    }


    public Book[] getBooks() {
        return books;
    }
}

operation包

Add

java 复制代码
import book.Book;
import book.BookList;

import java.util.Scanner;

public class Add implements Io{
    @Override
    public void work(BookList bookList) {
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入新增图书的名字");
        String name=scanner.nextLine();
        System.out.println("请输入新增图书的作者");
        String author=scanner.nextLine();
        System.out.println("请输入新增图书的价格");
        int price=scanner.nextInt();
        System.out.println("请输入新增图书的类型");
        String type=scanner.nextLine();
        Book book=new Book(name,author,price,type);
        int size=bookList.getUsedSize();
        if (size==bookList.getBooks().length){
            System.out.println("书架满了");
            return;
        }
        for(int i=0;i<size;i++)
        {
            if (bookList.getBook(i).getName().equals(name)){
                System.out.println("已经有这本书了");
                return;
            }
        }
        bookList.setBooks(size,book);
        bookList.setUsedSize(size+1);

    }
}

Borrow

java 复制代码
import book.BookList;

import java.util.Scanner;

public class Borrow implements Io{
    @Override
    public void work(BookList bookList) {
        System.out.println("请输入您要借的书名");
        Scanner sc=new Scanner(System.in);
        String name=sc.nextLine();
        int size=bookList.getUsedSize();
        for(int i=0;i<size;i++){
            if(bookList.getBook(i).getName().equals(name)){
                System.out.println("有您要借的书");
                System.out.println(bookList.getBook(i));
            }
        }
        System.out.println("您要借的书不存在");
    }
}

Exit

java 复制代码
import book.BookList;

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

Find

java 复制代码
import book.Book;
import book.BookList;

import java.util.Scanner;

public class Find implements Io{
    @Override
    public void work(BookList bookList) {
        System.out.println("查找图书!");

        Scanner scanner = new Scanner(System.in);
        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("没有找到这本书!");
    }
}

Io

java 复制代码
import book.BookList;

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

Return

java 复制代码
import book.BookList;

import java.util.Scanner;

public class Return implements Io{
    @Override
    public void work(BookList bookList) {
        System.out.println("请输入您要归还图书名");
        Scanner sc=new Scanner(System.in);
        String name=sc.nextLine();
        int szie=bookList.getUsedSize();
        for(int i=0;i<szie;i++){
            if(bookList.getBook(i).getName().equals(name)){
                bookList.getBook(i).setBorrowed(false);
                System.out.println("归还成功");
                return;
            }
        }
        System.out.println("未找到您要归还的图书");
    }
}

Show

java 复制代码
import book.BookList;

public class Show implements Io{
    @Override
    public void work(BookList bookList) {
        System.out.println("打印图书");
        int size=bookList.getUsedSize();
        for(int i=0;i<size;i++){
            System.out.println(bookList.getBook(i));
        }
    }
}

user包

Max

java 复制代码
import operation.*;

import java.util.Scanner;

public class Max extends Users{
    public Max(String name){
        //父类先进行构造
        super(name);
        this.io = new Io[]{
                new Exit(),
                new Find(),
                new Add(),
                new Del(),
                new Show(),
        };
    }
    @Override
    public int menu() {
        System.out.println("**********管理员用户*****");
        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("请输入你的操作:");
        int choice = scanner.nextInt();

        return choice;
    }
}

Min

java 复制代码
import operation.*;

import java.util.Scanner;

public class Min extends Users{
    public Min(String name){
        super(name);
        this.io = new Io[]{
                new Exit(),
                new Find(),
                new Borrow(),
                new Return()

        };
    }
    @Override
    public int menu() {
        System.out.println("**********普通用户******");
        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("请输入你的操作:");
        int choice = scanner.nextInt();

        return choice;
    }
}

Users

java 复制代码
import book.BookList;
import operation.Io;

public abstract class Users  {
private String name;
public Io io[];
public Users(String name){
    this.name=name;
}
public abstract int menu();
public void choice(int choice, BookList bookList){
    io[choice].work(bookList);
}

}

主函数

java 复制代码
import book.BookList;
import user.Max;
import user.Users;

import java.util.Scanner;

public class Main {
    public static Users shenfen(){
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入您的身份");
        String name= sc.nextLine();
        if(name.equals("Max")){
            return new Max(name);
        }
        if(name.equals("Min")) {
            return new Max(name);
        }
        return null;
    }
    public static void main(String[] args) {
        Users users=shenfen();
        Scanner sc=new Scanner(System.in);
        BookList bookList=new BookList();
        while (true){
            int chioce=users.menu();
            users.choice(chioce,bookList);
        }

    }
}

今天分享到这里就结束了,希望大家支持一下

相关推荐
vvvae123422 分钟前
分布式数据库
数据库
哎呦没23 分钟前
大学生就业招聘:Spring Boot系统的架构分析
java·spring boot·后端
_.Switch42 分钟前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
雪域迷影42 分钟前
PostgreSQL Docker Error – 5432: 地址已被占用
数据库·docker·postgresql
一路向前的月光1 小时前
Vue2中的监听和计算属性的区别
前端·javascript·vue.js
长路 ㅤ   1 小时前
vite学习教程06、vite.config.js配置
前端·vite配置·端口设置·本地开发
长路 ㅤ   1 小时前
vue-live2d看板娘集成方案设计使用教程
前端·javascript·vue.js·live2d
编程、小哥哥1 小时前
netty之Netty与SpringBoot整合
java·spring boot·spring
Fan_web1 小时前
jQuery——事件委托
开发语言·前端·javascript·css·jquery
安冬的码畜日常1 小时前
【CSS in Depth 2 精译_044】第七章 响应式设计概述
前端·css·css3·html5·响应式设计·响应式