• 基于Java封装继承多态实现的一个简单图书系统


    首先我们大概了解下图书系统的需求

     1.要有两种身份 管理员和普通用户。普通用户和管理员分别对应的功能不一样,需要分开实现

    2. 图书系统肯定要有图书,和存放图书的地方,存放就用数组来实现

    3.实现对应用户的功能

    接下来我们第一步:

    首先创建一个book的包,里面存放图书类和存放图书的列表

    在图书类中我们要有图书的属性,我们这边简易版只有图书名,作者,类型,价格和图书状态(借出,未被借出)

    因为是自己使用,为了安全性使用private修饰,然后构造get,set方法来进行set值和get值,在构造一个toString方法,方便后面来打印,还有一个带参数的构造方法,方便后面进行新建图书

    接下来实现存放图书

    首先需要创建一个数组来存放书,还需要记录当前书的数量,然后我们可以给数组一个默认的长度,和初始化图书,在初始化时就有几本默认的书,我们需要一个通过下标获取到数组中的书

    图书的定义初始化和存放做完后,我们来实现用户

    用户分为管理员用户和普通用户,管理员用户和普通用户都是用户,我们就可以定义一个父类User类,然后字类AdminUser和NormalUser来继承父类中共有的方法

    父类中定义姓名,然后创建一个User的构造方法,doOperation方法后面会讲到

    现在我们要创建我们要实现功能的类,因为每个类都要使用work(BookList bookList)的方法,我们封装一个接口给其他功能类使用

    然后逐次创建,继承IOPeration接口,创建完后我们来实现主函数

    主函数里要定义一个登陆方法,然后返回用户给系统

     因为登陆函数返回了用户,我们主函数调用登陆方法,第一步知道是进入哪个用户,然后通过choice来决定调用的是哪个方法,doOperation这个方法在上面已经实现,传入choice和书的列表,choice则是通过在普通用户类和管理员用户类中的menu()方法,然后输入,通过choice来接收,传给了doOperation方法,根据choice选择对应的方法

    接下来我们挨个实现展示图书,查找图书,新增图书,归还图书,借阅图书,退出图书系统,删除图书的各项功能。

    展示图书:

    查找图书:

     新增图书:

     借阅图书:

     删除图书:

     退出系统:

    归还图书:

     

    接下来我给完整版代码:

    Book类: 

    1. package book;
    2. /**
    3. * @author xiaofan
    4. * @version 1.0
    5. * @date 2023/11/14 21:49
    6. */
    7. public class Book {
    8. private String name;
    9. private String author;
    10. private String type;
    11. private int price;
    12. private boolean isBorrowed;
    13. @Override
    14. public String toString() {
    15. return "Book{" +
    16. "name='" + name + '\'' +
    17. ", author='" + author + '\'' +
    18. ", type='" + type + '\'' +
    19. ", price=" + price +'\''+
    20. ((isBorrowed == true) ? "已经借出" : "未被借出")+
    21. '}';
    22. }
    23. public Book(String name, String author, String type, int price) {
    24. this.name = name;
    25. this.author = author;
    26. this.type = type;
    27. this.price = price;
    28. }
    29. public String getName() {
    30. return name;
    31. }
    32. public void setName(String name) {
    33. this.name = name;
    34. }
    35. public String getAuthor() {
    36. return author;
    37. }
    38. public void setAuthor(String author) {
    39. this.author = author;
    40. }
    41. public String getType() {
    42. return type;
    43. }
    44. public void setType(String type) {
    45. this.type = type;
    46. }
    47. public int getPrice() {
    48. return price;
    49. }
    50. public void setPrice(int price) {
    51. this.price = price;
    52. }
    53. public boolean isBorrowed() {
    54. return isBorrowed;
    55. }
    56. public void setBorrowed(boolean borrowed) {
    57. isBorrowed = borrowed;
    58. }
    59. }
    BookList类:
    1. package book;
    2. /**
    3. * @author xiaofan
    4. * @version 1.0
    5. * @date 2023/11/14 21:49
    6. */
    7. public class BookList {
    8. private Book[] books; //数组来存放书
    9. private int usedSize; //记录当前书架上 实际存放的书的数量
    10. //默认容量
    11. private static final int DEFAULT_CAPACITY = 10;
    12. public BookList(){
    13. this.books = new Book[DEFAULT_CAPACITY];
    14. //放好书
    15. this.books[0] = new Book("三国演义","罗贯中","小说",10);
    16. this.books[1] = new Book("西游记","吴承恩","小说",120);
    17. this.books[2] = new Book("红楼梦","曹雪芹","小说",110);
    18. this.books[3] = new Book("斗破苍穹","天蚕土豆","小说",20);
    19. this.usedSize = 4;
    20. }
    21. public int getUsedSize() {
    22. return usedSize;
    23. }
    24. public void setUsedSize(int usedSize) {
    25. this.usedSize = usedSize;
    26. }
    27. //获取数组某一个下标的书
    28. public Book getBook(int pos){
    29. // if (pos >= 0){
    30. // return books[pos];
    31. // } else {
    32. // System.out.println("下标错误");
    33. // //return new Exception();
    34. // //throw new Exception("下标错误"); //抛出异常
    35. // }
    36. return books[pos];
    37. }
    38. //通过数组的下标的位置来放书
    39. public void setBooks(int pos,Book book){
    40. books[pos] = book;
    41. }
    42. public Book[] getBooks() {
    43. return books;
    44. }
    45. }
    User类:
    1. package user;
    2. import book.BookList;
    3. import operation.IOPeration;
    4. /**
    5. * @author xiaofan
    6. * @version 1.0
    7. * @date 2023/11/14 22:12
    8. */
    9. public abstract class User {
    10. protected String name;
    11. //定义一个存放方法的数组
    12. protected IOPeration[] ioPerations;
    13. public User(String name){
    14. this.name = name;
    15. }
    16. public abstract int menu();
    17. //通过传入choice来判断调用的方法
    18. public void doOperation(int choice, BookList bookList) {
    19. ioPerations[choice].work(bookList);
    20. }
    21. }
    AdminUser类:
    1. package user;
    2. import operation.*;
    3. import java.util.Scanner;
    4. /**
    5. * @author xiaofan
    6. * @version 1.0
    7. * @date 2023/11/14 22:14
    8. */
    9. public class AdminUser extends User{
    10. public AdminUser(String name) {
    11. super(name);
    12. this.ioPerations = new IOPeration[]{
    13. new ExitOperation(),
    14. new FindOperation(),
    15. new AddOperation(),
    16. new DelOperation(),
    17. new ShowOperation()
    18. };
    19. }
    20. public int menu(){
    21. System.out.println("******管理员用户*******");
    22. System.out.println("1. 查找图书");
    23. System.out.println("2. 新增图书");
    24. System.out.println("3. 删除图书");
    25. System.out.println("4. 显示图书");
    26. System.out.println("0. 退出系统");
    27. System.out.println("*************");
    28. Scanner scanner = new Scanner(System.in);
    29. System.out.println("请输入你的操作");
    30. int choice = scanner.nextInt();
    31. return choice;
    32. }
    33. }

    NormalUser类:

    1. package user;
    2. import operation.*;
    3. import java.util.Scanner;
    4. /**
    5. * @author xiaofan
    6. * @version 1.0
    7. * @date 2023/11/14 22:14
    8. */
    9. public class NormalUser extends User {
    10. public NormalUser(String name) {
    11. super(name);
    12. this.ioPerations = new IOPeration[]{
    13. new ExitOperation(),
    14. new FindOperation(),
    15. new BorrowOperation(),
    16. new ReturnOperation()
    17. };
    18. }
    19. public int menu(){
    20. System.out.println("*****普通用户********");
    21. System.out.println("1. 查找图书");
    22. System.out.println("2. 借阅图书");
    23. System.out.println("3. 归还图书");
    24. System.out.println("0. 退出系统");
    25. System.out.println("*************");
    26. Scanner scanner = new Scanner(System.in);
    27. System.out.println("请输入你的操作");
    28. int choice = scanner.nextInt();
    29. return choice;
    30. }
    31. }

    主函数:

    1. import book.BookList;
    2. import user.AdminUser;
    3. import user.NormalUser;
    4. import user.User;
    5. import java.util.Scanner;
    6. /**
    7. * @author xiaofan
    8. * @version 1.0
    9. * @date 2023/11/14 23:18
    10. */
    11. public class Main {
    12. public static User login(){
    13. System.out.println("请输入用户名");
    14. Scanner scanner = new Scanner(System.in);
    15. String name = scanner.nextLine();
    16. System.out.println("请输入你的身份: 1 管理员 2 普通用户");
    17. int choice = scanner.nextInt();
    18. if (choice == 1){
    19. //管理员
    20. return new AdminUser(name);
    21. } else{
    22. //普通用户
    23. return new NormalUser(name);
    24. }
    25. }
    26. public static void main(String[] args) {
    27. BookList bookList = new BookList();
    28. //user 指向哪个对象,看返回值是什么?
    29. User user = login();
    30. while (true){
    31. int choice = user.menu();
    32. //System.out.println("choice: " + choice);
    33. //根据choice的选择来决定调用的是哪个方法
    34. user.doOperation(choice,bookList);
    35. }
    36. }
    37. }

    接口

    IOPeration:
    1. package operation;
    2. import book.BookList;
    3. /**
    4. * @author xiaofan
    5. * @version 1.0
    6. * @date 2023/11/14 22:06
    7. */
    8. public interface IOPeration {
    9. void work(BookList bookList);
    10. }

     

    功能实现类:

    AddOperation:

    1. package operation;
    2. import book.Book;
    3. import book.BookList;
    4. import java.util.Scanner;
    5. /**
    6. * @author xiaofan
    7. * @version 1.0
    8. * @date 2023/11/14 22:02
    9. */
    10. public class AddOperation implements IOPeration{
    11. @Override
    12. public void work(BookList bookList) {
    13. //System.out.println("新增图书!");
    14. Scanner scanner = new Scanner(System.in);
    15. System.out.println("请输入要添加的书名");
    16. String name = scanner.nextLine();
    17. System.out.println("请输书名的作者");
    18. String author = scanner.nextLine();
    19. System.out.println("请输入要添加书的类型");
    20. String type = scanner.nextLine();
    21. System.out.println("请输入书的价格");
    22. int price = scanner.nextInt();
    23. Book book = new Book(name,author,type,price);
    24. //检查数组当中有没有这本书
    25. int currentSize = bookList.getUsedSize();
    26. for (int i = 0; i < currentSize; i++) {
    27. Book book1 = bookList.getBook(i);
    28. if (book1.getName().equals(name)){
    29. System.out.println("有相同的书了,不进行存放");
    30. return;
    31. }
    32. }
    33. if (currentSize == bookList.getBooks().length){
    34. System.out.println("图书已经被放满,不能放了");
    35. } else {
    36. //将书放在下标的最后一个位置
    37. bookList.setBooks(currentSize,book);
    38. //将书的数量加1
    39. bookList.setUsedSize(currentSize+1);
    40. }
    41. System.out.println("新增成功");
    42. }
    43. }
    BorrowOperation:
    1. package operation;
    2. import book.Book;
    3. import book.BookList;
    4. import java.util.Scanner;
    5. /**
    6. * @author xiaofan
    7. * @version 1.0
    8. * @date 2023/11/14 22:04
    9. */
    10. public class BorrowOperation implements IOPeration{
    11. @Override
    12. public void work(BookList bookList) {
    13. //System.out.println("借阅图书!");
    14. Scanner scanner = new Scanner(System.in);
    15. System.out.println("请输入你要借阅的书名");
    16. String name = scanner.nextLine();
    17. int curredSize = bookList.getUsedSize();
    18. int i = 0;
    19. for (; i < curredSize; i++) {
    20. Book book = bookList.getBook(i);
    21. if (book.getName().equals(name)){
    22. if (book.isBorrowed() == false){
    23. book.setBorrowed(true);
    24. System.out.println("借阅成功");
    25. System.out.println(book);
    26. return;
    27. } else {
    28. System.out.println("图书已被借阅,不能再次借出");
    29. return;
    30. }
    31. }
    32. }
    33. System.out.println("你借阅的图书不存在");
    34. }
    35. }
    DelOperation:
    1. package operation;
    2. import book.Book;
    3. import book.BookList;
    4. import java.util.Scanner;
    5. /**
    6. * @author xiaofan
    7. * @version 1.0
    8. * @date 2023/11/14 22:02
    9. */
    10. public class DelOperation implements IOPeration {
    11. @Override
    12. public void work(BookList bookList) {
    13. //System.out.println("删除图书!");
    14. Scanner scanner = new Scanner(System.in);
    15. System.out.println("请输入要删除的书名");
    16. String name = scanner.nextLine();
    17. int pos = -1;
    18. int i = 0;
    19. int currentSize = bookList.getUsedSize();
    20. for (; i < currentSize; i++) {
    21. Book book = bookList.getBook(i);
    22. if (book.getName().equals(name)){
    23. pos = i;
    24. break;
    25. }
    26. }
    27. if (i == currentSize){
    28. System.out.println("没有你要删除的图书!");
    29. return;
    30. }
    31. //开始删除
    32. int j = pos;
    33. for (; j < currentSize-1 ; j++) {
    34. Book book = bookList.getBook(j+1);
    35. bookList.setBooks(j,book);
    36. }
    37. //将j置为空
    38. bookList.setBooks(j,null);
    39. //将书的数量减1
    40. bookList.setUsedSize(currentSize-1);
    41. System.out.println("删除成功");
    42. }
    43. }
    ExitOperation:
    1. package operation;
    2. import book.BookList;
    3. /**
    4. * @author xiaofan
    5. * @version 1.0
    6. * @date 2023/11/14 22:03
    7. */
    8. public class ExitOperation implements IOPeration{
    9. @Override
    10. public void work(BookList bookList) {
    11. System.out.println("退出图书系统!");
    12. System.exit(0);
    13. }
    14. }
    FindOperation:
    1. package operation;
    2. import book.Book;
    3. import book.BookList;
    4. import java.util.Scanner;
    5. /**
    6. * @author xiaofan
    7. * @version 1.0
    8. * @date 2023/11/14 22:02
    9. */
    10. public class FindOperation implements IOPeration{
    11. @Override
    12. public void work(BookList bookList) {
    13. //System.out.println("查找图书!");
    14. Scanner scanner = new Scanner(System.in);
    15. System.out.println("请输入你要查找的书!");
    16. String name = scanner.nextLine();
    17. int currentSize = bookList.getUsedSize();
    18. for (int i = 0; i < currentSize; i++) {
    19. Book book = bookList.getBook(i);
    20. if (book.getName().equals(name)){
    21. System.out.println("找到了这本书,信息如下");
    22. System.out.println(book);
    23. return;
    24. }
    25. }
    26. System.out.println("没有找到这本书");
    27. }
    28. }
    ReturnOperation:
    1. package operation;
    2. import book.Book;
    3. import book.BookList;
    4. import java.util.Scanner;
    5. /**
    6. * @author xiaofan
    7. * @version 1.0
    8. * @date 2023/11/14 22:04
    9. */
    10. public class ReturnOperation implements IOPeration{
    11. @Override
    12. public void work(BookList bookList) {
    13. //System.out.println("归还图书!");
    14. Scanner scanner = new Scanner(System.in);
    15. System.out.println("请输入你要归还的书名");
    16. String name = scanner.nextLine();
    17. int curredSize = bookList.getUsedSize();
    18. int i = 0;
    19. for (; i < curredSize; i++) {
    20. Book book = bookList.getBook(i);
    21. if (book.getName().equals(name)){
    22. if (book.isBorrowed() == true){
    23. book.setBorrowed(false);
    24. System.out.println("归还成功");
    25. System.out.println(book);
    26. return;
    27. } else {
    28. System.out.println("该图书没有被借出,无需归还");
    29. return;
    30. }
    31. }
    32. }
    33. System.out.println("你归还的图书不存在");
    34. }
    35. }
    ShowOperation:
    1. package operation;
    2. import book.Book;
    3. import book.BookList;
    4. /**
    5. * @author xiaofan
    6. * @version 1.0
    7. * @date 2023/11/14 22:03
    8. */
    9. public class ShowOperation implements IOPeration{
    10. @Override
    11. public void work(BookList bookList) {
    12. //System.out.println("展示图书!");
    13. int currentSize = bookList.getUsedSize();
    14. for (int i = 0; i < currentSize; i++) {
    15. Book book = bookList.getBook(i);
    16. System.out.println(book);
    17. }
    18. }
    19. }

    仓库地址:Java学习仓库,从入门到精通: Java入门学习 - Gitee.com

  • 相关阅读:
    什么是合规性测试?如何进行合规性测试?
    操作系统 OS
    基于java+SpringBoot+HTML+Mysq校园勤工助学平台
    数字化助力生产管理:报工与跟踪管理系统
    codesys虚轴
    ETL VS ELT
    flink笔记2(Flink 快速上手)
    C++面试经典题目汇总
    【Nginx】Nginx双机热备
    Python下载、安装及如何配置Pycharm(Windows 11)详细教程
  • 原文地址:https://blog.csdn.net/qq_61658398/article/details/134525535