Java: Book Management System with Command Line Interface

I believe everyone is familiar with the library management system. Borrowing books, returning books, etc.... Then how to use Java to achieve it?

First, we extract objects one by one.

  • Administrator 's operation:
  • View a book, add a book, delete a book, print a book list, and log out of the system.
  • Operation of ordinary users :
  • View the information of a certain book, borrow books, return books, and log out of the system.
  • Of course, in addition to the words marked in color above, the most important thing is to implement a book and book list .

Book

  • Attributes: book title, author, price, type, whether to be borrowed (use private as much as possible for packaging)
  • Methods: construction method (used to initialize), get the title of the book, get the loaned status, set the loaned status, toString
public class Book {
    private String name;
    private String author;
    private double price;
    private String type;
    private boolean isBorrowed=false;

    //构造方法,创建实例时初始化变量
    public Book(String name, String author, double price, String type) {
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
    }

    public String getName() {
        return name;
    }

    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 + '\'' +
                '}';
    }
}

BookList

  • Attributes: the length of the book list, the maximum length of the list, an array of Book type
  • Methods: construction method, get the length of the list, set the length of the list, get the book with the specified subscript, set the book with the specified subscript
//书籍列表
public class BookList {
    private int size;
    private int capacity=100;
    Book[] books=new Book[capacity];

    public BookList() {
        books[0]=new Book("西游记","吴承恩",50,"古典文学");
        books[1]=new Book("三国演义","罗贯中",60,"古典文学");
        books[2]=new Book("从你的全世界路过","张嘉佳",45,"青春文学");
        size=3;
    }

    public int getSize() {
        return size;
    }

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

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

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

We can find common parts from delete, add... operations, put them in an interface, and then directly implement this interface when defining the classes of these operations~

IOperation

Note: The methods in the interface are abstract methods~

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

AddOperation

After adding a book, set the length of the book list to +1

import java.util.Scanner;

public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("添加书籍!");
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入新增书籍的名称");
        String name=sc.next();
        System.out.println("请输入新增书籍的作者");
        String author=sc.next();
        System.out.println("请输入新增书籍的价格");
        double price=sc.nextDouble();
        System.out.println("请输入新增书籍的类型");
        String type=sc.next();
        Book book=new Book(name,author,price,type);
        bookList.setSize(bookList.getSize()+1);
        bookList.setBooks(bookList.getSize()-1,book);
        System.out.println("添加书籍成功!");
    }
}

BorrowOperation

Borrow by entering the book name to determine whether the book exists. If it does not exist, the prompt does not exist; if it does, determine whether it can be borrowed or borrowed: After borrowing, set its borrowing status to borrowed (true), not borrowing: Remind not to borrow~

import java.util.Scanner;

public class BorrowOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅书籍!");
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入您想借阅的书籍名称");
        String name=sc.next();
        int i=0;
        for(;i<bookList.getSize();i++){
            if(bookList.getBooks(i).getName().equals(name)){
                if(bookList.getBooks(i).isBorrowed()==false){
                    System.out.println("借阅成功!");
                    bookList.getBooks(i).setBorrowed(true);
                }else{
                    System.out.println("这本书已被借出啦,看看其他书吧~");
                }
                return;
            }
        }
        if(i>=bookList.getSize()){
            System.out.println("您所借阅的这本书不存在!");
        }
    }
}

DelOperation

Note: The length of the book list after deletion is -1

import java.util.Scanner;

public class DelOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        //删除书籍相关操作
        System.out.println("删除书籍!");
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入您想删除的书的书名");
        String name=sc.next();
        int i=0;
        for(;i<bookList.getSize();i++){
            if(name.equals(bookList.getBooks(i).getName())){
                if(i==bookList.getSize()-1){
                    bookList.setSize(bookList.getSize()-1);
                }else{
                    bookList.setBooks(i,bookList.getBooks(bookList.getSize()-1));
                    bookList.setSize(bookList.getSize()-1);
                }
                System.out.println("删除书籍成功!");
                break;
            }
        }
        if(i>=bookList.getSize()){
            System.out.println("您所删除的书籍不存在");
        }

    }
}

DisplayOperation

public class DisplayOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        //展示所有书籍信息
        System.out.println("查看所有书籍信息");
        for(int i=0;i<bookList.getSize();i++){
            System.out.println(bookList.getBooks(i));
        }
    }
}

ExitOperation

System.exit();

public class ExitOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        //退出程序
        System.out.println("退出图书管理系统!byebye~");
        System.exit(0);
    }
}

FindOperation

Use contains() method~

import java.util.Scanner;

public class FindOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
//        查找书籍相关信息
        System.out.println("查找书籍!");
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入您想查找书籍的名称");
        String name=sc.next();
        int i=0;
        for(;i<bookList.getSize();i++){
            if(bookList.getBooks(i).getName().contains(name)){
                System.out.println(bookList.getBooks(i));
                System.out.println("查找成功!");
                return;
            }
        }
        if(i>=bookList.getSize()){
            System.out.println("您所查找的书籍不存在");
        }
    }
}

ReturnOperation

import java.util.Scanner;

public class ReturnOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        //归还书籍
        System.out.println("归还书籍!");
        System.out.println("请输入要归还书籍的名称");
        Scanner sc=new Scanner(System.in);
        String name=sc.next();
        int i=0;
        for(;i<bookList.getSize();i++){
            if(bookList.getBooks(i).getName().equals(name)){
                bookList.getBooks(i).setBorrowed(true);
                System.out.println("归还成功!");
                return;
            }
        }
        if(i>=bookList.getSize()){
            System.out.println("归还失败!检查一下是否输入有误~");
        }
    }
}

Administrators and ordinary users have the same attributes and methods, extract them and put them in a class, and then directly inherit this class when creating a user.

User

abstract public class User {
    protected String name;
    protected IOperation[] operations;
    public abstract int menue();

    public void doOperation(int choice,BookList bookList){
        IOperation operation=this.operations[choice-1];
        operation.work(bookList);
//        operations[choice-1].work(bookList);
    }
}

Admin

import java.util.Scanner;

public class Admin extends User{
    public Admin(String name) {
        this.name = name;
        this.operations=new IOperation[] {
                new FindOperation(),
                new AddOperation(),
                new DelOperation(),
                new DisplayOperation(),
                new ExitOperation()
        };
    }
    @Override
    public int menue() {
        System.out.println("===================");
        System.out.println("欢迎"+name+"进入新一代智能图书管理系统");
        System.out.println("1.查阅书籍信息");
        System.out.println("2.添加新的书籍");
        System.out.println("3.删除书籍");
        System.out.println("4.查看所有书籍列表");
        System.out.println("5.退出系统");
        System.out.println("===================");
        System.out.println("请输入您的选择");
        Scanner sc=new Scanner(System.in);
        int choice=sc.nextInt();
        return choice;
    }
}

NormalUser

import java.util.Scanner;

public class NormalUser extends User{
    public NormalUser(String name) {
        this.name=name;
        this.operations=new IOperation[]{
                new FindOperation(),
                new BorrowOperation(),
                new ReturnOperation(),
                new ExitOperation()
        };
    }
    @Override
    public int menue() {
        System.out.println("===================");
        System.out.println("欢迎"+name+"进入新一代智能图书管理系统");
        System.out.println("1.查阅书籍信息");
        System.out.println("2.借阅书籍");
        System.out.println("3.归还书籍");
        System.out.println("4.退出系统");
        System.out.println("===================");
        System.out.println("请输入您的选择");
        Scanner sc=new Scanner(System.in);
        int choice=sc.nextInt();
        return choice;

    }
}

Main

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        //首先创建一个书籍列表
        BookList bookList=new BookList();
        //用户登录
        User user=login();
        //循环操作
        while(true){
            int choice=user.menue();
            user.doOperation(choice,bookList);
        }
    }

    public static User login(){
        System.out.println("请输入您的姓名");
        Scanner sc= new Scanner(System.in);
        String name=sc.next();
        System.out.println("请选择您的身份:1.管理员 2.普通用户");
        int choice=sc.nextInt();
        if(choice==1){
            return new Admin(name);  //向上转型
        }
        return new NormalUser(name);
    }
}

operation result

 

Guess you like

Origin blog.csdn.net/weixin_43939602/article/details/113267497