当前位置:   article > 正文

面向对象实现图书管理系统_图书管理系统面向对象设计

图书管理系统面向对象设计

目录

一、功能需求分析:

管理员:

普通用户:

二、用到的方法及工具

三、具体实现

3.1、book包

3.2、User包

3.3、Operation包

3.4、测试类Main

四、效果展示:


一、功能需求分析:

管理员:

0.用户登录

1.退出系统:关闭JVM

2.查找图书:通过图书的名称查找图书,若有,展示查到的图书信息;若没有,返回未查到

3.新增图书:新增图书名称、作者、价格、类型

4.删除图书:通过图书名称删除图书

5.展示图书:将所有图书的数据输出

普通用户:

0.用户登录

1.退出系统:关闭JVM

2.展示图书:将所有图书的数据输出

3.借阅图书:通过图书名称借出图书,并将图书状态改为已借出

4.归还图书:通过图书名称归还图书,并将图书状态改为未借出

二、用到的方法及工具

方法:面向对象中的封装、继承、多态、接口、抽象,重写

工具:IDEA

三、具体实现

3.1、book包

 Book类是保存图书信息的实体类

  1. public class Book {
  2. private String name;
  3. private String author;
  4. private double price;
  5. private String type;
  6. private boolean isBorrow;
  7. public Book() {
  8. }
  9. public Book(String name, String author, double price, String type) {
  10. this.name = name;
  11. this.author = author;
  12. this.price = price;
  13. this.type = type;
  14. }
  15. public String getName() {
  16. return name;
  17. }
  18. public void setName(String name) {
  19. this.name = name;
  20. }
  21. public String getAuthor() {
  22. return author;
  23. }
  24. public void setAuthor(String author) {
  25. this.author = author;
  26. }
  27. public double getPrice() {
  28. return price;
  29. }
  30. public void setPrice(double price) {
  31. this.price = price;
  32. }
  33. public String getType() {
  34. return type;
  35. }
  36. public void setType(String type) {
  37. this.type = type;
  38. }
  39. public boolean isBorrow() {
  40. return isBorrow;
  41. }
  42. public void setBorrow(boolean borrow) {
  43. isBorrow = borrow;
  44. }
  45. @Override
  46. public boolean equals(Object o) {
  47. if (this == o) return true;
  48. if (o == null || getClass() != o.getClass()) return false;
  49. Book book = (Book) o;
  50. return Double.compare(book.price, price) == 0 &&
  51. isBorrow == book.isBorrow &&
  52. Objects.equals(name, book.name) &&
  53. Objects.equals(author, book.author) &&
  54. Objects.equals(type, book.type);
  55. }
  56. @Override
  57. public int hashCode() {
  58. return Objects.hash(name, author, price, type, isBorrow);
  59. }
  60. @Override
  61. public String toString() {
  62. return "Book{" +
  63. "name='" + name + '\'' +
  64. ", author='" + author + '\'' +
  65. ", price=" + price +
  66. ", type='" + type + '\'' +
  67. ", isBorrow=" +
  68. (isBorrow == true ? "已被借出" : "未被借出") +
  69. '}';
  70. }
  71. }

BookList是一个书架类,里面有两个属性,分别是书架的长度和书。

通过构造方法初始化三本书

注意:Book类型的属性的get和set方法需要自己按照需求指定

  1. public class BookList {
  2. private static final int DEFAULT_SIZE = 10;
  3. private Book[] book = new Book[DEFAULT_SIZE];
  4. private int bookSize; //记录当前book数组中有多少本书
  5. public BookList() {
  6. book[0] = new Book("红楼梦","曹雪芹", 20.3, "四大名著");
  7. book[1] = new Book("西游记","吴承恩",45.8,"四大名著");
  8. book[2] = new Book("水浒传","许恬芯",113.3,"四大名著");
  9. this.bookSize = 3;
  10. }
  11. public Book getBook(int pos) {
  12. return book[pos];
  13. }
  14. public void setBook(Book book) {
  15. this.book[bookSize] = book;
  16. }
  17. public void setBook(int index,Book book) {
  18. this.book[index] = book;
  19. }
  20. public int getBookSize() {
  21. return bookSize;
  22. }
  23. public void setBookSize(int bookSize) {
  24. this.bookSize = bookSize;
  25. }
  26. }

3.2、User包

User类是一个父类,被AdminUser和NomalUser继承,同时也是一个抽象类

  1. public abstract class User {
  2. protected String name;
  3. protected IOperation[] iOperations;
  4. public User(String name) {
  5. this.name = name;
  6. }
  7. public abstract int menu();
  8. public void doWork(int choice, BookList booklist) {
  9. this.iOperations[choice].work(booklist);
  10. }
  11. }

AdminUser类

  1. public class AdmainUser extends User{
  2. public AdmainUser(String name) {
  3. super(name);
  4. //父类和子类有同名属性或方法,要访问父类的时,super不能省略.所以这里可以用this
  5. this.iOperations = new IOperation[]{ //初始化,给他分配空间
  6. new ExitOperation(),
  7. new FindOperation(),
  8. new AddOperation(),
  9. new DeleteOperation(),
  10. new ShowOperation()
  11. };
  12. }
  13. public int menu() {
  14. System.out.println("************************");
  15. System.out.println("hello " + name + ",欢迎来到图书小练习");
  16. System.out.println("1.查找图书");
  17. System.out.println("2.新增图书");
  18. System.out.println("3.删除图书");
  19. System.out.println("4.显示图书");
  20. System.out.println("0.退出系统");
  21. System.out.println("************************");
  22. System.out.println("请输入你的操作:");
  23. Scanner sc = new Scanner(System.in);
  24. int choice = sc.nextInt();
  25. return choice;
  26. }
  27. }

NomalUser类

  1. public class NomalUser extends User{
  2. public NomalUser (String name) {
  3. super(name);
  4. this.iOperations = new IOperation[] {
  5. new ExitOperation(),
  6. new ShowOperation(),
  7. new BrrowOperation(),
  8. new ReturnOperation()
  9. };
  10. }
  11. public int menu() {
  12. System.out.println("************************");
  13. System.out.println("1.展示图书");
  14. System.out.println("2.借阅图书");
  15. System.out.println("3.归还图书");
  16. System.out.println("0.退出系统");
  17. System.out.println("************************");
  18. System.out.println("请输入你的操作:");
  19. Scanner sc = new Scanner(System.in);
  20. int choice = sc.nextInt();
  21. return choice;
  22. }
  23. }

3.3、Operation包

IOperation接口

  1. public interface IOperation {
  2. void work(BookList bookList);
  3. }

AddOperation

  1. package com.xtx.oper;
  2. import com.xtx.book.Book;
  3. import com.xtx.book.BookList;
  4. import java.util.Scanner;
  5. /**
  6. * @author 小许喵喵
  7. * @date 2023/6/14
  8. * @Description 新增图书
  9. */
  10. public class AddOperation implements IOperation{
  11. @Override
  12. public void work(BookList bookList) {
  13. System.out.println("请输入要新增的图书名称:");
  14. Scanner sc = new Scanner(System.in);
  15. System.out.println("请输入书名:");
  16. String name = sc.nextLine();
  17. System.out.println("请输入作者:");
  18. String author = sc.nextLine();
  19. System.out.println("请输入类型:");
  20. String type = sc.nextLine();
  21. System.out.println("请输入价格:");
  22. double price = sc.nextDouble();
  23. Book book = new Book(name,author,price,type);
  24. int currentBookSize = bookList.getBookSize();
  25. for (int i = 0; i < currentBookSize; i++) {
  26. Book books = bookList.getBook(i);
  27. if (name.equals(books.getName())) {
  28. System.out.println("已经存在这本书了,不可以重复添加!");
  29. return;
  30. }
  31. }
  32. bookList.setBook(book);
  33. bookList.setBookSize(currentBookSize + 1);
  34. System.out.println("新增成功,增添了:" + book);
  35. }
  36. }

BorrowOperation

  1. package com.xtx.oper;
  2. import com.xtx.book.Book;
  3. import com.xtx.book.BookList;
  4. import java.util.Scanner;
  5. /**
  6. * @author 小许喵喵
  7. * @date 2023/6/14
  8. * @Description
  9. */
  10. public class BrrowOperation implements IOperation{
  11. @Override
  12. public void work(BookList bookList) {
  13. System.out.println("请输入你要借阅的图书的名称:");
  14. Scanner sc = new Scanner(System.in);
  15. String name = sc.nextLine();
  16. int currentBookSize = bookList.getBookSize();
  17. for (int i = 0; i < currentBookSize; i++) {
  18. Book book = bookList.getBook(i);
  19. if (name.equals(book.getName()) && !book.isBorrow()) { //找到了这本书
  20. //修改书的状态
  21. book.setBorrow(true);
  22. System.out.println("借阅成功");
  23. return;
  24. }
  25. }
  26. System.out.println("没有你要找的书!");
  27. }
  28. }

DeleteOperation

  1. package com.xtx.oper;
  2. import com.xtx.book.Book;
  3. import com.xtx.book.BookList;
  4. import java.util.Scanner;
  5. /**
  6. * @author 小许喵喵
  7. * @date 2023/6/14
  8. * @Description 删除图书
  9. */
  10. public class DeleteOperation implements IOperation{
  11. @Override
  12. public void work(BookList bookList) {
  13. System.out.println("请输入你想删除的图书名称:");
  14. Scanner sc = new Scanner(System.in);
  15. String name = sc.nextLine();
  16. int currentBookSize = bookList.getBookSize();
  17. //判断有没有这个图书
  18. int index = -1;
  19. for (int i = 0; i < currentBookSize; i++) {
  20. Book book = bookList.getBook(i);
  21. if (name.equals(book.getName())) {
  22. index = i; //得到要删除的这个图书的下标
  23. break;
  24. }
  25. }
  26. //走到这说明我已经找到了这本书
  27. //现在要做的就是删除这本书,后面的覆盖前面的
  28. for (int j = 0; j < currentBookSize - 1; j++) {
  29. Book book = bookList.getBook(j + 1); //得到被删除对象的下一个对象
  30. bookList.setBook(j,book); //利用set重新赋值,把book给j位置
  31. }
  32. System.out.println("删除成功!");
  33. bookList.setBookSize(currentBookSize - 1);
  34. //将最后位置的对象置为NULL
  35. bookList.setBook(currentBookSize - 1,null);
  36. }
  37. }

ExitOperation

  1. public class ExitOperation implements IOperation{
  2. @Override
  3. public void work(BookList bookList) {
  4. System.out.println("退出系统");
  5. System.exit(0);
  6. }
  7. }

FindOperation

  1. package com.xtx.oper;
  2. import com.xtx.book.Book;
  3. import com.xtx.book.BookList;
  4. import java.util.Scanner;
  5. /**
  6. * @author 小许喵喵
  7. * @date 2023/6/14
  8. * @Description 查找图书
  9. */
  10. public class FindOperation implements IOperation{
  11. @Override
  12. public void work(BookList bookList) {
  13. System.out.println("请输入要查找的图书名称:");
  14. Scanner sc = new Scanner(System.in);
  15. String name = sc.nextLine();
  16. int currentBookSize = bookList.getBookSize();
  17. for (int i = 0; i < currentBookSize; i++) {
  18. Book book = bookList.getBook(i);
  19. if (name.equals(book.getName())) {
  20. System.out.println("找到了这本书:" + book);
  21. return;
  22. }
  23. }
  24. System.out.println("很遗憾,没有这本书。如有需要,请联系管理员添加");
  25. }
  26. }

ReturnOperation

  1. package com.xtx.oper;
  2. import com.xtx.book.Book;
  3. import com.xtx.book.BookList;
  4. import java.util.Scanner;
  5. /**
  6. * @author 小许喵喵
  7. * @date 2023/6/14
  8. * @Description 查找图书
  9. */
  10. public class FindOperation implements IOperation{
  11. @Override
  12. public void work(BookList bookList) {
  13. System.out.println("请输入要查找的图书名称:");
  14. Scanner sc = new Scanner(System.in);
  15. String name = sc.nextLine();
  16. int currentBookSize = bookList.getBookSize();
  17. for (int i = 0; i < currentBookSize; i++) {
  18. Book book = bookList.getBook(i);
  19. if (name.equals(book.getName())) {
  20. System.out.println("找到了这本书:" + book);
  21. return;
  22. }
  23. }
  24. System.out.println("很遗憾,没有这本书。如有需要,请联系管理员添加");
  25. }
  26. }

ShowOperation

  1. package com.xtx.oper;
  2. import com.xtx.book.Book;
  3. import com.xtx.book.BookList;
  4. /**
  5. * @author 小许喵喵
  6. * @date 2023/6/14
  7. * @Description 展示所有图书
  8. */
  9. public class ShowOperation implements IOperation{
  10. @Override
  11. public void work(BookList bookList) {
  12. System.out.println("打印所有图书:");
  13. int currentBookSize = bookList.getBookSize(); //获取当前图书的个数
  14. System.out.println(currentBookSize);
  15. for (int i = 0; i < currentBookSize ; i++) {
  16. Book book = bookList.getBook(i);
  17. System.out.println(book);
  18. }
  19. }
  20. }

3.4、测试类Main

  1. package com.xtx;
  2. import com.xtx.book.Book;
  3. import com.xtx.book.BookList;
  4. import com.xtx.user.AdmainUser;
  5. import com.xtx.user.NomalUser;
  6. import com.xtx.user.User;
  7. import java.util.Scanner;
  8. /**
  9. * @author 小许喵喵
  10. * @date 2023/6/14
  11. * @Description
  12. */
  13. public class Main {
  14. public static User login() {
  15. System.out.println("请输入你的姓名:");
  16. Scanner sc = new Scanner(System.in);
  17. String name = sc.nextLine();
  18. System.out.println("请输入你的身份: 1-》管理员 2-》普通用户");
  19. int choice = sc.nextInt();
  20. if (choice == 1) {
  21. return new AdmainUser(name);
  22. } else {
  23. return new NomalUser(name);
  24. }
  25. }
  26. public static void main(String[] args) {
  27. User user = login();
  28. BookList bookList = new BookList();
  29. while(true) {
  30. int choice = user.menu();
  31. user.doWork(choice,bookList);
  32. }
  33. }
  34. }

四、效果展示:

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/512234
推荐阅读
相关标签
  

闽ICP备14008679号