Java实现简单的图书管理系统

简介

通过登录确认自己的身份,如果是管理员,对图书进行增,删,查,显示等,如果是学生,则实现借阅,归还,查找等功能。
在这里插入图片描述
在这里插入图片描述

知识点

链接: link.

封装

链接: link.

链接: link.

继承

链接: link.

接口

链接: link.

多态

链接:link.

创建书(Book)

package book;

public class Book {
    /*一本书的属性包括书名,作者,价格,类型,编号,以及状态*/
    private String name;
    private String author;
    private int price;
    private String type;
    private int num;
    private boolean status;

    //提供构造方法
    public Book(String name, String author, int price, String type, int num) {
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
        this.num = num;
    }

    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 int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    //toString 方法打印
    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ", num=" + num +
                ((status == true) ? " 已借出 " : " 未借出 ") +
                '}';
    }
}

书架(BookList)

这里提前往书架放了三本书

package book;

public class BookList {
    //书都是同一类型的元素,创建一个Book类型的数组
    private Book[] books;
    private int size;

    //先给书架放三本书
    public BookList() {
        this.books = new Book[10];
        books[0] = new Book("张","小张",10,"小说",1);
        books[1] = new Book("王","小王",23,"小说",3);
        books[2] = new Book("李","小李",20,"小说",2);
        this.size = 3;
    }


    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

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

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

主函数(Main)

import book.BookList;
import user.Administrator;
import user.Ordinary;
import user.User;

import java.util.Scanner;

public class Main {
    public static User login() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入姓名:");
        String name = sc.next();
        System.out.println("请确认您的身份:1 > 管理员  0 > 学生");
        int choice = sc.nextInt();
        if(choice == 1){
            return new Administrator(name);
        }else{
            return new Ordinary(name);
        }
    }
    public static void main(String[] args) {
        BookList bl = new BookList();
        //登录
        User user = login();
        //选择菜单
        while(true){
            int choice = user.menu();
            user.doOperation(choice,bl);
        }

    }
}

用户(User)

package user;

import book.BookList;
import operation.IOperation;
//抽象类,主要为了被继承
public abstract class User {
    public String name;
    public IOperation[] operation;

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

    public abstract int menu();

    public void doOperation(int choice, BookList bl) {
        this.operation[choice].work(bl);
    }
}

管理员(Administrator)

package user;

import operation.*;

import java.util.Scanner;

public class Administrator extends User{

    public Administrator(String name) {
        super(name);
        this.operation = new IOperation[]{new ExiOperation(),new FinOperation(),new AddOperation(),new DelOperation(),new DisOperation()};
    }

    @Override
    public int menu() {
        System.out.println("欢迎" + this.name + "进入图书管理系统!");
        Scanner sc = new Scanner(System.in);
        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("=============");
        int choice = sc.nextInt();
        return choice;
    }
}

接口(IOperation)

package operation;

import book.BookList;

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

查(FinOperation)

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class FinOperation implements IOperation{
    @Override
    public void work(BookList bl) {
        System.out.println("查找书籍");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要查找的书籍名字:");
        String name = sc.next();
        for (int i = 0; i < bl.getSize(); i++) {
            Book book = bl.getBook(i);
            if(book.getName().equals(name)){
                System.out.println("有此书籍!");
                System.out.println(book);
                return;
            }
        }
        System.out.println("未找的您需要的书籍!");
    }
}

增(AddOperation)

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOperation {
    @Override
    public void work(BookList bl) {
        Scanner sc = new Scanner(System.in);
        System.out.println("添加图书");
        System.out.println("请输入图书的名字:");
        String name = sc.nextLine();
        System.out.println("请输入图书的作者:");
        String author = sc.nextLine();
        System.out.println("请输入图书价格:");
        int price = sc.nextInt();
        System.out.println("请输入图书的类型:");
        String type = sc.next();
        System.out.println("请输入图书的编号:");
        int num = sc.nextInt();

        Book book = new Book(name,author,price,type,num);
        int curSize = bl.getSize();//拿到长度
        bl.setBooks(curSize,book);
        bl.setSize(curSize+1);
        System.out.println("添加成功!");
    }
}

删(DelOperation)

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{
    @Override
    public void work(BookList bl) {
        System.out.println("删除图书");
        System.out.println("请输入需要删除的书籍名字:");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        int i = 0;
        for (; i < bl.getSize(); i++) {
            Book book = bl.getBook(i);
            if(book.getName().equals(name)){
                break;
            }
        }
        if(i >= bl.getSize()){
            System.out.println("没有找到此书籍");
            return;
        }
        int j = i;
        for(; j < bl.getSize() - 1; j++){
            Book book = bl.getBook(j + 1);
            bl.setBooks(j,book);
        }
        int cueSize = bl.getSize();
        bl.setSize(cueSize-1);
        System.out.println("成功删除此书籍!");
    }
}

显示(DisOperation)

package operation;

import book.Book;
import book.BookList;

public class DisOperation implements IOperation{
    @Override
    public void work(BookList bl) {
        System.out.println("显示图书");
        for (int i = 0; i < bl.getSize(); i++) {
            Book book = bl.getBook(i);
            System.out.println(book);
        }
    }
}

退出(ExiOperation)

package operation;

import book.BookList;

public class ExiOperation implements IOperation{
    @Override
    public void work(BookList bl) {
        System.out.println("退出系统");
        System.exit(1);
    }
}

学生(Ordinary)

package user;

import operation.*;

import java.util.Scanner;

public class Ordinary extends User {

    public Ordinary(String name) {
        super(name);
        this.operation = new IOperation[]{new ExiOperation(),new FinOperation(),new BorrOperation(),new RetOperation(),new DisOperation()};
    }

    @Override
    public int menu() {
        System.out.println("欢迎" + this.name + "同学进入图书管理系统!");
        Scanner sc = new Scanner(System.in);
        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("==============");
        int choice = sc.nextInt();
        return choice;
    }
}

查(FinOperation)

同管理员

借(BorrOperation)

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class BorrOperation implements IOperation{
    @Override
    public void work(BookList bl) {
        System.out.println("借阅书籍");
        System.out.println("请输入需要借阅的书籍名字:");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        for (int i = 0; i < bl.getSize(); i++) {
            Book book = bl.getBook(i);
            if(book.getName().equals(name)) {
                System.out.println("请取走您要的书籍!");
                book.setStatus(true);
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有你需要的书籍!");
    }
}

归(RetOperation)

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class RetOperation implements IOperation{
    @Override
    public void work(BookList bl) {
        System.out.println("归还图书");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要归还的书籍名字!");
        String name = sc.nextLine();
        for (int i = 0; i < bl.getSize(); i++) {
            Book book = bl.getBook(i);
            if(book.getName().equals(name)) {
                book.setStatus(false);
                System.out.println(book);
                return;
            }
        }
    }
}

显示(DisOperation)

同管理员

退出(ExiOperation)

同管理员

管理员运行截图

在这里插入图片描述

学生运行结果

在这里插入图片描述

评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值