当前位置:   article > 正文

图书管理系统(面向对象)_图书管理系统面向对象分析

图书管理系统面向对象分析

图书管理系统是对抽象类、接口、继承、封装等知识点的应用,也是对学习的知识的总结。

对于这个图书管理系统我们大致分为三个步骤进行编写。

(源码我放在最后)

一、对于图书相关类的创建和使用。

我们先先创建一个包book

1.1在包中我们先创建Book类来表示一本书

  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 =false;
  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() {return name;}
  15. public void setName(String name) {this.name = name;}
  16. public String getAuthor() {return author;}
  17. public void setAuthor(String author) {this.author = author;}
  18. public int getPrice() {return price;}
  19. public void setPrice(int price) {this.price = price;}
  20. public String getType() {return type;}
  21. public void setType(String type) {this.type = type;}
  22. public boolean isBorrowed() {return isBorrowed;}
  23. public void setBorrowed(boolean borrowed) {isBorrowed = borrowed;}
  24. @Override
  25. public String toString() {
  26. return "Book{" +
  27. "name='" + name + '\'' +
  28. ", author='" + author + '\'' +
  29. ", price=" + price +
  30. ", type='" + type + '\'' +
  31. ", isBorrowed=" + isBorrowed +
  32. '}';
  33. }
  34. }

在这个类当中由于我们对于一本书中的类容进行了编写,由于为都为私有,我们设置了它的get和set方法,同时我们也重写了toString方法,这样一本书就编写好了。

1.2创建BookList类用来保存N本书

  1. package book;
  2. public class BookList {
  3. private Book[] books=new Book[10];
  4. private int size=0;
  5. public BookList(){
  6. books[0] = new Book("三国演义", "罗贯中", 100, "小说");
  7. books[1] = new Book("水浒传", "施耐庵", 100, "小说");
  8. books[2] = new Book("西游记", "吴承恩", 100, "小说");
  9. this.size=3;
  10. }
  11. public Book[] getBooks() {return books;}
  12. public void setBooks(Book[] books) {this.books = books;}
  13. public int getSize() {return size;}
  14. public void setSize(int size) {this.size = size;}
  15. }

在初始化的时候我们定义三本书放进去,并将它4们的get和set方法设置出来。

二、创建操作相关的类

我们将相关类都放在operation包中

我们先创建一个接口

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

我们在接口中添加了一个方法,去遍历很多本书,通过继承让其他类继承这个接口,从而实现work方法。

  1. public class AddOperation implements IOperation{
  2. @Override
  3. public void work(BookList bookList) {
  4. }
  5. }
  1. public class DelOperation implements IOperation{
  2. @Override
  3. public void work(BookList bookList) {
  4. }
  5. }
  1. public class FindOperation implements IOperation{
  2. @Override
  3. public void work(BookList bookList) {
  4. }
  5. }
  1. public class RemoveOperation implements IOperation{
  2. @Override
  3. public void work(BookList bookList) {
  4. }
  5. }
  1. public class DisplayOperation implements IOperation{
  2. @Override
  3. public void work(BookList bookList) {
  4. }
  5. }
  1. public class BorrowOperation implements IOperation{
  2. @Override
  3. public void work(BookList bookList) {
  4. }
  5. }
  1. public class ReturnOperation implements IOperation{
  2. @Override
  3. public void work(BookList bookList) {
  4. }
  5. }
  1. public class ExitOperation implements IOperation{
  2. @Override
  3. public void work(BookList bookList) {
  4. }
  5. }

我们创建了不同的方法通过调用接口来实现方法。

三、创建用户相关的类

创建一个user包

  1. package uesr;
  2. import operation.IOperation;
  3. public abstract class User {
  4. protected String name;
  5. protected IOperation[] operations;
  6. //显示菜单
  7. abstract public int menu();
  8. }

创建普通用户类

  1. package uesr;
  2. import operation.*;
  3. import java.util.Scanner;
  4. public class NormalUser extends User{
  5. public NormalUser(String name){
  6. this.name=name;
  7. this.operations=new IOperation[]{
  8. new ExitOperation(),
  9. new FindOperation(),
  10. new BorrowOperation(),
  11. new ReturnOperation()
  12. };
  13. }
  14. @Override
  15. public int menu() {
  16. System.out.println("=============");
  17. System.out.println("欢迎用户使用图书系统");
  18. System.out.println("1、查找图书");
  19. System.out.println("2、借阅图书");
  20. System.out.println("3、归还图书");
  21. System.out.println("0、退出系统");
  22. System.out.println("=============");
  23. System.out.println("输入你的选择");
  24. Scanner scanner=new Scanner(System.in);
  25. int choice=scanner.nextInt();
  26. return choice;
  27. }
  28. }

创建管理员类

  1. package uesr;
  2. import operation.*;
  3. import java.util.Scanner;
  4. public class AdminUser extends User{
  5. public AdminUser(String name){
  6. this.name=name;
  7. this.operations=new IOperation[]{
  8. new ExitOperation(),
  9. new FindOperation(),
  10. new AddOperation(),
  11. new DelOperation(),
  12. new DisplayOperation()
  13. };
  14. }
  15. @Override
  16. public int menu() {
  17. System.out.println("=============");
  18. System.out.println("欢迎管理员使用图书系统");
  19. System.out.println("1、查找图书");
  20. System.out.println("2、新增图书");
  21. System.out.println("3、删除图书");
  22. System.out.println("4、显示所有图书");
  23. System.out.println("0、退出系统");
  24. System.out.println("=============");
  25. System.out.println("输入你的选择");
  26. Scanner scanner=new Scanner(System.in);
  27. int choice=scanner.nextInt();
  28. return choice;
  29. }
  30. }

四、对类进行整合(编写主函数)

  1. import book.BookList;
  2. import uesr.AdminUser;
  3. import uesr.NormalUser;
  4. import uesr.User;
  5. import java.util.Scanner;
  6. public class Main {
  7. public static User login(){
  8. Scanner scanner=new Scanner(System.in);
  9. System.out.println("请输入姓名");
  10. String name=scanner.next();
  11. System.out.println("请输入你的身份(1、管理员 2、普通用户)");
  12. int who=scanner.nextInt();
  13. if(who==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=user.menu();
  23. user.doOperation(choice,bookList);
  24. }
  25. }

对于主函数的整合,我们想要将他们结合起来我们要在User中添加doOperation方法:

  1. public void doOperation(int choice, BookList bookList){
  2. this.operations[choice].work(bookList);
  3. }

通过调用方法来进行对于类的整合。

最后我们加入while循环使代码循环起来

  1. while (true) {
  2. int choice = user.menu();
  3. user.doOperation(choice, bookList);
  4. }

实现:

五、对于操着相关类的实现

5.1 AddOperation(添加)实现类

  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. System.out.println("新曾图书!");
  9. Scanner scanner=new Scanner(System.in);
  10. System.out.println("请输入书的名称:");
  11. String name=scanner.nextLine();
  12. System.out.println("请输入书的作者:");
  13. String author=scanner.nextLine();
  14. System.out.println("请输入书的类型:");
  15. String type=scanner.nextLine();
  16. System.out.println("请输入书的价格:");
  17. int price=scanner.nextInt();
  18. Book book=new Book(name,author,price,type);
  19. int size=bookList.getSize();
  20. for (int i = 0; i < size; i++) {
  21. Book book1=bookList.getBook(i);
  22. if(book.getName().equals(book1.getName())){
  23. System.out.println("书名已经重复");
  24. return;
  25. }
  26. }
  27. bookList.setBook(size,book);
  28. bookList.setSize(size+1);
  29. System.out.println("新增图书成功!!!!");
  30. }
  31. }

在使用时我们要在BookList中加入两个方法

  1. public Book getBook(int size){
  2. return books[size];
  3. }
  4. public void setBook(int pos,Book book){
  5. books[pos]=book;
  6. }

5.2 DisplayOperation(打印)类的实现

  1. public class DisplayOperation implements IOperation{
  2. @Override
  3. public void work(BookList bookList) {
  4. int size=bookList.getSize();
  5. for (int i = 0; i < size; i++) {
  6. Book book1=bookList.getBook(i);
  7. System.out.println(book1);
  8. }
  9. }
  10. }

要注意:不能直接写System.out.println(bookList[i]);

5.3 FindOperation(查找)类的实现

  1. public class FindOperation implements IOperation{
  2. @Override
  3. public void work(BookList bookList) {
  4. System.out.println("查找图书!!");
  5. System.out.println("请输入图书的名称:");
  6. Scanner scanner=new Scanner(System.in);
  7. String name=scanner.nextLine();
  8. int size=bookList.getSize();
  9. for (int i = 0; i < size; i++) {
  10. Book book1=bookList.getBook(i);
  11. if(book1.getName().equals(name)){
  12. System.out.println("找到了!!!");
  13. System.out.println(book1);
  14. return;
  15. }
  16. }
  17. System.out.println("没有找到!!!");
  18. }
  19. }

5.4 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. }

5.5 DelOperation(删除)类的实现

  1. public class DelOperation implements IOperation{
  2. @Override
  3. public void work(BookList bookList) {
  4. System.out.println("删除图书!!");
  5. System.out.println("请输入删除图书的名称:");
  6. Scanner scanner=new Scanner(System.in);
  7. String name=scanner.nextLine();
  8. int size=bookList.getSize();
  9. int pos=-1;
  10. for (int i = 0; i < size; i++) {
  11. Book book1=bookList.getBook(i);
  12. if(book1.getName().equals(name)){
  13. pos=i;
  14. break;
  15. }
  16. }
  17. if(pos!=-1){
  18. for (int i = pos; i <size-1 ; i++) {
  19. Book book=bookList.getBook(i+1);
  20. bookList.setBook(i,book);
  21. }
  22. bookList.setSize(size-1);
  23. System.out.println("删除成功!!!");
  24. return;
  25. }
  26. System.out.println("删除失败!!!!");
  27. }
  28. }

5.6 BorrowOperation(借阅)类的实现

  1. public class BorrowOperation implements IOperation{
  2. @Override
  3. public void work(BookList bookList) {
  4. System.out.println("借阅图书!!");
  5. System.out.println("请输入借阅图书的名称:");
  6. Scanner scanner=new Scanner(System.in);
  7. String name=scanner.nextLine();
  8. int size=bookList.getSize();
  9. for (int i = 0; i < size; i++) {
  10. Book book1=bookList.getBook(i);
  11. if(book1.getName().equals(name)){
  12. book1.setBorrowed(true);
  13. System.out.println("借阅成功!!!!");
  14. return;
  15. }
  16. }
  17. System.out.println("借阅失败!!!");
  18. }
  19. }

5.7 ReturnOperation(归还)类的实现

  1. public class RemoveOperation implements IOperation{
  2. @Override
  3. public void work(BookList bookList) {
  4. System.out.println("归还图书!!");
  5. System.out.println("请输入归还图书的名称:");
  6. Scanner scanner=new Scanner(System.in);
  7. String name=scanner.nextLine();
  8. int size=bookList.getSize();
  9. for (int i = 0; i < size; i++) {
  10. Book book1=bookList.getBook(i);
  11. if(book1.getName().equals(name)){
  12. book1.setBorrowed(false);
  13. System.out.println("归还成功!!!!");
  14. return;
  15. }
  16. }
  17. System.out.println("归还失败!!!");
  18. }
  19. }

这样简单的图书管理系统就做完成了,下面是代码:图书管理系统

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

闽ICP备14008679号