• Java实现图书管理系统



     一、分析有主要对象

    二、整理思路

    三、框架的搭建

    四、操作内部的具体实现


    一、分析主要对象

    我们做的图书管理系统的目的,是可以根据不同的用户,所能执行的操作不一样,主要有增删查改图书等操作,选择这些不同的操作会给我们反馈不一样的结果,而我们的主要对象就有书、书架、用户、操作这四个对象。


    二、整理思路

    书里面可以放书名、作者、价格等变量,书架是用来放书的,因为书有多本,所以我们可以用数组管理起来,也方便后面的调用,我们可以把书和书架放在同一包用户分为普通用户和管理员用户,也可以把普通用户和管理员用户放在同一个包上,在建立一个用户类,利用多态,抽象类方便将他们统一不同的用户操作也会不同,也可以用一个把许多操作放在一起,再利用接口,通过接口把这些操作串起来。


    三、框架的搭建

    三个包和一个主方法

    包下面创建书类和书架类

    在书类我们创建相对的变量(名字,作者,类型,价格,bollen类型判断是否被借出,),在创建设置和得到这些变量的方法,以及构造方法初始化这些变量、重写toString方法,改写成我们想要打印显示出来的效果

    代码如下

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

    创建书架类

    里面放书,并且默认初始化三本书创建书类的数组,记录书的数量,设置和得到书的方法和书的数量,还有一个设置和得到书的数组的方法

    代码如下

    1. public class BookList{
    2. private Book[] books;//管理书的数组
    3. private int usedSize;//记录书的数量
    4. private static final int DEFAULT_CAPACITY = 10;
    5. public BookList(){
    6. //默认初始化三本书
    7. this.books = new Book[DEFAULT_CAPACITY];
    8. books[0] = new Book("三国演义" ,"罗贯中" ,"小说", 10);
    9. books[1] = new Book("西游记", "吴承恩","小说",9);
    10. books[2] = new Book("红楼梦", "曹雪芹", "小说", 19);
    11. this.usedSize = 3;
    12. }
    13. public Book getBook(int pos) {
    14. return books[pos];
    15. }
    16. public Book setBook(int pos, Book book) {
    17. return books[pos] = book;
    18. }
    19. public Book[] getBooks() {
    20. return books;
    21. }
    22. public int getUsedSize() {
    23. return usedSize;
    24. }
    25. public void setUsedSize(int usedSize) {
    26. this.usedSize = usedSize;
    27. }
    28. }

    创建操作的包里面的类,

    用一个接口,统一管理起来这些操作类,如下

    使用work方法调用这些类

    其他类都实现这个IOperation接口

    如下

    其他的类也一样,就不展示了

    用户包

    上面两个用户类继承User类,并且把User定义成抽象类

    User类内部具体代码如下:

    1. public abstract class User {
    2. protected String name;
    3. public User(String name) {
    4. this.name = name;
    5. }
    6. protected IOperation[] iOperations;
    7. public abstract int menu();
    8. public void doOperation(int choice, BookList bookList) {
    9. iOperations[choice].work(bookList);
    10. }
    11. }
    AdminUser类和NormalUser类
    1. public class AdminUser extends User{
    2. public AdminUser(String name) {
    3. super(name);
    4. this.iOperations = new IOperation[] {
    5. new ExitOperation(),
    6. new FindOperation(),
    7. new AddOperation(),
    8. new DelOperation(),
    9. new ShowOperation()
    10. };
    11. }
    12. public int menu() {
    13. System.out.println("*****管理员用户*****");
    14. System.out.println("1.查找图书");
    15. System.out.println("2.新增图书");
    16. System.out.println("3.删除图书");
    17. System.out.println("4.显示图书");
    18. System.out.println("0.退出系统");
    19. System.out.println("*****************");
    20. System.out.println("请输入您的操作:");
    21. Scanner scanner = new Scanner(System.in);
    22. int choice = scanner.nextInt();
    23. return choice;
    24. }
    25. }
    1. public class NormalUser extends User{
    2. public NormalUser(String name) {
    3. super(name);
    4. this.iOperations = new IOperation[] {
    5. new ExitOperation(),
    6. new FindOperation(),
    7. new BorrowOperation(),
    8. new ReturnOperation()
    9. };
    10. }
    11. @Override
    12. public int menu() {
    13. System.out.println("*****普通用户******");
    14. System.out.println("1.查找图书");
    15. System.out.println("2.借阅图书");
    16. System.out.println("3.归还图书");
    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. }

    写Main主方法

    首先,我们要实例化一个书架,然后根据提示输入选择,展示出一个菜单供我们选择,我们要写一个登录的方法,而选择不同,返回的类型也不同,这里运用到向上转型,代码如下

    1. public class Main {
    2. public static User login() {
    3. Scanner scanner = new Scanner(System.in);
    4. System.out.println("请输入您的姓名");
    5. String name = scanner.nextLine();
    6. System.out.println("请输入您的身份 1、管理员用户 2、普通用户->");
    7. int choice = scanner.nextInt();
    8. if(choice == 1) {
    9. return new AdminUser(name);
    10. }else {
    11. return new NormalUser(name);
    12. }
    13. }
    14. public static void main(String[] args) {
    15. BookList bookList = new BookList();
    16. User user = login();
    17. int choice =user.menu();
    18. user.doOperation(choice, bookList);
    19. }
    20. }

    用user和接口把他们全部串起来

    通过我们不同的选择,就会调用不同的操作

    四、操作内部的具体实现

    AddOperation:

    1. public class AddOperation implements IOperation{
    2. public void work(BookList bookList) {
    3. System.out.println("新增图书");
    4. Scanner scanner = new Scanner(System.in);
    5. System.out.println("请输入书名");
    6. String name = scanner.nextLine();
    7. System.out.println("请输入作者");
    8. String author = scanner.nextLine();
    9. System.out.println("请输入类型");
    10. String type = scanner.nextLine();
    11. System.out.println("请输入价格");
    12. int price = scanner.nextInt();
    13. Book book = new Book(name, author, type, price);
    14. int currentSize = bookList.getUsedSize();
    15. int i = 0;
    16. for (; i < currentSize; i++) {
    17. Book book1 = bookList.getBook(i);
    18. if(book1.getName().equals(name)) {
    19. System.out.println("有这本书了,不进行存放");
    20. }
    21. }
    22. if(currentSize == bookList.getBooks().length) {
    23. System.out.println("书架放满了");
    24. return;
    25. } else {
    26. bookList.setBook(currentSize,book);
    27. bookList.setUsedSize(currentSize + 1);
    28. }
    29. }
    30. }

    BorrowOperation:

    1. public class BorrowOperation implements IOperation{
    2. public void work(BookList bookList) {
    3. System.out.println("借阅图书");
    4. Scanner scanner = new Scanner(System.in);
    5. System.out.println("请输入您要借阅的图书");
    6. String name = scanner.nextLine();
    7. int currentSize = bookList.getUsedSize();
    8. for (int i = 0; i < currentSize; i++) {
    9. Book book = new BookList().getBook(i);
    10. if(book.getName().equals(name)) {
    11. book.setBorrow(true);
    12. System.out.println("借阅成功");
    13. System.out.println(book);
    14. return;
    15. }
    16. }
    17. System.out.println("您要查阅的图书没有");
    18. }
    19. }

    DelOperation:

    1. public class DelOperation implements IOperation{
    2. public void work(BookList bookList) {
    3. System.out.println("删除图书");
    4. Scanner scanner = new Scanner(System.in);
    5. System.out.println("请输入您要删除的图书");
    6. String name = scanner.nextLine();
    7. int currentSize = bookList.getUsedSize();
    8. int pos = -1;
    9. int i = 0;
    10. for (; i < currentSize; i++) {
    11. Book book = bookList.getBook(i);
    12. if(book.getName().equals(name)) {
    13. pos = i;//标记要删除书的下标
    14. break;
    15. }
    16. }
    17. if(i == currentSize) {
    18. System.out.println("没有您要删除的图书");
    19. }
    20. int j = pos;
    21. //下标[j+1]放在[j]上,往前推
    22. for (; j < currentSize - 1; j++) {
    23. Book book = bookList.getBook(j + 1);
    24. bookList.setBook(j, book);
    25. }
    26. bookList.setBook(j, null);
    27. bookList.setUsedSize(currentSize - 1);
    28. }
    29. }

    ExitOperation:

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

    FindOperation:

    1. public class FindOperation implements IOperation{
    2. public void work(BookList bookList) {
    3. System.out.println("查找图书");
    4. Scanner scanner = new Scanner(System.in);
    5. System.out.println("请输入您要查找的图书");
    6. String name = scanner.nextLine();
    7. int currentSize = bookList.getUsedSize();
    8. for (int i = 0; i < currentSize; i++) {
    9. Book book = bookList.getBook(i);
    10. if(book.getName().equals(name)) {
    11. System.out.println("找到了这本书,信息如下");
    12. System.out.println(book);
    13. return;
    14. }
    15. }
    16. System.out.println("没找到这本书");
    17. }
    18. }

    ReturnOperation:

    1. public class ReturnOperation implements IOperation{
    2. public void work(BookList bookList) {
    3. System.out.println("归还图书");
    4. Scanner scanner = new Scanner(System.in);
    5. System.out.println("请输入您要归还的图书");
    6. String name = scanner.nextLine();
    7. int currentSize = bookList.getUsedSize();
    8. for (int i = 0; i < currentSize; i++) {
    9. Book book = bookList.getBook(i);
    10. if(book.getName().equals(name)) {
    11. System.out.println("归还成功,归还的图书如下:");
    12. System.out.println(book);
    13. return;
    14. }
    15. }
    16. System.out.println("您要归还的图书不存在");
    17. }
    18. }

    ShowOperation:

    1. public class ShowOperation implements IOperation{
    2. public void work(BookList bookList) {
    3. System.out.println("显示图书");
    4. int currentSize = bookList.getUsedSize();
    5. for (int i = 0; i < currentSize; i++) {
    6. Book book = bookList.getBook(i);
    7. System.out.println(book);
    8. }
    9. }
    10. }

    都看到这了,观众老爷方便的话点个赞呗

  • 相关阅读:
    SLAM学习笔记(二)
    缓存篇—缓存击穿
    iwebsec靶场搭建
    数据预处理和特征工程2-缺失值处理、分类型特征:编码与哑变量
    cmake配置opencv与boost库
    直播常见问题原因汇总
    微信小程序小说阅读器+后台管理系统|前后分离VUE
    SpringBoot+Mybatis-Plus+Thymeleaf 实现增删改查+登录/注册
    鸿蒙4.0开发笔记之DevEco Studio之配置代码片段快速生成(三)
    LabVIEW 应用程序视窗始终置于顶层
  • 原文地址:https://blog.csdn.net/cool_tao6/article/details/132952017