• 图书管理系统


    目录

    框架分析:

    Java文件分类:

    代码实现:

    book:

    Book

    BookList

    user 

    父类——User

    子类——管理员 AdminUser

    子类——普通用户 NormalUser

    operations:

    接口IOperation

    AddOperation

    BorrowOperation

    DelOperation

    ExitOperation

    FindOperation

     ReturnOperation 

     ShowOperation

    Main

    运行结果的部分展示:


    框架分析:

     具体实现类是根据用户的需求所添加的

    Java文件分类:

     

    代码实现:

    book:

    Book

    1. package com.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. //boolean类型默认是false,默认未被借出
    9. //构造方法
    10. public Book(String name, String author, int price, String type) {
    11. this.name = name;
    12. this.author = author;
    13. this.price = price;
    14. this.type = type;
    15. }
    16. //get、set方法:
    17. public String getName() {
    18. return name;
    19. }
    20. public void setName(String name) {
    21. this.name = name;
    22. }
    23. public String getAuthor() {
    24. return author;
    25. }
    26. public void setAuthor(String author) {
    27. this.author = author;
    28. }
    29. public int getPrice() {
    30. return price;
    31. }
    32. public void setPrice(int price) {
    33. this.price = price;
    34. }
    35. public String getType() {
    36. return type;
    37. }
    38. public void setType(String type) {
    39. this.type = type;
    40. }
    41. public boolean isBorrowed() {
    42. return isBorrowed;
    43. }
    44. public void setBorrowed(boolean borrowed) {
    45. isBorrowed = borrowed;
    46. }
    47. //打印:
    48. @Override
    49. public String toString() {
    50. return "Book{" +
    51. "name='" + name + '\'' +
    52. ", author='" + author + '\'' +
    53. ", price=" + price +
    54. ", type='" + type + '\'' +
    55. ((isBorrowed==true)?" 已借出":" 未借出 ")+
    56. '}';
    57. }
    58. }

    BookList

    1. package com.book;
    2. public class BookList {
    3. public Book[] books=new Book[10];
    4. public int usedSize;//计数器,记录当前书的本数
    5. //先存几本书
    6. public BookList() {
    7. books[0]=new Book("狼王梦","沈石溪",35,"小说");
    8. books[1]=new Book("活着","余华",40,"小说");
    9. books[2]=new Book("三体","刘慈欣",38,"小说");
    10. this.usedSize=3;
    11. }
    12. public int getUsedSize() {
    13. return usedSize;
    14. }
    15. public void setUsedSize(int usedSize) {
    16. this.usedSize = usedSize;
    17. }
    18. //获取指定位置
    19. public Book getPos(int pos) {
    20. return books[pos];
    21. }
    22. //存放在指定位置,指定位置为,当前可以存放书籍的书架上最后一个位置
    23. public void setBooks(Book book,int pos) {
    24. books[pos]=book;
    25. }
    26. }

    user 

    父类——User

    1. package com.user;
    2. import com.book.BookList;
    3. import com.operations.IOperation;
    4. public abstract class User {
    5. protected String name;
    6. protected 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. }

    子类——管理员 AdminUser

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

    子类——普通用户 NormalUser

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

    operations:

    接口IOperation

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

    AddOperation

    1. package com.operations;
    2. import com.book.Book;
    3. import com.book.BookList;
    4. import java.util.Scanner;
    5. //添加图书
    6. public class AddOperation implements IOperation{
    7. public void work(BookList bookList) {
    8. System.out.println("请输入要新增的书名:");
    9. Scanner scanner=new Scanner(System.in);
    10. String name=scanner.nextLine();
    11. System.out.println("请输入图书作者:");
    12. String author=scanner.nextLine();
    13. System.out.println("图书价格:");
    14. int price=scanner.nextInt();
    15. scanner.nextLine();//吸收换行符
    16. System.out.println("图书类型:");
    17. String type=scanner.nextLine();
    18. Book book=new Book(name,author,price,type);
    19. //获取当前可以存放书的位置
    20. int currentSize= bookList.getUsedSize();
    21. //将书放入指定位置
    22. bookList.setBooks(book,currentSize);
    23. //属的有效个数加一
    24. bookList.setUsedSize(currentSize+1);
    25. }
    26. }

    BorrowOperation

    1. package com.operations;
    2. import com.book.Book;
    3. import com.book.BookList;
    4. import java.util.Scanner;
    5. //借阅图书
    6. public class BorrowOperation implements IOperation{
    7. @Override
    8. public void work(BookList bookList) {
    9. System.out.println("请输出要借阅图书的名字");
    10. Scanner scanner=new Scanner(System.in);
    11. String name=scanner.nextLine();
    12. int currentSize = bookList.getUsedSize();
    13. for (int i = 0; i < currentSize; i++) {
    14. Book book=bookList.getPos(i);
    15. if(name.equals(book.getName())) {
    16. if(book.isBorrowed()) {
    17. System.out.println("已被借出!");
    18. } else {
    19. book.setBorrowed(true);
    20. System.out.println("借阅成功");
    21. }
    22. return ;
    23. }
    24. }
    25. System.out.println("查无此书");
    26. }
    27. }

    DelOperation

    1. package com.operations;
    2. import com.book.Book;
    3. import com.book.BookList;
    4. import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
    5. import java.util.Scanner;
    6. //删除图书
    7. public class DelOperation implements IOperation {
    8. @Override
    9. public void work(BookList bookList) {
    10. System.out.println("请输入你要删除的书名:");
    11. Scanner scanner=new Scanner(System.in);
    12. String name=scanner.nextLine();
    13. //遍历数组,查找是否有要删除的书,如果有,则记录下标
    14. int index=-1;
    15. int currentSize = bookList.getUsedSize();
    16. for (int i = 0; i < currentSize; i++) {
    17. Book book=bookList.getPos(i);
    18. if(name.equals(book.getName())) {
    19. index=i;
    20. break;
    21. }
    22. }
    23. //
    24. if(index==-1) {
    25. System.out.println("查无此书");
    26. return ;
    27. }
    28. //删除
    29. for (int i = index; i < currentSize-1; i++) {
    30. //后面使用的是i+1,所以这里是Size-1
    31. Book book=bookList.getPos(i+1);
    32. bookList.setBooks(book,i);
    33. }
    34. //删除后,最后一个需要置空,否则一直有人引用,内存不会释放
    35. bookList.setBooks(null,currentSize-1);
    36. bookList.setUsedSize(currentSize-1);
    37. System.out.println("删除成功!");
    38. }
    39. }

    ExitOperation

    1. package com.operations;
    2. import com.book.BookList;
    3. //退出
    4. public class ExitOperation implements IOperation{
    5. @Override
    6. public void work(BookList bookList) {
    7. System.exit(0);
    8. }
    9. }

    FindOperation

    1. package com.operations;
    2. import com.book.Book;
    3. import com.book.BookList;
    4. import java.util.Scanner;
    5. //查找图书
    6. public class FindOperation implements IOperation{
    7. public void work(BookList bookList) {
    8. System.out.println("请输入要查找图书的名字:");
    9. Scanner scanner=new Scanner(System.in);
    10. String name=scanner.nextLine();
    11. int currentSize= bookList.getUsedSize();
    12. for (int i = 0; i < currentSize; i++) {
    13. Book book=bookList.getPos(i);
    14. if(name.equals(book.getName())) {
    15. System.out.println(book);
    16. return;
    17. }
    18. }
    19. System.out.println("没有这本书");
    20. }
    21. }

     ReturnOperation 

    1. package com.operations;
    2. import com.book.Book;
    3. import com.book.BookList;
    4. import java.util.Scanner;
    5. public class ReturnOperation implements IOperation{
    6. @Override
    7. public void work(BookList bookList) {
    8. System.out.println("归还图书!");
    9. System.out.println("请输入你要归还的图书的名字:");
    10. Scanner scanner = new Scanner(System.in);
    11. String name = scanner.nextLine();
    12. int currentSize = bookList.getUsedSize();;
    13. for (int i = 0; i < currentSize; i++) {
    14. Book book = bookList.getPos(i);
    15. if(name.equals(book.getName())) {
    16. book.setBorrowed(false);
    17. System.out.println("归还图书成功!");
    18. return;
    19. }
    20. }
    21. System.out.println("没有你要归还的图书!");
    22. }
    23. }

     ShowOperation

    1. package com.operations;
    2. import com.book.Book;
    3. import com.book.BookList;
    4. //显示图书
    5. public class ShowOperation implements IOperation{
    6. @Override
    7. public void work(BookList bookList) {
    8. int currentSize= bookList.getUsedSize();
    9. for(int i=0;i
    10. Book book=bookList.getPos(i);
    11. System.out.println(book);
    12. }
    13. }
    14. }

    Main

    1. package com;
    2. import com.book.BookList;
    3. import com.user.AdminUser;
    4. import com.user.NormalUser;
    5. import com.user.User;
    6. import java.util.Scanner;
    7. /**
    8. * Created with IntelliJ IDEA.
    9. * Description:
    10. * User:龙宝
    11. * Date:2022-08-10
    12. * Time:15:20
    13. */
    14. public class Main {
    15. public static void main(String[] args) {
    16. //准备数据
    17. BookList bookList=new BookList();
    18. //登录
    19. User user=login();//user引用的对象与你的选择有关
    20. while(true) {
    21. int choice=user.menu();//根据引用的对象不同,调用相应的菜单,多态绑定
    22. user.doOperation(choice,bookList);
    23. }
    24. }
    25. public static User login() {
    26. System.out.println("请输入姓名:");
    27. Scanner scanner=new Scanner(System.in);
    28. String userName=scanner.nextLine();
    29. System.out.println("请输入身份:1、管理员 2、普通用户");
    30. int choice= scanner.nextInt();
    31. if(choice==1) {
    32. return new AdminUser(userName);
    33. } else {
    34. return new NormalUser(userName);
    35. }
    36. }
    37. }

    运行结果的部分展示:

     

    本期结束啦,下期见!!! 

  • 相关阅读:
    ExcelPatternTool 开箱即用的Excel工具包现已发布!
    Java编程之道:巧妙解决Excel公式迭代计算难题
    Android 12 源码分析 —— 应用层 六(StatusBar的UI创建和初始化)
    SpringBoot集成OpenPDF导出pdf
    .NET的基元类型包括哪些?Unmanaged和Blittable类型又是什么?
    zabbix监控安装-linux
    MYSQL 移机重装步骤(windows11)
    线程安全与共享资源
    安卓APP(1)——安卓工程构建和第一APP运行
    loganalyzer 展示数据库中的日志
  • 原文地址:https://blog.csdn.net/LYJbao/article/details/126278202