图书管理系统--Java版本

  Java语言是面向对象语言,在写图书管理系统的内容的时候,我们需要从对象的角度去进行思考。

一、核心的需求

  1、简单的登录

  2、管理端

  3、用户端

二、类的设计

  图书管理系统建立于:管理员、书、借阅者这三个对象。

    接下来需要从上述的三个对象进行思考,每个类在内部需要完成的任务。

    管理员: 整理书籍  查阅书籍  增加书籍  删除书籍  打印书籍列表  退出(简单的来说就是实现:增删改查这个四个基本功能和退出

    用   户: 查询书籍  借阅书籍  归还书籍  退  出

这些就是我们在编写程序的时候需要注意到的一些方法。如果有其他需求可以在这个基础上进行添加

 

三、类的实现

  在开始的时候,我创建了三个包,用来存储上述的三个对象,分别是book,User,Operation。

    首先需要确定书有什么主要的特征,即是属性:书名、作者、价格、是否被借出。

      有上方的描述可以先写出一个Book类

public class Book{                                                 

  private String name;//书名

  private String author;//作者 

  private int price;//价格

  private int type;//类型

  private boolean isBorrowed;//是否借出

}

上边的只是个人的写法,构造方法、set和get方法可以通过编辑器进行生成。可以自行百度,我的构造器变量中没有最后一项,因为引用类型的变量,默认为false。

 

用户的设置则是较为简单,我只设置了姓名这个属性:

 

对于操作包来说:主要是将操作进行实现,并且提供给用户和管理员进行相关的调用。

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

package book;

public class BookList {

    public Book[] elem = new Book[10];

    private int usedSize;

    public BookList(){
         this.elem[0] = new Book("三国演义","罗贯中",78,"小说");
        this.elem[1] = new Book("水浒传","施耐庵",78,"小说");
        this.elem[2] = new Book("西游记","吴承恩",98,"小说");
        this.usedSize = 3;
    }

    public void setBook(int pos,Book book){
        this.elem[pos] = book;
    }
    public Book getBook(int pos){
        return this.elem[pos];
    }

    public int getUsedSize() {
        return usedSize;
    }

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

package operation;


import book.Book;
import book.BookList;

public class AddOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("新增图书!");
        System.out.println("请输入书名:");
        String name = this.sc.next();
        System.out.println("请输入作者:");
        String author = this.sc.next();
        System.out.println("请输入价格:");
        int price = this.sc.nextInt();
        System.out.println("请输入类型:");
        String type = this.sc.next();

        Book book = new Book(name,author,price,type);
        int currentSize = bookList.getUsedSize();
        bookList.setBook(currentSize,book);
        bookList.setUsedSize(currentSize+1);
    }
}

package operation;

import book.Book;
import book.BookList;

public class BorrowOperation implements IOperation{
    public void work(BookList booklist){
        System.out.println("租借图书!");
        String name = this.sc.next();
        int currentSize = booklist.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = booklist.getBook(i);
            if(book.getName().equals(name)) {
                book.setBorrowed(true);
                System.out.println(book.isBorrowed());
                break;
            }
        }
    }
}

package operation;

import book.Book;
import book.BookList;

public class DelOperation implements IOperation{
    public void work(BookList booklist){
        System.out.println("删除图书!");
        String name = this.sc.next();
        int currentSize = booklist.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = booklist.getBook(i);
            Book book1 = booklist.getBook(i+1);
            if(book.getName().equals(name)) {
                if(booklist.getBook(i+2) == null){
                    book1 = null;
                }
                booklist.setBook(i,booklist.getBook(i+1));
                break;
            }
        }
    }
}

package operation;

import book.BookList;
import book.Book;
public class DisplayOperation implements IOperation{
    public void work(BookList booklist){
        System.out.println("展示图书!");
        int currentSize = booklist.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = booklist.getBook(i);
            System.out.println(book);
        }
    }

}


package operation;

import book.BookList;

public class ExitOperation implements IOperation{
    public void work(BookList booklist){
        System.out.println("退出系统!");
        System.exit(0);
    }
}

package operation;

import book.BookList;
import book.Book;
public class FindOperation implements IOperation{
    public void work(BookList booklist){
        System.out.println("查找图书!");
        String name = this.sc.next();
        int currentSize = booklist.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = booklist.getBook(i);
            if(book.getName().equals(name)) {
                System.out.println(book);
                break;
            }
        }
    }

}


package operation;

import book.BookList;

import java.util.Scanner;

public interface IOperation {
    Scanner sc = new Scanner(System.in);
    void work(BookList booklist);
}


package operation;

import book.Book;
import book.BookList;

public class ReturnOperation implements IOperation{
    public void work(BookList booklist){
        System.out.println("归还图书!");
        String name = this.sc.next();
        int currentSize = booklist.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = booklist.getBook(i);
            if(book.getName().equals(name)) {
                book.setBorrowed(false);
                System.out.println(book.isBorrowed());
                break;
            }
        }
    }
}


package user;

import operation.*;

import java.util.Scanner;

public class Admin extends User{
    public Admin(String name){
        super(name);
        this.iOperations =new IOperation[] {
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DelOperation(),
                new DisplayOperation(),

        };
    }

    @Override
    public int menu() {
        System.out.println("hello,管理员"+ this.name + ",欢迎使用图书管理系统");
        System.out.println("1、查找图书");
        System.out.println("2、新增图书");
        System.out.println("3、删除图书");
        System.out.println("4、显示所有图书图书");
        System.out.println("0、退出系统");
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        return a;
    }
}


package user;

import operation.*;

import java.util.Scanner;

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

        };
    }

    @Override
    public int menu() {
        System.out.println("hello,"+ this.name + ",欢迎使用图书管理系统");
        System.out.println("1、查找图书");
        System.out.println("2、借阅图书");
        System.out.println("3、归还图书");
        System.out.println("0、退出系统");
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        return a;
    }
}

package user;

import operation.IOperation;

abstract public class User {
    protected String name;
    protected IOperation[] iOperations;
    public User(String name){
        this.name = name;
    }
    public abstract int menu();
    public IOperation doOperation(int ch){
        return this.iOperations[ch];
    }
}


import book.BookList;
import operation.IOperation;
import user.Admin;
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.next();
        System.out.println("请输入你的身份:1是管理员,0是普通用户");

        int choice = scanner.nextInt();
        if(choice == 1){
            return new Admin(name);
        }else{
            return new NormalUser(name);
        }
    }



    public static void main(String[] args) {
        BookList bookList = new BookList();
        User user = login();
        while(true){
            int ch = user.menu();
            IOperation iOperation = user.doOperation(ch);
            iOperation.work(bookList);
        }

    }
}

 

posted @ 2020-10-28 22:21  叁三彡  阅读(343)  评论(0编辑  收藏  举报