JAVA realizes the library management system (ideas, and complete code)

Because there are too many files, the relationship between each file is as follows ( there is only one class in each file ):

Because JAVA is an object-oriented programming language, we need to take the following steps to implement a library management system:

  • find all objects in
  • implement all objects
  • Complete the interaction between objects

In the library management system, we can think of these objects : books , users (users can also be divided into ordinary users and administrators) , and there must be bookshelves for storing books .

After finding the objects we put them in two packages ( one for books and bookshelves, the other for normal users and administrators ).

We can create an array of books in the bookshelf class, which is equivalent to putting books in the bookshelf.

package book;

public class BookList {
    public Book[] books;//书架
    public int numBooks;//书架实际存放的书的本书
}

At this time, you can also modify the properties in the book class with private, because all our operations will be aimed at the bookshelf (if you use private modification, you need to implement get and set methods for each property).

The Book class contains the following properties:

String name;

String author;

int money;

String type;

boolean isBorrow;

The following methods:

public Book(){};
public Book(String name, String author, int money, String kind) {}
public String getName() {}

public void setName(String name) {}

public String getAuthor() {}

public void setAuthor(String author) {}

public int getMoney() {}

public void setMoney(int money) {}

public String getType() {}

public void setType(String kind) {}

public boolean getBorrow() {}

public void setBorrow(boolean borrow) {}

@Override
public String toString() {
    return  "name='" + name + '\'' +
            ", author='" + author + '\'' +
            ", money=" + money +
            ", kind='" + type + '\'' +
            ",Status: "+(isBorrow == false ? "Not Borrowed" : "Borrowed"); 
}

ToString is rewritten here so that System.out.println() can be used directly; output book

Because ordinary users and administrators belong to users, inheritance can be used to allow  ordinary users and administrators to inherit the parent class of users .

Administrator privileges: Ordinary user privileges:

                                                 

 

In this way, we can use the idea of ​​polymorphism to create a user class, and the results of calling the same method are different due to different referenced objects.

But now there is a new problem that the menus printed by administrators and ordinary users are different, so they cannot be implemented in the parent class, so the parent class cannot be an ordinary parent class, it must be an abstract class .

Because there are too many user operations and there are repeated methods, it is recommended to create a new package and put them all into a new package, and encapsulate all operations separately into a class.

The operation belongs to the user so it is written in the following format:

//用户类
abstract public class User {
    protected String name;
    protected Work[] work;//存放用户所有的操作
    public int counst; //work中的元素个数
    
}
//管理员
public class AdvancedUser extends User{

    public AdvancedUser(String name){
        this.name = name;
        this.work = new Work[]{
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DelOperation(),
                new ShowOperation()
        };
        this.counst = 5;
    }

}
//普通用户
public class NormalUser extends User {

    public NormalUser(String name){
        this.name = name;
        this.work = new Work[]{
                new ExitOperation(),
                new FindOperation(),
                new BorrowOperation(),
                new RetOperation(),
        };
        this.counst = 4;
    }
    
}

 This way you can use the array to call the method

public void operation(int choice, BookList bookList){

        work[choice].work(bookList);
    }

Full code:

package main;

import book.BookList;
import user.AdvancedUser;
import user.NormalUser;
import user.User;

import java.util.Scanner;

public class Main {

    public static User login(){
        Scanner in = new Scanner(System.in);
        System.out.println("请输入你的姓名:");
        String name = in.nextLine();
        System.out.println("请选择你的身份:1.管理员    0.普通用户");
        if (in.nextInt() == 1){
            return new AdvancedUser(name);
        }else {
            return new NormalUser(name);
        }
    }

    public static void main(String[] args) {
        BookList bookList = new BookList();
        User user = login();
        while(true){
            int choice;
            do{
                choice = user.menu();
            }while(choice < 0 || choice >= user.counst);

            user.operation(choice, bookList);
        }
    }
}
package book;

public class Book {
    private String name;
    private String author;
    private int money;
    private String type;
    private boolean isBorrow;

    public Book(){};
    public Book(String name, String author, int money, String kind) {
        this.name = name;
        this.author = author;
        this.money = money;
        this.type = kind;
    }

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

    public void setMoney(int money) {
        this.money = money;
    }

    public String getType() {
        return type;
    }

    public void setType(String kind) {
        this.type = kind;
    }

    public boolean getBorrow() {
        return isBorrow;
    }

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

    @Override
    public String toString() {
        return  "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", money=" + money +
                ", kind='" + type + '\'' +
                ", 状态:"+(isBorrow == false ? "未被借出" : "已被借出");
    }
}
package book;

public class BookList {
    public Book[] books;//书架
    public int numBooks;//书架实际存放的书的本书

    public BookList() {
        this.books = new Book[10];
        //默认书架存放两本书
        this.books[0] = new Book("西游记","吴承恩",39,"小说");
        this.books[1] = new Book("红楼梦","曹雪芹",39,"小说");
        this.numBooks = 2;
    }


}

 

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation extends Work{

    @Override
    public void work(BookList bookList) {
        Scanner in = new Scanner(System.in);
        bookList.books[bookList.numBooks] = new Book();
        System.out.print("请输入书名:");
        bookList.books[bookList.numBooks].setName(in.nextLine());
        System.out.print("请输入作者:");
        bookList.books[bookList.numBooks].setAuthor(in.nextLine());
        System.out.print("请输入所属书类:");
        bookList.books[bookList.numBooks].setType(in.nextLine());
        System.out.print("请输入书价:");
        bookList.books[bookList.numBooks].setMoney(in.nextInt());
        bookList.numBooks++;
    }
}

 

package operation;

import book.BookList;

import java.util.Scanner;

public class BorrowOperation extends Work{

    @Override
    public void work(BookList bookList) {
        int numBooks = bookList.numBooks;

        Scanner in = new Scanner(System.in);
        System.out.print("请输入书名:");
        String name = in.nextLine();

        for (int i = 0; i < numBooks; i++) {
            if (name.equals(bookList.books[i].getName())) {
                if (bookList.books[i].getBorrow() == false){
                    System.out.println("借阅成功");
                    bookList.books[i].setBorrow(true);
                    return;
                }else{
                    System.out.println("该书已被借出");
                    return;
                }
            }
        }
        System.out.println("未收录该书");
    }
}
package operation;

import book.BookList;

import java.util.Scanner;

public class DelOperation extends Work{

    @Override
    public void work(BookList bookList) {
        int numBooks = bookList.numBooks;

        Scanner in = new Scanner(System.in);
        System.out.print("请输入书名:");
        String name = in.nextLine();

        for (int i = 0; i < numBooks; i++) {
            if (name.equals(bookList.books[i].getName())){
                int j = 0;
                for (j = i; j < numBooks-1; j++) {
                    bookList.books[j] = bookList.books[j + 1];
                }
                bookList.books[j] = null;
                bookList.numBooks--;
                return;
            }
        }
    }
}
package operation;

import book.BookList;

public class ExitOperation extends Work{

    @Override
    public void work(BookList bookList) {
        System.exit(0);
    }
}

 

package operation;

import book.BookList;

import java.util.Scanner;

public class FindOperation extends Work{

    @Override
    public void work(BookList bookList) {
        int numBooks = bookList.numBooks;

        Scanner in = new Scanner(System.in);
        System.out.print("请输入书名:");
        String name = in.nextLine();

        for (int i = 0; i < numBooks; i++) {
            if (name.equals(bookList.books[i].getName())) {
                System.out.println(bookList.books[i]);
                return;
            }
        }
        System.out.println("未收录该书");
    }
}
package operation;

import book.BookList;

import java.util.Scanner;

public class RetOperation extends Work{

    @Override
    public void work(BookList bookList) {
        int numBooks = bookList.numBooks;

        Scanner in = new Scanner(System.in);
        System.out.print("请输入书名:");
        String name = in.nextLine();

        for (int i = 0; i < numBooks; i++) {
            if (name.equals(bookList.books[i].getName())) {
                if (bookList.books[i].getBorrow()){
                    System.out.println("归还成功");
                    bookList.books[i].setBorrow(false);
                    return;
                }else{
                    System.out.println("该书未被借出");
                    return;
                }
            }
        }
        System.out.println("未收录该书");
    }
}
package operation;

import book.BookList;

public class ShowOperation extends Work{

    @Override
    public void work(BookList bookList) {
        for (int i = 0; i < bookList.numBooks; i++) {
            System.out.println(bookList.books[i]);
        }
    }
}
package operation;

import book.BookList;

abstract public class Work {
    abstract public void work(BookList bookList);
}
package user;

import operation.*;

import java.util.Scanner;

public class AdvancedUser extends User{

    public AdvancedUser(String name){
        this.name = name;
        this.work = new Work[]{
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DelOperation(),
                new ShowOperation()
        };
        this.counst = 5;
    }

    @Override
    public int menu() {
        System.out.println("###########################");
        System.out.println(" Hello " + "AdvancedUser:" + this.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 in = new Scanner(System.in);
        return in.nextInt();
    }
}

 

package user;

import operation.*;

import java.util.Scanner;

public class NormalUser extends User {

    public NormalUser(String name){
        this.name = name;
        this.work = new Work[]{
                new ExitOperation(),
                new FindOperation(),
                new BorrowOperation(),
                new RetOperation(),
        };
        this.counst = 4;
    }
    @Override
    public int menu() {
        System.out.println("###########################");
        System.out.println(" Hello " + "NormalUser:" + this.name);
        System.out.println("1.查找图书");
        System.out.println("2.借阅图书");
        System.out.println("3.归还图书");
        System.out.println("0.退出系统");
        System.out.println("###########################");
        System.out.println("请选择你的操作:");
        Scanner in = new Scanner(System.in);
        return in.nextInt();
    }
}
package user;

import book.BookList;
import operation.Work;

abstract public class User {
    protected String name;
    protected Work[] work;//存放用户所有的操作
    public int counst; //work中的元素个数
    abstract public int menu();

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

operation result:

 

Guess you like

Origin blog.csdn.net/2302_76339343/article/details/132167792