iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > JAVA >图书管理借阅系统【Java简易版】Java三大特征封装,继承,多态的综合运用
  • 762
分享到

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

java开发语言数据库 2023-09-05 21:09:43 762人浏览 八月长安
摘要

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

前言
前几篇文章讲到了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;

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

   private int usedSize;

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

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

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

来源地址:https://blog.csdn.net/weixin_65476629/article/details/132167670

--结束END--

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

本文链接: https://www.lsjlt.com/news/396149.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作