图书管理借阅系统【Java简易版】Java三大特征封装,继承,多态的综合运用

简介: 图书管理借阅系统【Java简易版】Java三大特征封装,继承,多态的综合运用

前言

前几篇文章讲到了Java的基本语法规则,今天我们就用前面学到的数组,类和对象,封装,继承,多态,抽象类,接口等做一个图书管理借阅系统。

1.分析图书管理系统要实现的功能

Java语言是面向对象的,所以首先要分析完成这个图书管理系统,有哪些对象:

👱使用者User

📘书Book

📲操作Operation

使用者分为两种,普通用户(NormalUser)和图书管理员(AdminUser)对于普通用户来说,需要查找,借阅,归还图书操作。

对于图书管理员来说,需要查找,新增,删除,显示图书操作。

🥇2.在IDEA中创建对象

🥇3.实现book对象

🥉Book类

书有以下属性:

书名 String name

作者 String author

价格 int price

类型 String type

是否被借出 boolean isBorrow

  private String name;
    private String author;
    private int price;
    private String type;
    private boolean isBorrowed;

注意:这里的控制符全部设置为了私有的,需要提供方法来获取

提供get( )和set( )进行设置和获取

 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;
    }

提供一个构造方法

注意构造方法中不提供isBorrow,isBorrow是boolean类型,默认值为false,表示未被借出

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

提供ToString方法显示书的信息

  public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ", isBorrowed=" + isBorrowed +((isBorrowed==true)?"已被借阅":"未被借阅")+
                '}';
    }

完整的book类

package 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=" + isBorrowed +((isBorrowed==true)?"已被借阅":"未被借阅")+
                '}';
    }
}

🥉BookList类

用于存放book,相当于书架

设置一个book类型的数组存存放书

private Book[] books;
• 1

再设置一个成员变量记录书的数量

private int usedSize;
• 1

提供一个构造方法,进行一次初始化

 public BookList() {
        this.books =new Book[DEFAULT_CAPACITY];
        //初始化三本书
        this.books[0]=new Book("三国演义","罗贯中",10,"小说");
        this.books[1]=new Book("西游记","吴承恩",10,"小说");
        this.books[2]=new Book("红楼梦","曹雪芹",10,"小说");
        this.usedSize=3;
    }

再提供一个book类型的成员方法获取下标为pos的book

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

再提供一个方法,给一个数组下标和book,存放到bookList中


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

提供一个方法,获取书的数量


  public int getUsedSize() {
        return usedSize;
    }

提供一个方法,修改书的数量

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

提供一个方法,获取book数组


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

完整的BookList类

package book;
public class BookList {
    private Book[] books;
    private int usedSize;
    private static final int DEFAULT_CAPACITY = 10;
    public BookList() {
        this.books =new Book[DEFAULT_CAPACITY];
        //初始化三本书
        this.books[0]=new Book("三国演义","罗贯中",10,"小说");
        this.books[1]=new Book("西游记","吴承恩",10,"小说");
        this.books[2]=new Book("红楼梦","曹雪芹",10,"小说");
        this.usedSize=3;
    }
    public Book getBook(int pos) {
        return books[pos];
    }
    public void setBooks(int pos,Book book) {
        books[pos]=book;
    }
    public int getUsedSize() {
        return usedSize;
    }
    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }
    public Book[] getBooks() {
        return books;
    }
}

🥇4.功能

管理员或是普通用户,对书的操作都是在BookList类的数组books中进行操作,提供一个IOperation的接口,实现对数组的操作

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

创建各种类,来实现对书的所有操作,引用IOperation接口,对方法进行重写

举个例子:

public class AddOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书!");
    }
}

🥇5.面向用户

普通用户和图书管理员都是用户,需要进行操作,所以我们直接创建一个User类

package user;
import book.BookList;
import operation.IOperation;
public abstract class User {
    protected String name;
    protected IOperation[] iOperation;
    public User(String name) {
        this.name = name;
    }
    public abstract int menu();
    public void doOperation(int choice, BookList bookList) {
        iOperation[choice].work(bookList);
    }
}

🥉普通用户类

package user;
import operation.*;
import java.util.Scanner;
public class NormalUser extends User{
    public NormalUser(String name) {
        super(name);
        this.iOperation=new IOperation[]{
                new ExitOperation(),
                new FindOperation(),
                new BorrowOperation(),
                new ReturnOperation()
        };
    }
    @Override
    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;
    }
}

🥉图书管理员

package user;
import operation.*;
import java.util.Scanner;
public class AdminUser extends User{
    public AdminUser(String name) {
        super(name);
        this.iOperation=new IOperation[]{
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DelOperation(),
                new ShowOperation(),
        };
    }
    @Override
    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;
    }
}

🥉测试类

将所有类连接起来,使类之间相互交互

package user;
import book.BookList;
import java.util.Scanner;
import operation.*;
public class Main {
    public static User login(){
        System.out.println("请输入你的名字: ");
        Scanner scanner=new Scanner(System.in);
        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) {
        BookList bookList=new BookList();
        User user=login();
        while(true){
            int choice= user.menu();
            System.out.println("choice: "+choice);
            user.doOperation(choice,bookList);
        }
    }
}

运行结果

🥇5.实现操作功能

🥉新增图书

package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        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 curr=bookList.getUsedSize();
        for (int i = 0; i < curr; i++) {
            Book book1=bookList.getBook(i);
            if(book1.getName().equals(name)){
                System.out.println("该书已存在,无法存放!");
            } else if (curr==bookList.getBooks().length) {
                System.out.println("已经存放满,无法再存放!");
            }else {
                bookList.setBooks(curr,book);
                bookList.setUsedSize(curr+1);
            }
        }
    }
}

🥉删除图书

package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书!");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要删除的书名:");
        String name=scanner.nextLine();
        int curr= bookList.getUsedSize();
        int pos=-1;
        for (int i = 0; i < curr; i++) {
            Book book1=bookList.getBook(i);
            if(book1.getName().equals(name)) {
                pos = i;
                break;
            }
        }
        if (pos==-1){
            System.out.println("没有找到书名!");
        }else {
            int j=pos;
            for ( ; j < curr-1; j++) {
                Book book=bookList.getBook(j+1);
                bookList.setBooks(j,book);
            }
            bookList.setBooks(j,null);
            bookList.setUsedSize(curr-1);
            System.out.println("删除成功!");
        }
    }
}

🥉查找图书

package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("查找图书!");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要查找的书名:");
        String name=scanner.nextLine();
        int curr= bookList.getUsedSize();
        for (int i = 0; i < curr; i++) {
            Book book=bookList.getBook(i);
            if(book.getName().equals(name)){
                System.out.println("找到你想查找的书名:");
                System.out.println(book);
                return;
            }
        }
        System.out.println("你要查找的书籍不存在");
    }
}

🥉借阅图书

package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅图书!");
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入你要借阅的书名:");
        String name=scanner.nextLine();
        int curr=bookList.getUsedSize();
        for (int i = 0; i < curr; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                book.setBorrowed(true);
                System.out.println("借阅成功!");
                System.out.println(book);
                return;
            }
        }
        System.out.println("借阅的书籍不存在!");
    }
}

🥉归还图书

package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("归还图书!");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要归还的书名:");
        String name= scanner.nextLine();
        int curr= bookList.getUsedSize();
        for (int i = 0; i < curr; i++) {
            Book book=bookList.getBook(i);
            if(book.getName().equals(name)){
                book.setBorrowed(false);
                System.out.println("归还成功!");
                System.out.println(book);
                return;
            }
        }
        System.out.println("未查找到你要归还的书名!");
    }
}

🥉打印图书

package operation;
import book.BookList;
public class ShowOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("打印图书!");
        int curr= bookList.getUsedSize();
        for (int i = 0; i < curr; i++) {
            System.out.println(bookList.getBook(i));
        }
    }
}

🥉退出系统

package operation;
import book.BookList;
public class ExitOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("退出图书!");
        System.out.println("退出系统!");
        System.exit(0);
    }
}

写文至此,如有不同见解或者疑惑,欢迎在评论区留言!

目录
相关文章
|
7月前
|
人工智能 JSON Java
列表结构与树结构转换分析与工具类封装(java版)
本文介绍了将线性列表转换为树形结构的实现方法及工具类封装。核心思路是先获取所有根节点,将其余节点作为子节点,通过递归构建每个根节点的子节点。关键在于节点需包含 `id`、`parentId` 和 `children` 三个属性。文中提供了两种封装方式:一是基于基类 `BaseTree` 的通用工具类,二是使用函数式接口实现更灵活的方式。推荐使用后者,因其避免了继承限制,更具扩展性。代码示例中使用了 Jackson 库进行 JSON 格式化输出,便于结果展示。最后总结指出,理解原理是进一步优化和封装的基础。
196 0
|
8月前
|
安全 Java 开发者
【JAVA】封装多线程原理
Java 中的多线程封装旨在简化使用、提高安全性和增强可维护性。通过抽象和隐藏底层细节,提供简洁接口。常见封装方式包括基于 Runnable 和 Callable 接口的任务封装,以及线程池的封装。Runnable 适用于无返回值任务,Callable 支持有返回值任务。线程池(如 ExecutorService)则用于管理和复用线程,减少性能开销。示例代码展示了如何实现这些封装,使多线程编程更加高效和安全。
|
8月前
|
安全 网络协议 Java
Java网络编程封装
Java网络编程封装原理旨在隐藏底层通信细节,提供简洁、安全的高层接口。通过简化开发、提高安全性和增强可维护性,封装使开发者能更高效地进行网络应用开发。常见的封装层次包括套接字层(如Socket和ServerSocket类),以及更高层次的HTTP请求封装(如RestTemplate)。示例代码展示了如何使用RestTemplate简化HTTP请求的发送与处理,确保代码清晰易维护。
|
9月前
|
Java
Java 面向对象编程的三大法宝:封装、继承与多态
本文介绍了Java面向对象编程中的三大核心概念:封装、继承和多态。
464 15
|
11月前
|
Java
在Java中,接口之间可以继承吗?
接口继承是一种重要的机制,它允许一个接口从另一个或多个接口继承方法和常量。
884 60
|
12月前
|
Java
在Java多线程编程中,实现Runnable接口通常优于继承Thread类
【10月更文挑战第20天】在Java多线程编程中,实现Runnable接口通常优于继承Thread类。原因包括:1) Java只支持单继承,实现接口不受此限制;2) Runnable接口便于代码复用和线程池管理;3) 分离任务与线程,提高灵活性。因此,实现Runnable接口是更佳选择。
225 2
|
搜索推荐 Java 编译器
【Java探索之旅】多态:重写、动静态绑定
【Java探索之旅】多态:重写、动静态绑定
109 0
|
Java 程序员 C++
java面向对象编程_包_继承_多态_重载和重写_抽象类_接口_this和super(3)
java面向对象编程_包_继承_多态_重载和重写_抽象类_接口_this和super(3)
274 0
java面向对象编程_包_继承_多态_重载和重写_抽象类_接口_this和super(3)
|
Java 编译器
java面向对象编程_包_继承_多态_重载和重写_抽象类_接口_this和super(2)
java面向对象编程_包_继承_多态_重载和重写_抽象类_接口_this和super(2)
212 0
java面向对象编程_包_继承_多态_重载和重写_抽象类_接口_this和super(2)
|
Java 编译器 数据库
java面向对象编程_包_继承_多态_重载和重写_抽象类_接口_this和super(1)
java面向对象编程_包_继承_多态_重载和重写_抽象类_接口_this和super(1)
137 0
java面向对象编程_包_继承_多态_重载和重写_抽象类_接口_this和super(1)