Java SE 图书管理系统1.0

目录

目录

一、图书管理系统简介

二、实现过程及代码

2.1具体代码

1.book包

2.operation包

3.user包

4.Main类

2.2效果展示

一、图书管理系统简介

二、实现过程及代码

2.1具体代码

2.2效果展示 



一、图书管理系统简介

图书管理系统的用户无非就是普通用户管理员。而且这两种用户所能接触到的功能也是不同的。

管理员:

 普通用户:

这些所实现的功能还是有所欠缺,大家可以去升级或者完善他。毕竟是1.0嘛! 


二、实现过程及代码

2.1具体代码

 因为代码过于多,所以在这里我将这个系统分为了三个包来实现

Book:这是和图书存放、取出有关的一个包

Operation:这是图书管理系统操作包

User:这是和用户有关的一个包

而这些包中的类实现了这些功能

Main是用来实现这些操作的一个类


1.Book包

1.1Book类

它是存放图书的信息、以及是否借出 

package book;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: aajio
 * Date: 2022-05-21
 * Time: 17:33
 */
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 Book(String 西游记, String 吴承恩, String s, String 小说) {
    }

    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)?"借出":"未借出")+
                ((isBorrowed == true)?"借出":"未借出")+
                '}';
    }
}

1.2BookList 

 BookList是一个相当于书架的类,在这里因为能力有限,在这里我们默认存储10本书,并且默认已经存储好了三本书

package book;


public class BookList {
    //最多可以放10本书
    private Book[] books = new Book[10];

    private  int usedSize;//记录书架子有多少书

    public BookList(){
        books[0] = new Book("心理学与生活","理查德·格里格,菲利普·津巴多",65,"心理");
        books[1] = new Book("爱的艺术","艾·弗洛姆",59,"心理");
        books[2] = new Book("拆掉思维里的墙","古典",69,"心理");
        usedSize = 3;
    }

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

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

    /**
     * 实时获取当前 书的个数
     */
    public int getUsedSize(){
        return usedSize;
    }

    /**
     * 实时修改当前书架上的书的个数
     * @param usedSize
     */
    public void setUsedSize(int size) {
        usedSize = size;
    }





}

2.Operation包 

2.1接口IOperation 

package operation;

import book.BookList;

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

2.2AddOperation 

 实现新增图书

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOperation{

    @Override
    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.setBooks(currentSize,book);

        bookList.setUsedSize(currentSize+1);
        System.out.println("新增图书成功!");

    }
}

 2.3BorrowOperation

 实现借阅图书

import book.Book;
import book.BookList;

import java.util.Scanner;

public class BorrowOperation 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 currentSize = bookList.getUsedSize();

        for (int i = 0; i < currentSize; i++) {
            Book book  = bookList.getBooks(i);
            if (book.getName().equals(name)){
                book.setBorrowed(true);
                System.out.println("借阅成功!");
                return;
            }
        }

    }
}

2.4DelOperation 

实现删除功能 

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;


public class DelOperation 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 nowSize = bookList.getUsedSize();
        for (int i = 0; i < nowSize; i++) {
            if (name.equals(bookList.getBooks(i).getName())){
                for (int j = 0; j > 0; j--) {
                    Book book = bookList.getBooks(j-1);
                    book = bookList.getBooks(j);
                }
                bookList.setUsedSize(nowSize-1);
                System.out.println("删除成功!");
                return;
            }
        }
        System.out.println("此书已被删除!");


    }
}

 2.5DissplayOperation

显示所有图书 

package operation;

import book.Book;
import book.BookList;


public class DisplayOperation implements IOperation{
    @Override
    public void work(BookList bookList) {

        System.out.println("显示图书!");
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            System.out.println(bookList.getBooks(i));
        }
    }
}

2.6ExitOPeration 

退出系统 

package operation;

import book.BookList;

public class ExitOperation implements IOperation{
    @Override
    public void work(BookList bookList) {

        System.out.println("退出系统!");
        System.exit(0);

    }
}

2.7FindOperation 

 查找图书

import book.Book;
import book.BookList;

import java.util.Scanner;

public class FindOperation 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 currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book  = bookList.getBooks(i);
            if (book.getName().equals(name)){
                System.out.println("找到这本书!");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有此书,数重新输入书名!  ");
    }
}

2.8ReturnOperation 

 归还图书

import book.Book;
import book.BookList;

import java.util.Scanner;

public class ReturnOperation 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 currentSize = bookList.getUsedSize();

        for (int i = 0; i < currentSize; i++) {
            Book book  = bookList.getBooks(i);
            if (book.getName().equals(name)){
                book.setBorrowed(false);
                System.out.println("归还成功!");
                return;
            }
        }
    }
}

3.User包 

 在开始时我说过两个用户。在这个包里边就是来写这两个用户的功能

3. 1User类

User就是其他两个类的父类,其他的类来继承User 

package user;

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 doDperation(int choice, BookList bookList){
        this.iOperations[choice].work(bookList);
    }
}

 3.2AdminUser

管理员 

package user;

import book.BookList;
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("Hello " +this.name+"欢迎来到图书系统1.0版本!");
        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);
        int choice = scanner.nextInt();
        return choice;


    }

}

3.3NormanUser 

普通用户 

package user;

import operation.*;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: aajio
 * Date: 2022-05-21
 * Time: 18:16
 */
public class NormalUser extends User{

    public NormalUser(String name) {
        super(name);
        this.iOperations = new IOperation[]{
                new ExitOperation(),
                new FindOperation(),
                new BorrowOperation(),
                new ReturnOperation(),

        };

    }
    public int menu(){
        System.out.println("Hello " +this.name+"欢迎来到图书系统1.0版本!");
        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);
        int choice = scanner.nextInt();
        return choice;


    }
}

4.Main类 

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("请输入你的身份信息:0->管理员,1->普通用户");
        int choice = scanner.nextInt();

        if (choice == 0){
            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.doDperation(choice,bookList);
         }


    }
}

2.2效果展示 

管理员

 

普通用户

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值