一张图来实现简易图书系统(Java版)

图书系统之java版(一图流)

完整代码:

book包(书)

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

public class BookList {
    private Book[] books=new Book[10];
    private int usedSize;//有效的数据个数(实际放的书的个数)

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

        this.usedSize = 3;
    }

    public int getUsedSize() {
        return usedSize;
    }

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

    public Book[] getBooks() {
        return books;
    }
    public Book getBook(int pos){//查验书架有无插入的该本书
        return books[pos];
    }
    public void setBooks(int pos,Book books) {
        this.books[pos] = books;
    }
}

user包:

User

public abstract class User {
    public String name;
    public IOperation[] ioPerations;
//使用接口IOperation来定义数组ioPerations 该数组存储任何实现了该接口的对象的引用
    //可以通过该数组来统一调用这些对象的方法

    public abstract int menu();//抽象方法menu:用于搭建不同用户的框架

    public User(String name) {
        this.name = name;
    }
    public User(IOperation[] ioPerations) {
        this.ioPerations = ioPerations;
    }
//定义一个方法doIoperation 通过数组ioPerations和choice去调用对象中的work方法
    public void doIoperation(int choice, BookList bookList){
        ioPerations[choice].work(bookList);
    }
}
AdminUser

public class AdminUser extends User{
    public AdminUser(String name) {
        super(name);
        this.ioPerations=new IOperation[]{
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DelOperation(),
                new ShowOperation()
        };
    }
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("*****************");
    Scanner scanner=new Scanner(System.in);
    System.out.println("请输入你的操作:");
    int choice=scanner.nextInt();
    return choice;
  }
}
NormalUser

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("******普通用户菜单******");
    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);
    System.out.println("请输入你的操作:");
    int choice = scanner.nextInt();
    return choice;
  }
}

ioperations包:

接口IOperation

public interface IOperation {
    void work(BookList booklist);
}
AddOperation方法

public class AddOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("新增图书....");
        //1.判断书架上的书有无满
        int currentSize=bookList.getUsedSize();
        if(currentSize== bookList.getBooks().length){
            System.out.println("书架满了 不能放了...");
            return;
        }
        //2.构建对象
        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 newBook=new Book(name,author,price,type);

        //3.判断书架有无这本书
        for (int i=0;i<currentSize;i++){
            Book book=bookList.getBook(i);
            if(book.getName().equals(name)){
                System.out.println("有这本书不能插入!");
                return;
            }

            //4.插入这本书
            bookList.setBooks(currentSize,newBook);
            bookList.setUsedSize(currentSize+1);
            System.out.println("新增图书成功!!!");
        }
    }
}
BorrowOperation方法

public class BorrowOperation implements IOperation {
    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.getBook(i);

            if (book.getName().equals(name)) {
                if (book.isBorrowed()) {
                    System.out.println("这本书已经被借出了!");
                    return;
                }
                book.setBorrowed(true);
                System.out.println("借阅成功");
                return;
            }
        }
        System.out.println("没有你要借阅的这本书...");
    }
}
 DelOperation方法

public class DelOperation implements IOperation{
    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();

        int pos=-1;
        //找到要删除的这本书
        int i = 0;
        for (; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                pos = i;
                break;
            }
        }
        if(i==currentSize) {
            System.out.println("没有你要删除的书");
            return;
        }
            //删除(在数组中 删除:被前/后 一个覆盖
            for(int j=pos;j<currentSize;j++){
                //令下标为j+1的书覆盖下标为j的书
                Book book=bookList.getBook(j+1);
                bookList.setBooks(j,book);
            }
            bookList.setBooks(currentSize-1,null);
            bookList.setUsedSize(currentSize-1);
            System.out.println("删除成功");

    }
}
ExitOperation方法

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

public class FindOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("查找图书.....");
        Scanner scanner = new Scanner(System.in);
        System.out.printf("请输入你的书名:");
        String name = scanner.nextLine();

        int currentSize = bookList.getUsedSize();

        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                System.out.println("找到了这本书:");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有你要找的这本书....");
    }
}
ReturnOperation方法

public class ReturnOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("归还图书....");
        Scanner scanner = new Scanner(System.in);
        System.out.printf("请输入你归还的书名:");
        String name = scanner.nextLine();

        int currentSize = bookList.getUsedSize();

        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);

            if(book.getName().equals(name)) {
                if(book.isBorrowed()) {
                    book.setBorrowed(false);
                    System.out.println("归还成功!");
                    return;
                }
            }
        }
        System.out.println("没有你要归还的图书!!!");
    }
}
ShowOperation方法

public class ShowOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("显示图书....");
        int currentSize = bookList.getUsedSize();//3
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            System.out.println(book);
            //Book book1 = bookList[i];
        }
    }
}

测试代码:

public class Test {
    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();//1   2

        //问题:如果是1 管理员对象,如果是2 普通用户对象
        if(choice == 1) {
            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();
            //要根据这个返回值 来看 调用哪个对象的 哪个方法
            /**
             * 1. 哪个对象
             *   已经做了:User user = login();
             * 2. 哪个方法
             *  2.1 确定当前对象 已经包含了 这些方法?
             *    在调用子类对象的时候,构造方法会初始化好对应的操作对象
             *  2.2 怎么具体调用
             */
            user.doIoperation(choice,bookList);

            //user.ioPerations[choice].work(bookList);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值