图书管理系统

一.思路

我们的图书管理系统里面大概需要三类东西

书,人,操作,所以我们可以根据这三类分为三个包

书包括书和书架

人包括普通读书人和管理员

操作分为普通人操作和管理员操作:

所以我们大概需要这些项目

因为所有操作都需要一个方法,所以我们可以定义一个接口标准:Iop

package operation;

import book.BookList;

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

同样读书人和管理者也有许多相同点,所以可以定义一个父类User

package user;

import book.BookList;
import operation.Iop;

public abstract class User {
    protected String name;
    public abstract int menu();
}

而读书人与管理者菜单是不一样的,所以都需要重写菜单

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("****************");
        System.out.println(name+"你要干什么?");
        Scanner sc = new Scanner(System.in);
        return sc.nextInt();
    }
 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("****************");
        System.out.println(name+"你要干什么?:");
        Scanner sc = new Scanner(System.in);
        return sc.nextInt();
    }

二.核心代码

项目开始,我们需要得到两个东西,一个是来访者是读书人还是管理者,其次是他们选择的操作,所以我们用user和choice把他们存起来

public class Main {
    public static User login() {
        System.out.println("请输入你的名字:");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        System.out.println(name + "欢迎来到图书管理系统");
        System.out.println("你是管理员还是读者?");
        System.out.println("1.管理员");
        System.out.println("2.读者");
        int choice = sc.nextInt();
        if (choice == 1) {
            return new MasterUser(name);
        } else {
            return new NormalUser(name);
        }
    }

    public static void main(String[] args) {
        BookList bookList = new BookList();
        while (true) {
            User user = login();
            int choice = user.menu();
            user.dooperation(choice, bookList);
        }
    }
}

然后我们需要将操作和刚刚我们存起来的信息建立联系

我们在User里面建立一个Iop类型的数组,然后在Master和Normal中分别初始化它,对应上相应的操作,然后在定义一个dooperation操作建立联系

Iop[] iop;
    public void dooperation(int choice, BookList bookList){
        iop[choice].work(bookList);
    }

Normal和Master里面分别初始化

  public NormalUser(String name) {
        this.name = name;
        iop = new Iop[]{
          new ReturnOperation(),
          new FindOperation(),
          new BorrowOperation(),
          new BackOperation()
        };
    }

    public MasterUser(String name) {
        this.name = name;
        this.iop = new Iop[]{
                new ReturnOperation(),
                new FindOperation(),
                new AddOperation(),
                new DeletOperation(),
                new ShowOperation()
        };
    }

Booklist中创建一个Book数组用来放书

package book;

public class BookList {
    public Book[] books = new Book[10];
    int num;

    public BookList() {
        this.books[0] = new Book("三国演义","罗贯中",10,"小说");
        this.books[1] = new Book("西游记","吴承恩",23,"小说");;
        this.books[2] = new Book("红楼梦","曹雪芹",8,"小说");;
        this.num = 3;
    }

    public Book getBooks(int dd) {
        return books[dd]; //传过去的是Booklist中的一个Book类
    }

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

        public int getNum() {
            return num;
        }

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

Book

package book;

public class Book {
    private String name;
    private String man;
    private int price;
    private String type;
    private boolean isborrow;//默认为false

    public Book(String name, String man, int price, String type) {
        this.name = name;
        this.man = man;
        this.price = price;
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setIsborrow(boolean isborrow) {
        this.isborrow = isborrow;
    }

    public boolean getisIsborrow() {
        return isborrow;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", man='" + man + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ((isborrow == true) ? "    借出去了" : "    没借出去") +
                '}';
    }
}

三.实现操作

1.新增图书

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements Iop{
    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书");
        int size = bookList.getNum();
        System.out.println("书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("作者:");
        String man = scanner.nextLine();
        System.out.println("价格:");
        int price = scanner.nextInt();
        System.out.println("类型:");
        scanner.nextLine();
        String type = scanner.nextLine();
        Book book = new Book(name, man, price, type);
        bookList.setBooks(size ,book);
        bookList.setNum(size+1);
        System.out.println("新增成功!");
    }
}

2.删除图书

import book.BookList;

import java.util.Scanner;

public class DeletOperation implements Iop{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        int k = -1;
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要删除的书:");
        String name = scanner.nextLine();
        int size = bookList.getNum();
        for (int i = 0; i < size; i++) {
            if (name.equals(bookList.getBooks(i).getName())) {
                k = i;
                break;
            }
        }
        if (k == -1){
            System.out.println("没有这本书");
            return;
        }
            for (int j = k; j < bookList.books.length-1; j++) {
                bookList.setBooks(j, bookList.getBooks(j + 1));
            }
            bookList.setNum(size - 1);
        System.out.println("删除成功!");
        }
    }

3.查找图书

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class FindOperation implements Iop{
    @Override
    public void work(BookList bookList) {
        System.out.println("查询图书");
        System.out.println("请输入你要查找的书");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int num = bookList.getNum();
        for (int i = 0; i <num ; i++) {
            if (name.equals(bookList.getBooks(i).getName())){
                System.out.println("找到了");
                System.out.println(bookList.getBooks(i));
                return;
            }
        }
        System.out.println("没有这本书");
    }
}

4.显示图书

package operation;

import book.Book;
import book.BookList;

public class ShowOperation implements Iop{
    @Override
    public void work(BookList bookList) {
        System.out.println("显示图书");
        int size = bookList.getNum();
        for (int i = 0; i < size; i++) {
            Book book = bookList.getBooks(i);
            System.out.println(book);
        }
    }
}

5.借阅图书

package operation;

import book.BookList;

import java.util.Scanner;

public class BorrowOperation implements Iop{
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅图书");
        System.out.println("请输入你要借的书:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int size = bookList.getNum();
        for (int i = 0; i <size ; i++) {
            if (name.equals(bookList.getBooks(i).getName())){
                bookList.getBooks(i).setIsborrow(true);
                System.out.println("借阅成功!");
                return;
            }
        }
        System.out.println("没有这本书");
    }
}

6.归还图书

package operation;

import book.BookList;

import java.util.Scanner;

public class BackOperation implements Iop{
    @Override
    public void work(BookList bookList) {
        System.out.println("归还图书 ");
        System.out.println("请输入你要还的书:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int size = bookList.getNum();
        for (int i = 0; i <size ; i++) {
            if ((name.equals(bookList.getBooks(i).getName())) && (bookList.getBooks(i).getisIsborrow())){
                bookList.getBooks(i).setIsborrow(false);
                System.out.println("还书成功!");
                return;
            }
        }
        System.out.println("你不需要还这本书");
    }
}

7.退出系统

package operation;

import book.Book;
import book.BookList;

public class ReturnOperation implements Iop{
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统");
        int size = bookList.getNum();
        for (int i = 0; i < size; i++) {
            bookList.setBooks(i , null);
        }
        bookList.setNum(0);
        System.exit(0);//退出系统
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值