图书管理系统

1. 面向对象整体思路

        1、找出其中的所有的对象(书,用户(普通用户和管理员),存放书的书架

        2、实现所有的对象

        3、完成对象之间的交互(单独封装成一个类的各种操作

        book包:实现书类和书架类

        operation包:实现我们的操作类

由于对象较多,所以需要各种用来交互的操作,故此定义IOperation接口,每个操作实现该接口然后重写里面的抽象方法,完成实例化操作类。

        user包:实现普通用户类与管理员类:

首先创建一个User父类,让NormalUser类(普通用户)和AdminUser(系统管理者)两个类来继承User父类的各种成员变量,和成员方法。具体情况如下图:

        接下来对每一给自定义类进行详细的解说。

2. Book包

2.1 Book类

        Book类主要是对book对象的成员变量(书名,作者,价格,类型,借阅状态)进行描述。

package book;

public class Book {
    private String name;
    private String author;
    private int price;
    private String type;
    private boolean isHere;

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

    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 isHere() {
        return isHere;
    }

    public void setisHere(boolean no) {
        isHere = no;
    }

    @Override
    public String toString() {
        return "[" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ", "+(isHere == false?"已被借出":"未被借出")+
                ']';
    }
}

2.2 BookShelf类

        我们不能把书直接放在书架上,要首先用一个book类的book【】数组来存放书籍,然后把这个数组放在书架bookshelf上。

package book;

import com.sun.xml.internal.ws.api.streaming.XMLStreamWriterFactory;

public class BookShelf {
    private Book[] books;
    private int bookNumber;
    private static final  int DEFAULT_CAPACITY = 48;

    public BookShelf() {
        this.books = new Book[DEFAULT_CAPACITY];
        this.books[0] = new Book("十日游戏","天猫精灵",30,"BE");
        this.books[1] = new Book("大怨种回忆录","杨_博宁",20,"BE");
        this.books[2] = new Book("喜宴","smallye",40,"BE");
        this.books[3] = new Book("人生解答书2022","怪人的倒影",40,"HE");

        this.bookNumber = 4;
    }

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

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

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

    public int getBookNumber() {
        return bookNumber;
    }

    public void setBookNumber(int bookNumber) {
        this.bookNumber = bookNumber;
    }
}

3.user包

3.1User类

        其中User类是作为NormalUser类与AdminUser类的父类,是一个抽象父类。

import book.BookShelf;
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 doOperation(int choice, BookShelf bookShelf) {
        ioPerations[choice].work(bookShelf);
    }
}

3.2 NormalUser类

4. operation

4.1 IOperation接口

package operation;

import book.BookShelf;

public interface IOperation {
    //给接口是指用户类和书架之间的抽象交互接口
    void work(BookShelf bookList);
}

 该接口会有一个work的抽象方法,其参数类型为BookShelf的,意味着该接口是通过对书架进行操作的一种工作规范。具体怎样操作,就看下面的具体实现该接口类来写怎样的具体work方法了。

4.2 ExitOperation类

        退出整个系统

package operation;

import book.BookShelf;

public class ExitOperation implements IOperation{
    @Override
    public void work(BookShelf bookList) {
        System.out.println("退出系统");
        System.exit(0);
    }
}

4.3FindOperation类

        用于查找图书

package operation;

import book.Book;
import book.BookShelf;

import java.util.Scanner;

public class FindOperation implements IOperation{
    @Override
    public void work(BookShelf bookShelf) {
        System.out.println("查找图书!");

        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        //遍历这个数组
        int currentSize = bookShelf.getBookNumber();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookShelf.getBook(i);
            if(book.getName().equals(name)) {
                System.out.println("找到了这本书,信息如下:");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有找到这本书!");
    }
}

4.4ShowOperation类

        用于展示书架上所有图书

package operation;

import book.Book;
import book.BookShelf;

public class ShowOperation implements IOperation{
    @Override
    public void work(BookShelf bookShelf) {
        System.out.println("打印图书!");
        int currentSize = bookShelf.getBookNumber();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookShelf.getBook(i);
            System.out.println(book);
        }
    }
}

 4.5AddOperation类

        用于添加图书

package operation;

import book.Book;
import book.BookShelf;

import java.util.Scanner;

public class AddOperation implements IOperation{
    @Override
    public void work(BookShelf bookShelf) {
        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 = bookShelf.getBookNumber();
        for (int i = 0; i < currentSize; i++) {
            Book book1 = bookShelf.getBook(i);
            if(book1.getName().equals(name)) {
                System.out.println("有这本书,不进行存放了!");
                return;
            }
        }
        if(currentSize == bookShelf.getBooks().length) {
            System.out.println("书架满了!");
        }else {
            bookShelf.setBooks(currentSize,book);
            bookShelf.setBookNumber(currentSize+1);
        }
    }
}

4.6DelOperation类

package operation;

import book.Book;
import book.BookShelf;

import java.util.Scanner;

public class DeleteOperation implements IOperation{
    @Override
    public void work(BookShelf bookShelf) {
        System.out.println("删除图书!");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要删除的图书:");
        String name = scanner.nextLine();

        int pos = -1;

        int currentSize = bookShelf.getBookNumber();
        int i = 0;
        for (; i < currentSize; i++) {
            Book book = bookShelf.getBook(i);
            if(book.getName().equals(name)) {
                pos = i;
                break;
            }
        }

        if(i == currentSize) {
            System.out.println("没有你要删除的图书!");
            return;
        }
        //开始删除
        int j = pos;
        for (; j < currentSize-1; j++) {
            //[j] = [j+1]
            Book book = bookShelf.getBook(j+1);

            bookShelf.setBooks(j,book);
        }

        bookShelf.setBooks(j,null);

        bookShelf.setBookNumber(currentSize-1);
    }
}

 4.7BorrowOperation类

package operation;

import book.Book;
import book.BookShelf;

import java.util.Scanner;

public class BorrowOperation implements IOperation{
    @Override
    public void work(BookShelf bookShelf) {
        System.out.println("借阅图书!");
        /**
         * 1. 你要借阅哪本书?
         * 2. 你借阅的书有没有?
         * 3. 借阅的方式是什么? -》 isB.... = true
         */
        Scanner scanner  = new Scanner(System.in);
        System.out.println("请输入你要借阅的图书:");
        String name = scanner.nextLine();

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

        System.out.println("你借阅的图书 不存在!! ");

    }
}

4.8 ReturnOperation类 

package operation;

import book.Book;
import book.BookShelf;

import java.util.Scanner;

public class ReturnOperation implements IOperation{
    @Override
    public void work(BookShelf bookShelf) {
        System.out.println("归还图书!");

        Scanner scanner  = new Scanner(System.in);
        System.out.println("请输入你要归还的图书:");
        String name = scanner.nextLine();

        int currentSize = bookShelf.getBookNumber();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookShelf.getBook(i);
            if(book.getName().equals(name)) {
                book.setisHere(true);
                System.out.println("归还成功!");
                System.out.println(book);
                return;
            }
        }
        System.out.println("你归还的图书 不存在!! ");
    }
}

5 、运行测试类

import java.util.Scanner;

import book.BookShelf;
import user.AdminUser;
import user.NormalUser;
import user.User;

public class Main {
    public static User login(){
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入你的姓名");
        String name=scanner.nextLine();
        System.out.println("请输入你的身份:> 1.管理员  2.普通用户");
        int choice=scanner.nextInt();
        if(choice==1){
            return new AdminUser(name);
        }else {
            return new NormalUser(name);
        }

    }

    public static void main(String[] args) {
        BookShelf bookShelf = new BookShelf();
        User user=login();
        int choice=-1;
        do{
            choice=user.menu();
            user.doOperation(choice,bookShelf);
        }while (choice!=0);
    }
}

 

简单运行结果 :

ps:本次学习就到此结束了,请大家一键三连。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值