当前位置:   article > 正文

简易图书管理系统(面向对象思想)

简易图书管理系统

目录

前言

       1.整体思路

2.Book包

2.1Book类

2.2BookList类

3.user包

3.1User类

3.2NormalUser类

3.3AdminUser类

4.operation

4.1IOPeration接口

4.2ExitOperation类

4.3FindOperation类

4.4ShowOperation类

4.5AddOperation类

4.6DelOperation类

4.7BorrowOperation类

4.8ReturnOperation类

5.Text类


前言

图书管理系统充分展示面向对象思想,是类与对象,封装,继承,多态,抽象类,接口等知识的融合体现。

若有不了解的内容,可以查看本博主的曾写的相关内容。

面向对象程序三大特性一:封装(超详细)_WHabcwu的博客-CSDN博客

面向对象程序三大特性一:多态(超详细)_WHabcwu的博客-CSDN博客

面向对象程序三大特性一:多态(超详细)_WHabcwu的博客-CSDN博客

抽象类的顶级理解_WHabcwu的博客-CSDN博客

接口的顶级理解_WHabcwu的博客-CSDN博客

需求:登录的用户分为普通用户和管理员,实现界面目录的需求(均用姓名索引)。 

普通用户登录后界面:

管理员用户登录后界面:

图书管理系统已有图书如下:


 

1.整体思路

面向对象思想解决问题主要分为这3步:

  • 找出其中的所有的对象
  • 实现所有的对象
  • 完成对象之间的交互

在图书管理系统中我们可以想到这几个对象:书,用户(普通用户和管理员),存放书的书架。 

操作太多这里推荐新建将所有的操作都单独封装成一个类。

分为三个包进行实现:

book包里面实现书类和书架类

operation包里实现我们的操作类

user包里实现普通用户类与管理员类

其中所有的操作都实现名为IOperation的接口。便于统一管理(多态的体现)

普通用户和管理员都继承用户User这个父类。

实现所有类如下:

 


2.Book包

2.1Book类

在这个类实现的是我们图书该有的属性,与相应的构造方法,以及为了方便我们打印而重写的toString方法

  1. package book;
  2. public class Book {
  3. private String name;
  4. private String author;
  5. private int price;
  6. private String type;
  7. private boolean isBorrowed;
  8. public Book(String name, String author, int price, String type) {
  9. this.name = name;
  10. this.author = author;
  11. this.price = price;
  12. this.type = type;
  13. }
  14. public String getName() {
  15. return name;
  16. }
  17. public void setName(String name) {
  18. this.name = name;
  19. }
  20. public String getAuthor() {
  21. return author;
  22. }
  23. public void setAuthor(String author) {
  24. this.author = author;
  25. }
  26. public int getPrice() {
  27. return price;
  28. }
  29. public void setPrice(int price) {
  30. this.price = price;
  31. }
  32. public String getType() {
  33. return type;
  34. }
  35. public void setType(String type) {
  36. this.type = type;
  37. }
  38. public void setBorrowed(boolean borrowed) {
  39. isBorrowed = borrowed;
  40. }
  41. @Override
  42. public String toString() {
  43. return "{" +
  44. "name='" + name + '\'' +
  45. ", author='" + author + '\'' +
  46. ", price=" + price +
  47. ", type='" + type + '\'' +
  48. ", "+(isBorrowed==false?"未被借出":"已被借出")+
  49. '}';
  50. }
  51. }

2.2BookList类

我们需要存放很多Book的数组,操作的数组,加入图书管理系统已有图书。

  1. public class BookList {
  2. private Book books[]=new Book[100];
  3. private int useSize;
  4. public BookList() {
  5. this.books[0] = new Book("三国演义","罗贯中",10,"小说");
  6. this.books[1] = new Book("西游记","吴承恩",9,"小说");
  7. this.books[2] = new Book("红楼梦","曹雪芹",19,"小说");
  8. this.useSize = 3;
  9. }
  10. public int getUseSize() {
  11. return useSize;
  12. }
  13. public void setUseSize(int useSize) {
  14. this.useSize = useSize;
  15. }
  16. public Book getbook(int pose){
  17. return books[pose];
  18. }
  19. public void setbook(int pose,Book book){
  20. books[pose]=book;
  21. }
  22. public Book[] getBooks() {
  23. return books;
  24. }
  25. }

 

3.user包

3.1User类

这里的User类是作为NormalUser类与AdminUser类的父类,是一个抽象类

  1. package user;
  2. import book.BookList;
  3. import operation.IOperation;
  4. public abstract class User {
  5. protected String name;
  6. IOperation iOperations[];
  7. public User(String name) {
  8. this.name = name;
  9. }
  10. public abstract int menu();
  11. public void doOperation(int choice, BookList bookList){
  12. iOperations[choice].work(bookList);
  13. }
  14. }

3.2NormalUser类

  1. package user;
  2. import operation.*;
  3. import java.util.Scanner;
  4. public class NormalUser extends User {
  5. public NormalUser(String name) {
  6. super(name);
  7. this.iOperations = new IOperation[]{new ExitOperation(), new FindOperation(),
  8. new BorrowOperation(), new ReturnOperation(),new ShowOperation()};
  9. }
  10. @Override
  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("4.显示图书");
  17. System.out.println("0.退出系统");
  18. System.out.println("**************************");
  19. System.out.println("请输入你的操作");
  20. Scanner scanner = new Scanner(System.in);
  21. int choice = scanner.nextInt();
  22. return choice;
  23. }
  24. }

3.3AdminUser类

  1. package user;
  2. import operation.*;
  3. import java.util.Scanner;
  4. public class AdminUser extends User{
  5. public AdminUser(String name) {
  6. super(name);
  7. this.iOperations=new IOperation[]{new ExitOperation(),new FindOperation()
  8. ,new AddOperation(), new DelOperation(),new ShowOperation()};
  9. }
  10. public int menu(){
  11. System.out.println("**************************");
  12. System.out.println("1.查找图书");
  13. System.out.println("2.新增图书");
  14. System.out.println("3.删除图书");
  15. System.out.println("4.显示图书");
  16. System.out.println("0.退出系统");
  17. System.out.println("**************************");
  18. Scanner scanner =new Scanner(System.in);
  19. System.out.println("请输入你的操作");
  20. int choice=scanner.nextInt();
  21. return choice;
  22. }
  23. }

 

4.operation

4.1IOPeration接口

会有一个work的抽象方法,参数类型为BookList的

  1. package operation;
  2. import book.BookList;
  3. public interface IOperation {
  4. void work(BookList bookList);
  5. }

4.2ExitOperation类

用于退出该系统

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

4.3FindOperation类

用于查找图书

  1. package operation;
  2. import book.Book;
  3. import book.BookList;
  4. import java.util.Scanner;
  5. public class FindOperation implements IOperation{
  6. @Override
  7. public void work(BookList bookList) {
  8. int size=bookList.getUseSize();
  9. Scanner scanner = new Scanner(System.in);
  10. System.out.println("请输入你要查询的书名");
  11. String name=scanner.nextLine();
  12. for (int i = 0; i < size; i++) {
  13. Book book = bookList.getbook(i);
  14. if(book.getName().equals(name)){
  15. System.out.println("找到这本书,信息如下");
  16. System.out.println(book);
  17. return;
  18. }
  19. }
  20. System.out.println("查无此书");
  21. }
  22. }

4.4ShowOperation类

用于展示所有图书

  1. package operation;
  2. import book.Book;
  3. import book.BookList;
  4. public class ShowOperation implements IOperation {
  5. @Override
  6. public void work(BookList bookList) {
  7. int size=bookList.getUseSize();
  8. for (int i = 0; i < size; i++) {
  9. Book book = bookList.getbook(i);
  10. System.out.println(book);
  11. }
  12. }
  13. }

4.5AddOperation类

用于添加图书

  1. package operation;
  2. import book.Book;
  3. import book.BookList;
  4. import java.util.Scanner;
  5. public class AddOperation implements IOperation {
  6. @Override
  7. public void work(BookList bookList) {
  8. int size=bookList.getUseSize();
  9. if(size==bookList.getBooks().length){
  10. System.out.println("书架已满,无法添加图书");
  11. return;
  12. }
  13. Scanner scanner=new Scanner(System.in);
  14. System.out.println("请输入添加书的书名");
  15. String name=scanner.nextLine();
  16. System.out.println("请输入添加书的作者");
  17. String author=scanner.nextLine();
  18. System.out.println("请输入添加书的类型");
  19. String type=scanner.nextLine();
  20. System.out.println("请输入添加书的价格");
  21. int price=scanner.nextInt();
  22. Book book = new Book(name, author, price, type);
  23. for (int i = 0; i < size; i++) {
  24. if(bookList.getbook(i).getName().equals(name)){
  25. System.out.println("该书已有,不再存入");
  26. return;
  27. }
  28. }
  29. bookList.setbook(size,book);
  30. bookList.setUseSize(size+1);
  31. }
  32. }

4.6DelOperation类

用于删除图书

  1. package operation;
  2. import book.BookList;
  3. import java.util.Scanner;
  4. public class DelOperation implements IOperation{
  5. @Override
  6. public void work(BookList bookList) {
  7. Scanner scanner=new Scanner(System.in);
  8. System.out.println("请输入要删除书的书名");
  9. String name=scanner.nextLine();
  10. int size = bookList.getUseSize();
  11. int i=0;
  12. for (i = 0; i < size; i++) {
  13. if(bookList.getbook(i).getName().equals(name)){
  14. break;
  15. }
  16. }
  17. if(i==size){
  18. System.out.println("你要删除的书不存在");
  19. return;
  20. }
  21. int j=0;
  22. for ( j = i; j <size-1; j++) {
  23. bookList.setbook(j,bookList.getbook(j+1));
  24. }
  25. bookList.setbook(j,null);
  26. bookList.setUseSize(size-1);
  27. System.out.println("删除成功");
  28. }
  29. }

4.7BorrowOperation类

  1. package operation;
  2. import book.BookList;
  3. import java.util.Scanner;
  4. public class BorrowOperation implements IOperation{
  5. @Override
  6. public void work(BookList bookList) {
  7. Scanner scanner=new Scanner(System.in);
  8. System.out.println("请输入要借阅书的书名");
  9. int size=bookList.getUseSize();
  10. String name=scanner.nextLine();
  11. for (int i = 0; i < size; i++) {
  12. if(bookList.getbook(i).getName().equals(name)){
  13. bookList.getbook(i).setBorrowed(true);
  14. System.out.println("成功借出");
  15. return;
  16. }
  17. }
  18. System.out.println("你要借的书不存在");
  19. }
  20. }

4.8ReturnOperation类

  1. package operation;
  2. import book.BookList;
  3. import java.util.Scanner;
  4. public class ReturnOperation implements IOperation{
  5. @Override
  6. public void work(BookList bookList) {
  7. Scanner scanner=new Scanner(System.in);
  8. System.out.println("请输入要还的书的书名");
  9. int size=bookList.getUseSize();
  10. String name=scanner.nextLine();
  11. for (int i = 0; i < size; i++) {
  12. if(bookList.getbook(i).getName().equals(name)){
  13. bookList.getbook(i).setBorrowed(false);
  14. System.out.println("成功归还");
  15. return;
  16. }
  17. }
  18. System.out.println("你要还的书不存在");
  19. }
  20. }

5.Text类

  1. import java.util.Scanner;
  2. import book.BookList;
  3. import user.AdminUser;
  4. import user.NormalUser;
  5. import user.User;
  6. public class Text {
  7. public static User login(){
  8. Scanner scanner=new Scanner(System.in);
  9. System.out.println("请输入你的姓名");
  10. String name=scanner.nextLine();
  11. System.out.println("请输入你的身份:> 1.管理员 2.普通用户");
  12. int choice=scanner.nextInt();
  13. if(choice==1){
  14. return new AdminUser(name);
  15. }else {
  16. return new NormalUser(name);
  17. }
  18. }
  19. public static void main(String[] args) {
  20. BookList bookList = new BookList();
  21. User user=login();
  22. int choice=-1;
  23. do{
  24. choice=user.menu();
  25. user.doOperation(choice,bookList);
  26. }while (choice!=0);
  27. }
  28. }

以上为我个人的小分享,如有问题,欢迎讨论!!! 

都看到这了,不如关注一下,给个免费的赞 

 

 

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

闽ICP备14008679号