**

基本思路

**此系统的对象有 1:书籍 2:使用者(用户和图书管理员)

1. 图书:它的属性有书名,编号,作者,类别,价格,状态(是否被借出)
2. 使用者
2.1 管理员:姓名,增加书籍,删除书籍,查找书籍,打印书籍信息,退出系统一些简单的操作。
2.2 用户:姓名,查找书籍 ,借阅书籍,归还书籍,退出系统操作。

创建一个BookManage类

package Demo;

public class BookManage {
    private String name;
    private String id;
    private String auther;
    private String type;
    private int price;
    private boolean isBorrow;

    public BookManage(String name,String id,String auther,
                      String type,int price,boolean isBorrow){
        this.name = name;
        this.id = id;
        this.auther = auther;
        this.type = type;
        this.price = price;
        this.isBorrow = isBorrow;
    }

    public String getId(){
        return id;
    }

    public Boolean isBorrow(){
        return isBorrow;
    }

    public void setBorrow(boolean borrow){
        isBorrow = borrow;
    }

    public String getName(){
        return name;
    }

    //重写
@Override
    public String toString() {
        return "BookManage{" +
                "name='" + name + '\'' +
                ", id='" + id + '\'' +
                ", auther='" + auther + '\'' +
                ", type='" + type + '\'' +
                ", price=" + price +
                ", isBorrow=" + isBorrow +
                '}';
    }

创建一个类保存书籍

package Demo;

import java.awt.print.Book;

public class BookList {
    private  BookManage[] books = new BookManage[100];
    private int size;

    public BookList(){
        books[0] = new BookManage("数据库","001","王珊","数学课本",49,false);
        books[1] = new BookManage("信号与系统分析","002","吴大正","数学课本",32,false);
        books[2] = new BookManage("计算机组成原理","003","蒋本珊","数学课本",45,false);
        size = 3;
    }

    public int getSize(){
        return size;
    }

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

    public BookManage getBook(int index){
        return books[index];
    }

    public void setBooks(int index, BookManage book) {
        books[index] = book;
    }
}

我们将所有的操作方法放到operation包里,所有方法用接口来实现。

package operation;

import Demo.BookList;

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

1.增加书籍

import Demo.BookManage;

import java.util.Scanner;

public class AddBookoperation implements IOperation{

    @Override
    public void work(BookList bookList) {
        System.out.println("新增一本书籍!");
        System.out.println("请输入新增书籍的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        System.out.println("请输入新增书籍的id:");
        String id = scanner.next();
        System.out.println("请输入新增书籍的作者:");
        String author = scanner.next();
        System.out.println("请输入新增书籍的类型:");
        String type = scanner.next();
        System.out.println("请输入新增书籍的价格:");
        int price = scanner.nextInt();
        BookManage book = new BookManage(name,id,author,type,price,false);
        bookList.setBooks(bookList.getSize(), book);
        bookList.setSize(bookList.getSize()+1);
    }
}

2.删除书籍

package operation;

import Demo.BookList;
import Demo.BookManage;

import java.util.Scanner;

public class DeleteOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("删除书籍!");
        System.out.println("请输入您想要删除的书籍的id:");
        Scanner scanner = new Scanner(System.in);
        String id = scanner.next();
        int i = 0;
        for (; i < bookList.getSize(); i++) {
            BookManage book = bookList.getBook(i);
            if(book.getId().equals(id)) {
                break;
            }
        }
        if(i >= bookList.getSize()) {
            System.out.println("未找到要找的书籍!");
            return;
        }

        BookManage lastBook = bookList.getBook(bookList.getSize()-1);
        bookList.setBooks(i, lastBook);
        bookList.setSize(bookList.getSize() - 1);
        System.out.println("删除成功!");
    }
}

3.查找书籍

package operation;

import Demo.BookList;
import Demo.BookManage;

import java.util.Scanner;

public class Findoperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("查找书籍!");
        System.out.println("请输入要查找书籍的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        int count = 0;
        for(int i = 0; i < bookList.getSize(); i++) {
            BookManage book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                System.out.println(book);
                count++;
            }
         }
        if(count == 0) {
            System.out.println("没找到此书籍!");
        } else {
            System.out.println("共找到" + count + "本相同书籍!");
        }
    }
}

4.借阅书籍

package operation;

import Demo.BookList;
import Demo.BookManage;

import java.util.Scanner;

public class BorrowOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅书籍!");
        System.out.println("请输入要借阅书籍的id:");
        Scanner scanner = new Scanner(System.in);
        String id = scanner.next();

        for (int i = 0; i < bookList.getSize(); i++) {
            BookManage book = bookList.getBook(i);
            if (!book.getId().equals(id)) {
                continue;
            }
            if (book.isBorrow()) {
                System.out.println("此书已经被借走了!");
                break;
            }
            book.setBorrow(true);
            System.out.println("借阅成功");
        }
    }
}
  1. 打印所有书籍信息
import Demo.BookList;
import Demo.BookManage;

public class printAlloperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        int i;
        for (i = 0; i < bookList.getSize(); i++) {
            System.out.println(bookList.getBook(i));
        }
        System.out.println("共有" + bookList.getSize() + "本书籍");
    }
}

6.退出系统

package operation;

import Demo.BookList;
import Demo.BookManage;

import javax.imageio.plugins.bmp.BMPImageWriteParam;
import java.util.Scanner;

public class ReturnOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("归还书籍!");
        System.out.println("请输入要归还书籍的id:");
        Scanner scanner = new Scanner(System.in);
        String id = scanner.next();
        int i = 0;
        for(; i < bookList.getSize(); i++) {
            BookManage  book = bookList.getBook(i);
            if(!book.getId().equals(id)) {
                continue;
            }
            if(!book.isBorrow()) {
                System.out.println("这本书未借出去,归还失败!");
            }
            book.setBorrow(false);
        }
    }
}

抽象用户代码

package user;

import Demo.BookList;
import operation.IOperation;

    abstract public class User {
        protected String name;

        protected IOperation[] operations;

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

    abstract public int menu();

    public void doOpration(int choice, BookList bookList) {
        operations[choice].work(bookList);
    }
}

管理员代码

package user;
import Demo.BookList;
import Demo.Delete;
import operation.*;

import java.util.Scanner;


public class Admin extends User {
    public Admin(String name) {
        super(name);
        operations = new IOperation[]{
                new ExitOperation(),
                new AddBookoperation(),
                new DeleteOperation(),
                new Findoperation(),
                new printAlloperation(),
        };
    }

    @Override
    public int menu() {
        System.out.println("-------------------");
        System.out.println("hello" + name);
        System.out.println("1.增加书籍");
        System.out.println("2.删除书籍");
        System.out.println("3.查找书籍");
        System.out.println("4.打印所有信息");
        System.out.println("0.退出");
        System.out.println("-------------------");
        System.out.println("请输入您的选择:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

用户代码

package user;

import operation.*;
import operation.IOperation.*;
import java.util.Scanner;

public class NormalUser extends User {
    public NormalUser(String name) {
        super(name);
        operations = new IOperation[] {
                new ExitOperation(),
                new Findoperation(),
                new BorrowOperation(),
                new ReturnOperation(),
        };
    }

    @Override
    public int menu() {
        System.out.println("----------------");
        System.out.println("hello" + name);
        System.out.println("1.查找书籍");
        System.out.println("2.借阅书籍");
        System.out.println("3.归还书籍");
        System.out.println("0.退出");
        System.out.println("----------------");
        System.out.println("请输入您的选择:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;

    }
}

结束