• Java之图书管理系统


    🤷‍♀️🤷‍♀️🤷‍♀️ 今天给大家分享一下Java实现一个简易的图书管理系统!

    清风的个人主页🎉✏️✏️ 

    🌂c/java领域新星创作者

    🎉欢迎👍点赞✍评论❤️收藏

    😛😛😛希望我的文章能对你有所帮助,有不足的地方还请各位看官多多指教,大家一起学习交流!

    动动你们发财的小手,点点关注点点赞!在此谢过啦!哈哈哈!😛😛😛


    目录

     一、找到抽象化的对象

    1.书类

    2.书架类

    二、管理员与普通用户登录

    三、实现的功能

    1.查找图书

    2.新增图书(管理员功能)

    3.删除图书(管理员功能)

    4.显示图书信息

    5.退出系统

    6.借阅图书(普通用户功能)

    7.归还图书(普通用户功能)

    四、main方法



    图书管理系统源码链接-满船清梦压星河的Gitee

     

     一、找到抽象化的对象

    1.书类

    经过分析,我们可以知道,书可以抽象成一个类型。它的属性包括:书名,作者,价格,书的类型等等...我们就先以这些为例。为了保持封装性,我们把这些属性都设置成private修饰的。

    下面是书类的定义代码:
    这段代码包括一些构造函数以及设置书的属性、重写String函数等。

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

    2.书架类

    我们可以利用一个数组来存放这些书籍,并记录当前存放书籍的数量,为后续的增删查改做准备,同时初始化有三本书籍。

    下面是代码:

    1. public class BookList {
    2. private Book[] books;
    3. private int usedSize;//记录当前书架上实际存放的书的数量
    4. public BookList(){
    5. this.books=new Book[10];
    6. this.books[0]=new Book("三国演义","罗贯中",18,"小说");
    7. this.books[1]=new Book("西游记","吴承恩",28,"小说");
    8. this.books[2]=new Book("红楼梦","曹雪芹",35,"小说");
    9. this.usedSize=3;
    10. }
    11. //获取当前存放书籍数量
    12. public int getUsedSize() {
    13. return usedSize;
    14. }
    15. //设置存放书籍数量
    16. public void setUsedSize(int usedSize) {
    17. this.usedSize = usedSize;
    18. }
    19. //返回下标为pos的书籍
    20. public Book getBook(int pos){
    21. return books[pos];
    22. }
    23. //设置下标为pos位置的书籍为book
    24. public void setBook(int pos,Book book){
    25. books[pos]=book;
    26. }
    27. //返回书籍这个数组
    28. public Book[] getBooks(){
    29. return books;
    30. }
    31. }

    二、管理员与普通用户登录

    首先定义一个用户抽象类,再定义管理员与普通用户去继承抽象类并重写菜单方法。

    下面是用户抽象类代码:

    1. abstract public 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 doOperation(int choice, BookList bookList){
    9. ioPerations[choice].work(bookList);
    10. }
    11. }

    管理员类代码:

    1. public class AdiminUser extends User{
    2. public AdiminUser(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. Scanner scanner=new Scanner(System.in);
    21. System.out.println("请输入你的选择:>");
    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 BorrowedOperation(),
    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. Scanner scanner=new Scanner(System.in);
    19. System.out.println("请输入你的选择:>");
    20. int choice=scanner.nextInt();
    21. return choice;
    22. }
    23. }

    三、实现的功能

    实现以下几个功能,可以定义一个接口,方便后续的相关操作。

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

    1.查找图书

    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. //遍历这个数组
    9. int currentSize=bookList.getUsedSize();
    10. for (int i = 0; i < currentSize; i++) {
    11. Book book=bookList.getBook(i);
    12. if(book.getName().equals(name)){
    13. System.out.println("该书信息如下>:");
    14. System.out.println(book);
    15. return;
    16. }
    17. }
    18. System.out.println("无此书!!!");
    19. }
    20. }

    2.新增图书(管理员功能)

    1. public class AddOperation implements IOPeration{
    2. @Override
    3. public void work(BookList bookList) {
    4. System.out.println("新增图书>:");
    5. int cunrrentSize=bookList.getUsedSize();
    6. if (cunrrentSize==bookList.getBooks().length){
    7. System.out.println("书架已满!");
    8. return;
    9. }
    10. Scanner scanner=new Scanner(System.in);
    11. System.out.println("输入要新增书籍>:");
    12. String name=scanner.nextLine();
    13. //检查数组当中有没有这本书
    14. for (int i = 0; i
    15. Book book1=bookList.getBook(i);
    16. if(book1.getName().equals(name)){
    17. System.out.println("该书已存放,无需新增!!!");
    18. return;
    19. }
    20. }
    21. System.out.println("输入书籍作者>:");
    22. String author=scanner.nextLine();
    23. System.out.println("输入书籍类型>:");
    24. String type=scanner.nextLine();
    25. System.out.println("输入书籍价格>:");
    26. int price=scanner.nextInt();
    27. Book book=new Book(name,author,price,type);
    28. bookList.setBook(cunrrentSize,book);
    29. bookList.setUsedSize(cunrrentSize+1);
    30. System.out.println("新增书籍成功!!!");
    31. }
    32. }

    3.删除图书(管理员功能)

    1. public class AddOperation implements IOPeration{
    2. @Override
    3. public void work(BookList bookList) {
    4. System.out.println("新增图书>:");
    5. int cunrrentSize=bookList.getUsedSize();
    6. if (cunrrentSize==bookList.getBooks().length){
    7. System.out.println("书架已满!");
    8. return;
    9. }
    10. Scanner scanner=new Scanner(System.in);
    11. System.out.println("输入要新增书籍>:");
    12. String name=scanner.nextLine();
    13. //检查数组当中有没有这本书
    14. for (int i = 0; i
    15. Book book1=bookList.getBook(i);
    16. if(book1.getName().equals(name)){
    17. System.out.println("该书已存放,无需新增!!!");
    18. return;
    19. }
    20. }
    21. System.out.println("输入书籍作者>:");
    22. String author=scanner.nextLine();
    23. System.out.println("输入书籍类型>:");
    24. String type=scanner.nextLine();
    25. System.out.println("输入书籍价格>:");
    26. int price=scanner.nextInt();
    27. Book book=new Book(name,author,price,type);
    28. bookList.setBook(cunrrentSize,book);
    29. bookList.setUsedSize(cunrrentSize+1);
    30. System.out.println("新增书籍成功!!!");
    31. }
    32. }

    4.显示图书信息

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

    5.退出系统

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

    6.借阅图书(普通用户功能)

    1. public class BorrowedOperation implements IOPeration{
    2. @Override
    3. public void work(BookList bookList) {
    4. System.out.println("借阅图书>:");
    5. /**
    6. * 1.你要借阅哪本书?
    7. * 2.你借阅的书存在吗?
    8. * 借阅的方式是什么?
    9. */
    10. Scanner scanner=new Scanner(System.in);
    11. System.out.println("输入要借阅书籍>:");
    12. String name=scanner.nextLine();
    13. int currentSize=bookList.getUsedSize();
    14. int i = 0;
    15. for (; i
    16. Book book=bookList.getBook(i);
    17. if(book.getName().equals(name)){
    18. book.setBorrowed(true);
    19. System.out.println("借阅成功!!!");
    20. return;
    21. }
    22. }
    23. if(i==currentSize){
    24. System.out.println("该书不存在,无法借阅!!!");
    25. }
    26. }
    27. }

    7.归还图书(普通用户功能)

    1. public class ReturnOperation implements IOPeration{
    2. @Override
    3. public void work(BookList bookList) {
    4. System.out.println("归还图书>:");
    5. Scanner scanner=new Scanner(System.in);
    6. System.out.println("输入要归还书籍>:");
    7. String name=scanner.nextLine();
    8. int currentSize=bookList.getUsedSize();
    9. int i = 0;
    10. for (; i
    11. Book book=bookList.getBook(i);
    12. if(book.getName().equals(name)){
    13. book.setBorrowed(false);
    14. System.out.println("归还成功!!!");
    15. return;
    16. }
    17. }
    18. if(i==currentSize){
    19. System.out.println("该书不存在,无需归还!!!");
    20. }
    21. }
    22. }

    四、main方法

    1. public class Main {
    2. public static User login() {
    3. System.out.println("请输入你的姓名:>");
    4. Scanner scanner = new Scanner(System.in);
    5. String name = scanner.nextLine();
    6. System.out.println("请输入你的身份:> 1.管理员 2.普通用户");
    7. int choice = scanner.nextInt();
    8. if (choice == 1) {
    9. //管理员
    10. return new AdiminUser(name);
    11. } else {
    12. //普通用户
    13. return new NormalUser(name);
    14. }
    15. }
    16. public static void main(String[] args) {
    17. BookList bookList = new BookList();
    18. //user指向哪个对象,就看返回值是什么
    19. User user = login();
    20. while (true) {
    21. int choice = user.menu();
    22. System.out.println("choice:" + choice);
    23. //根据choice决定调用的是哪个方法
    24. user.doOperation(choice, bookList);
    25. }
    26. }
    27. }

    🎉好啦,今天的分享就到这里!!

    创作不易,还希望各位大佬支持一下!

    👍点赞,你的认可是我创作的动力!

    收藏,你的青睐是我努力的方向!

    ✏️评论:你的意见是我进步的财富!

     

  • 相关阅读:
    Redhat8.3上部署Lustre文件系统
    恒创科技:无法与服务器建立安全连接怎么解决?
    springboot入门
    java源码系列:HashMap底层存储原理详解——1、快速开始-存储和查询数据
    高通平台Android 蓝牙调试和配置手册--debug Logcat的log
    Redis概述和安装
    什么软件做可视化大屏最简单、最快?
    Java系列技术之JavaScript基础(从入门开始)③
    FSC在全球范围内增强品牌相关度,促进公众理解
    2023国赛数学建模C题思路模型代码 高教社杯
  • 原文地址:https://blog.csdn.net/m0_73920844/article/details/134223331