图书管理系统

作者简介: 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);
        }

    }
}

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

相关推荐
伏虎山真人10 分钟前
开源数据库 - mysql - mysql-server-8.4(gtid主主同步+ keepalived热切换)部署方案
数据库·mysql·开源
徐*红10 分钟前
java 线程池
java·开发语言
尚学教辅学习资料10 分钟前
基于SSM的养老院管理系统+LW示例参考
java·开发语言·java毕设·养老院
2401_8576363910 分钟前
计算机课程管理平台:Spring Boot与工程认证的结合
java·spring boot·后端
1 9 J12 分钟前
Java 上机实践4(类与对象)
java·开发语言·算法
Code apprenticeship13 分钟前
Java面试题(2)
java·开发语言
也无晴也无风雨1 小时前
深入剖析输入URL按下回车,浏览器做了什么
前端·后端·计算机网络
憨子周1 小时前
2M的带宽怎么怎么设置tcp滑动窗口以及连接池
java·网络·网络协议·tcp/ip
Martin -Tang2 小时前
Vue 3 中,ref 和 reactive的区别
前端·javascript·vue.js
FIN技术铺3 小时前
Redis集群模式之Redis Sentinel vs. Redis Cluster
数据库·redis·sentinel