Java implements library management system-1

Table of contents

Preface

1. Creative ideas

2. Build an overall logical framework

2.1 Definition of user class

2.2 Definition of books and bookshelf

2.3 Writing the main function

2.4 Functional interface implementation

2.5 Calling of functional interface

Summarize



Preface

This article is a practical process in the process of learning Java, which fully reflects the relationship between classes and objects in Java and the use of inheritance and polymorphism to solve problems! This article can be used as a small lesson plan, and students in need can pick it up for themselves! The source code is posted at the end of the article! !


1. Creative ideas

For a project that has just started, you must have a clear idea before you can write the project! Next, let’s analyze my thoughts:

1. For a library management system, there will be administrators and users respectively (the database is not implemented here, just a small summary of JavaSE). User classes will be generated here.

2. Determine the person to use it. Next is the issue of books. First, there needs to be a book class to describe a book, and then there needs to be a bookshelf class to store the books.

3. After the above two issues have been determined, the only problem is book management. Various book management classes need to be defined (add class, delete class, borrow class, return class, etc.)

This article is completely written from my first perspective: all the processes are clear! ! !

2. Build an overall logical framework

2.1 Definition of user class

First define a user class

public abstract class User {
    protected String username;
    public User(String username) {
        this.username = username;
    }
}

Then define an administrator class and an ordinary user class respectively to inherit the user class (as for why we need to inherit, we will talk about it later)

Ordinary user class:

public class NormalUser extends User{
    public NormalUser(String username) {
        super(username);
    }
}

Administrator class: 

public class AdminUser extends User{
    public AdminUser(String username) {
        super(username);
    }
}

After the user class is defined, we start defining the book class

2.2 Definition of books and bookshelf

Here I only define the book title, author, price, type, and whether to lend it out. Others can be added according to actual needs. Then generate the corresponding set and get methods and toString method

public class Book {
    private String bookName;
    private String author;
    private double price;
    private String type;
    private boolean isBorrow;
    
    //boolean初始值是false这里就不将其加入构造方法了
    public Book(String bookName, String author, double price, String type) {
        this.bookName = bookName;
        this.author = author;
        this.price = price;
        this.type = type;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getType() {
        return type;
    }

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

    public boolean isBorrow() {
        return isBorrow;
    }

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

    @Override
    public String toString() {
        return "Book{" +
                "bookName='" + bookName + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +","+
                ((isBorrow==true)?"已借出":"未借出") +
                '}';
    }
}

 After you have the books, the next step is the bookshelf to put the books on.

public class BookRack {
    private Book[] books = new Book[10];//书架最多存放的书,根据实际需求来
    private int bookCount;//目前书架的书的数量
    //初始化的时候书架就有三本书,根据自己的需求来
    public BookRack(){
        books[0] = new Book("三国演义","罗贯中",29.9,"小说");
        books[1] = new Book("西游记","吴承恩",39.9,"小说");
        books[2] = new Book("水浒传","施耐庵",19.9,"小说");
        this.bookCount = 3;//将书架的书变为三
    }
}

After writing this, you can basically start writing the main content.

2.3 Writing the main function

After writing the general framework, the next step is the entrance of the program. First, we need a login method.

public class Main {

    public static void login(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入姓名:");
        String name = sc.nextLine();
        System.out.println("请输入登录身份:1->管理员登录   0->用户登录");
        int choice = sc.nextInt();
        if(choice == 1){
            System.out.println(name + " 管理员,欢迎您登录图书管理系统!");
        }else{
            System.out.println(name + " 用户,欢迎您登录图书管理系统!");
        }
    }

    public static void main(String[] args) {
        login();
    }
}

Okay, I found a problem when I wrote this. I don’t know whether it is the administrator or the user who is logging in ! ! ! Here comes the key point! ! ! Can I use an object to receive it? Okay, here comes the question again. I will return two different objects here , one for a user and one for an administrator. So who should I return? Of course, it returns the parent class object (this is why inheritance is necessary) ! ! ! This problem can be perfectly solved with an upward promotion, just do it! Modify the above code immediately

public class Main {

    public static User login(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入姓名:");
        String name = sc.nextLine();
        System.out.println("请输入登录身份:1->管理员登录   0->用户登录");
        int choice = sc.nextInt();
        if(choice == 1){
            System.out.println(name + " 管理员,欢迎您登录图书管理系统!");
            //选管理员我就返回管理员对象
            return new AdminUser(name);
        }else{
            System.out.println(name + " 用户,欢迎您登录图书管理系统!");
            //选用户我就返回用户对象
            return new NormalUser(name);
        }
    }

    public static void main(String[] args) {
        User user = login();
    }
}

 After modifying the code, we now know which object is logged in. Then the next step is to implement the functional interface.

2.4 Functional interface implementation

There should be different functional interfaces for different users. I won’t go into details here. They will be based on actual needs.

Administrator function interface:

public class AdminUser extends User{

    public AdminUser(String username) {
        super(username);
    }

    @Override
    public int menu() {
        System.out.println("**************************************");
        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 sc = new Scanner(System.in);
        //我后面选了什么是不是要做一个记录?这样才能知道我选了哪个操作
        int choice = sc.nextInt();
        return choice;
    }
}

 User function interface:

public class NormalUser extends User{
    public NormalUser(String username) {
        super(username);
    }

    @Override
    public int menu() {
        System.out.println("**************************************");
        System.out.println("***           请输入您的操作:        ***");
        System.out.println("***           1.查找图书             ***");
        System.out.println("***           2.借阅图书             ***");
        System.out.println("***           3.归还图书             ***");
        System.out.println("***           0.退出系统             ***");
        System.out.println("**************************************");
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        return choice;
    }
}

 Then you need to call the method of the functional interface through the object

2.5 Calling of functional interface

Here, calling the subclass method through the parent class object is the idea of ​​polymorphism! ! !

 public static void main(String[] args) {
        User user = login();
        //只要我还没有退出程序我就需要进行操作选择,所以这里需要写一个循环
        while (true) {
            int choice = user.menu();
        }
    }

Okay, there is a problem after writing this (I was confused at the time)? Suddenly discovered! A WOC subclass cannot achieve polymorphic conditions without overriding the parent class method. My parent class does not have this method (if you don’t understand here, please take a look at the polymorphic article on my homepage)! ! ! Add it quickly

public abstract class User {
    protected String username;
    IOperation[] iOperations;
    public User(String username) {
        this.username = username;
    }
    //因为这个方法不需要实现什么内容,直接给他抽象了
    public abstract int menu();
}

 Well, writing this can be considered as completing the overall framework! But the problem comes again! I selected the number corresponding to the operation. How do I call the corresponding method (you can think about it actively)? That involves the specific operation content. If an article is too long, you definitely don’t want to read it! If you want to know what happened next time, please listen to the breakdown! ! ! Or jump directly to http://t.csdn.cn/lf0Ij


Summarize

This article mainly introduces the first half of the library management system (the construction of the overall logical framework), and shows the entire programming process from my first perspective. You can definitely understand it! If I don’t understand, slap me twice! ! !

Guess you like

Origin blog.csdn.net/x2656271356/article/details/129865031