• Java实现图书管理系统


    目录

    一、整体思路分析

    二、分模块完成功能设计 

    1、图书模块

    a.图书类

    b.书架类 

     2、操作模块

    a.添加图书

    b.删除图书

    c.显示图书

    d.查找图书 

    e.借阅图书 

    f.归还图书 

    g.退出系统

    3、用户模块

    a.User父类

    b.管理员 

    c.普通用户 

     4、主函数模块

    三、功能演示 


    一、整体思路分析

    对于图书管理系统,首先会有两类用户:普通用户和管理员。

    其次明确他们各自应该具备的权限:

    • 管理员:添加图书、删除图书、显示图书(显示所有的图书信息)、查看图书、退出系统。
    • 普通用户:显示图书、查找图书、借阅图书、归还图书、退出系统。

    那么大体就可以分为四个模块:图书模块(图书类和书架存放图书)、操作模块(封装用户的各类操作)、用户模块(管理员和普通用户)和主函数测试模块。

    二、分模块完成功能设计 

    1、图书模块

    a.图书类

    定义图书的相关属性,带参的构造方法,各个属性的get和set方法,以及重写toString和equals方法

    1. public class Book {
    2. private String name;//图书名称
    3. private String author;//图书作者
    4. private int price;//图书价格
    5. private String theme;//图书主题
    6. private boolean isBorrowed;//图书是否被借出
    7. public Book(String name, String author,int price, String theme) {
    8. this.name = name;
    9. this.price=price;
    10. this.author = author;
    11. this.theme = theme;
    12. this.isBorrowed = false;//默认未借出
    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 String getTheme() {
    27. return theme;
    28. }
    29. public void setTheme(String theme) {
    30. this.theme = theme;
    31. }
    32. public boolean isBorrowed() {
    33. return isBorrowed;
    34. }
    35. public void setBorrowed(boolean borrowed) {
    36. isBorrowed = borrowed;
    37. }
    38. public int getPrice() {
    39. return price;
    40. }
    41. public void setPrice(int price) {
    42. this.price = price;
    43. }
    44. @Override
    45. public String toString() {
    46. return name + "\t"+ author + "\t" +price+"\t"+ theme + "\t" + (isBorrowed()?"已借出":"未借出" );
    47. }
    48. @Override
    49. public boolean equals(Object o) {
    50. if (this == o) return true;
    51. if (o == null || getClass() != o.getClass()) return false;
    52. Book book = (Book) o;
    53. return isBorrowed == book.isBorrowed && Objects.equals(name, book.name) && Objects.equals(author, book.author) && Objects.equals(theme, book.theme);
    54. }
    55. }

    b.书架类 

    书架类需要定义一个图书类的数组,还有书架的容量,以及书架当前存放图书的数目,还需要定义获取指定位置图书的方法,以及指定位置修改图书的方法。对于构造方法应该是包含书架的容量,但是对于本系统的普通用户需要对图书进行相关操作,但是他没有添加用户的权限,故书架默认存放了几本图书。 

    1. public class Bookshelf {
    2. private Book[] books ;//书架类
    3. private int size;//书架当前存放图书的数目
    4. private int currentSize=0;
    5. public Bookshelf(int size) {
    6. books = new Book[size];
    7. this.size=size;
    8. }
    9. public Bookshelf()
    10. {
    11. books = new Book[]{
    12. new Book("微机原理","胡浩",55,"教材"),
    13. new Book("彼此尊重","张琪",90,"小说"),
    14. new Book("一夜暴富","小G",99,"玄幻"),
    15. null,null,null,null,null,null
    16. };
    17. this.size=10;
    18. this.currentSize=3;
    19. }
    20. public int getCurrentSize() {
    21. return currentSize;
    22. }
    23. public void setCurrentSize(int currentSize) {
    24. this.currentSize = currentSize;
    25. }
    26. public int getSize() {
    27. return size;
    28. }
    29. public void setSize(int size) {
    30. this.size = size;
    31. }
    32. /**
    33. * 获取指定位置的图书
    34. * @param pos
    35. * @return
    36. */
    37. public Book getBook(int pos){
    38. if(pos>=size){
    39. System.out.println("已超出书架当前容量");
    40. }else{
    41. return books[pos];
    42. }
    43. return null;
    44. }
    45. public void addBook(Book book){
    46. books[currentSize]=book;
    47. }
    48. /**
    49. * 修改指定位置的图书
    50. * @param pos
    51. * @param book
    52. * @return
    53. */
    54. public void modify(int pos,Book book){
    55. if(pos>=size){
    56. System.out.println("已超出书架当前容量");
    57. return ;
    58. }else{
    59. books[pos]=book;
    60. }
    61. }
    62. }

     2、操作模块

    由于管理员和普通用户都是对书架进行操作,可以定义一个操作接口,包含一个抽象方法,让所有的操作类都实现操作接口的方法。

    1. public interface Operate {
    2. void work(Bookshelf books);
    3. }

    a.添加图书

    首先应该判断书架是否已满,若未满,则输入图书信息,然后再需要对将要添加的图书与书架中的图书进行比较,判断是否已经存在,若未存在,则添加成功,书架目前存放图书数目加一,否则无法添加。

    1. public class AddBook implements Operate{
    2. @Override
    3. public void work(Bookshelf books) {
    4. if(books.getCurrentSize()>= books.getSize()){
    5. System.out.println("书架已满,无法继续添加");
    6. }else{
    7. System.out.println("请输入图书信息:");
    8. System.out.println("图书名称:");
    9. Scanner sc=new Scanner(System.in);
    10. String name=sc.nextLine();
    11. System.out.println("图书作者:");
    12. String author = sc.nextLine();
    13. System.out.println("图书价格:");
    14. int price = sc.nextInt();
    15. System.out.println("图书主题:");
    16. sc.nextLine();
    17. String theme= sc.nextLine();
    18. Book book = new Book(name, author, price, theme);
    19. for (int i = 0; i < books.getCurrentSize(); i++) {
    20. if(book.equals(books.getBook(i))){
    21. System.out.println("该图书已经存在,无法继续添加!");
    22. return;
    23. }
    24. }
    25. books.addBook(book);
    26. books.setCurrentSize(books.getCurrentSize()+1);
    27. System.out.println("添加成功!");
    28. }
    29. }
    30. }

    b.删除图书

    首先用户输入要删除的图书名称,然后在书架上进行查找,若能找到,标记图书所在位置,然后从此位置之后的一本书开始依次覆盖前一本书,然后将最后一个位置置为null,书架目前存放图书数目减一,否则找不到,无法删除。

    1. public class DelBook implements Operate{
    2. @Override
    3. public void work(Bookshelf books) {
    4. System.out.println("请输入要删除的图书名称");
    5. Scanner sc=new Scanner(System.in);
    6. String name=sc.nextLine();
    7. int pos=-1;
    8. for (int i = 0; i < books.getCurrentSize(); i++) {
    9. if(name.equals(books.getBook(i).getName())){
    10. pos=i;
    11. break;
    12. }
    13. }
    14. if(pos==-1){
    15. System.out.println("未找到您要删除的图书");
    16. }else{
    17. for(int i= pos+1;i<books.getCurrentSize();i++){
    18. Book book=books.getBook(i);
    19. books.modify(i-1,book);
    20. }
    21. books.modify(books.getCurrentSize(),null);
    22. books.setCurrentSize(books.getCurrentSize()-1);
    23. System.out.println("删除成功!");
    24. }
    25. }
    26. }

    c.显示图书

     对书架进行遍历,依次输出每本图书信息。

    1. public class DisplayBook implements Operate{
    2. @Override
    3. public void work(Bookshelf books) {
    4. System.out.println("===============================================");
    5. System.out.println("图书名称\t作者\t价格\t主题\t状态");
    6. for(int i=0;i
    7. System.out.println(books.getBook(i).toString());
    8. }
    9. System.out.println("===============================================");
    10. }
    11. }

    d.查找图书 

    用户输入要查找的图书名称,然后对书架进行遍历,若找到则输出图书信息,否则书架上并未存放这本书。

    1. public class FindBook implements Operate{
    2. @Override
    3. public void work(Bookshelf books) {
    4. System.out.println("请输入要查找的图书名称");
    5. Scanner sc=new Scanner(System.in);
    6. String name=sc.nextLine();
    7. int pos=-1;
    8. for (int i = 0; i < books.getCurrentSize(); i++) {
    9. if(name.equals(books.getBook(i).getName())){
    10. pos=i;
    11. break;
    12. }
    13. }
    14. if(pos==-1){
    15. System.out.println("该图书不存在");
    16. }else{
    17. System.out.println("===============================================");
    18. System.out.println("图书名称\t作者\t价格\t主题\t状态");
    19. System.out.println(books.getBook(pos).toString());
    20. System.out.println("===============================================");
    21. }
    22. }
    23. }

    e.借阅图书 

    用户输入图书名称,然后对书架进行遍历,若能找到并且图书未借出,则将图书的状态置为true,表示借出,否则无法借阅。

    1. public class BorrowBook implements Operate{
    2. @Override
    3. public void work(Bookshelf books) {
    4. System.out.println("请输入要借阅的图书名称");
    5. Scanner sc=new Scanner(System.in);
    6. String name=sc.nextLine();
    7. int pos=-1;
    8. for (int i = 0; i < books.getCurrentSize(); i++) {
    9. if(name.equals(books.getBook(i).getName())){
    10. pos=i;
    11. break;
    12. }
    13. }
    14. if(pos==-1){
    15. System.out.println("未找到您需要的图书");
    16. }else{
    17. if(books.getBook(pos).isBorrowed()==true){
    18. System.out.println("该图书已经借出");
    19. return;
    20. }
    21. books.getBook(pos).setBorrowed(true);
    22. System.out.println("借阅成功!");
    23. }
    24. }
    25. }

    f.归还图书 

    用户输入要归还的图书名称,然后在书架上依次进行遍历,找到要归还的图书,将图书的状态改为false,即为未借出,归还成功,若未找到,则不存在该图书,归还失败。

    1. public class ReturnBook implements Operate {
    2. @Override
    3. public void work(Bookshelf books) {
    4. System.out.println("请输入要归还的图书名称");
    5. Scanner sc=new Scanner(System.in);
    6. String name=sc.nextLine();
    7. int pos=-1;
    8. for (int i = 0; i < books.getCurrentSize(); i++) {
    9. if(name.equals(books.getBook(i).getName())){
    10. pos=i;
    11. break;
    12. }
    13. }
    14. if(pos==-1){
    15. System.out.println("未找到您需要的图书");
    16. }else{
    17. books.getBook(pos).setBorrowed(true);
    18. System.out.println("归还成功!");
    19. }
    20. }
    21. }

    g.退出系统

    即要退出循环,利用System.exit(0)正常退出。

    1. public class ExitOpe implements Operate{
    2. @Override
    3. public void work(Bookshelf books) {
    4. System.out.println("退出成功,欢迎下次使用!");
    5. System.exit(0);
    6. }
    7. }

    3、用户模块

    a.User父类

    定义一个用户的父类,定义了用户的姓名,以及包含操作的一个数组,还有菜单的抽象方法,以及封装了一个用户选择对应的操作类的方法。

    1. public abstract class User {
    2. private String name;
    3. public Operate[] operates;
    4. public User(String name) {
    5. this.name = name;
    6. }
    7. public String getName() {
    8. return name;
    9. }
    10. public void setName(String name) {
    11. this.name = name;
    12. }
    13. public abstract int menu();
    14. public void doWork(int option,Bookshelf books ){
    15. this.operates[option].work(books);
    16. }
    17. }

    b.管理员 

     继承User父类,重写菜单方法,在构造方法中,对操作数组进行相关定义。

    1. public class Administrator extends User{
    2. public Administrator(String name) {
    3. super(name);
    4. this.operates=new Operate[]{
    5. new ExitOpe(),
    6. new AddBook(),
    7. new DelBook(),
    8. new DisplayBook(),
    9. new FindBook(),
    10. };
    11. }
    12. @Override
    13. public int menu() {
    14. System.out.println("*******************************************");
    15. System.out.println(" 1.添加图书 2.删除图书 ");
    16. System.out.println(" 3.显示图书 4.查找图书 ");
    17. System.out.println(" 0.退出系统 ");
    18. System.out.println("*******************************************");
    19. System.out.println("请输入您的操作:");
    20. Scanner sc = new Scanner(System.in);
    21. int option = sc.nextInt();
    22. return option;
    23. }
    24. }

    c.普通用户 

      继承User父类,重写菜单方法,在构造方法中,对操作数组进行相关定义。

    1. public class RegularUser extends User{
    2. public RegularUser(String name) {
    3. super(name);
    4. this.operates=new Operate[]{
    5. new ExitOpe(),
    6. new BorrowBook(),
    7. new ReturnBook(),
    8. new DisplayBook(),
    9. new FindBook(),
    10. };
    11. }
    12. @Override
    13. public int menu() {
    14. System.out.println("*******************************************");
    15. System.out.println(" 1.借阅图书 2.归还图书 ");
    16. System.out.println(" 3.显示图书 4.查找图书 ");
    17. System.out.println(" 0.退出系统 ");
    18. System.out.println("*******************************************");
    19. System.out.println("请输入您的操作:");
    20. Scanner sc = new Scanner(System.in);
    21. int option = sc.nextInt();
    22. return option;
    23. }
    24. }

     4、主函数模块

     需要定义一个登录方法,来判断是普通用户还是管理员,该方法返回的是User类,用到了向上转型,登录成功之后就可以进入对应菜单进行相关操作。

    1. public class Main {
    2. public static User login() {
    3. System.out.println("欢迎来到图书管理系统");
    4. System.out.println("请选择身份:1>管理员 2>普通用户");
    5. Scanner sc = new Scanner(System.in);
    6. int option=sc.nextInt();
    7. if(option!=1&&option!=2){
    8. System.out.println("输入有误!");
    9. }
    10. System.out.println("请输入姓名");
    11. sc.nextLine();
    12. String name=sc.nextLine();
    13. System.out.println("欢迎"+name+",祝您使用愉快");
    14. if (option==1){
    15. return new Administrator(name);
    16. }else {
    17. return new RegularUser(name);
    18. }
    19. }
    20. public static void main(String[] args) {
    21. Bookshelf books = new Bookshelf();
    22. User user=login();
    23. while(true){
    24. user.doWork(user.menu(),books);
    25. }
    26. }
    27. }

    三、功能演示 

     管理员:

    普通用户:

     

     

     

     

  • 相关阅读:
    002讲:CAD2024下方任务栏不显示坐标解决方案——CAD知识讲堂
    linux运维笔记:ssh服务认证类型
    试从多个方面比较电路交换、报文交换和分组交换的主要优缺点?
    DMF (dimethylformamide), labeling grade,DMF(二甲基甲酰胺)标号等级
    MMCV学习——基础篇3(fileio)| 五千字:含示例代码教程
    Django第三章(模版系统全局变量-if判断-for循环-过滤器-模版继承/引用-引用静态文件)
    DHCP-PXE
    [基础服务] windows10安装WSL2
    Spark中广播的使用
    基于多源数据融合方法的中国1公里土地覆盖图(2000)
  • 原文地址:https://blog.csdn.net/m0_62404808/article/details/127927807