• JavaSE综合大练习——图书管理系统的实现


    目录

    图书管理系统演示

    包的分类

    包的内部成员

    book包

    user包

    operation包

    具体实现

    main函数

    User

    AdminUser

    NormalUser

    Book

    BookList

    FindOperation

    AddOperation 

    DelOperation

    DisplayOperation

    ExitOperation

    BorrowOperation

    ReturnOperation


    图书管理系统演示

    以管理员身份进入图书管理系统

     显示图书

    查找图书 

    增加图书 

     

     再次显示图书

     删除图书

    再次显示图书

    退出系统

    以普通成员身份进入图书管理系统

    借阅图书

    查找图书

    归还图书

     

    再次查找图书

    包的分类

    我们要实现图书管理系统首先必不可少的是要确定有哪几个对象,每个对象都放入一个包当中。

    图书管理系统的对象有:图书,使用者。所以我们这里就可以分俩个包。为了方便理解,我们也可以把使用者的一些操作单独放入一个包当中

    包的内部成员

    包分配好了之后,我们就可以在包中写每个包中的具体成员了。

    比如book包,我们可以有图书类和用于存放图书的书架类

    user包,我们可以有管理员类,普通成员类

    operation包中就可以有一些操作:查找图书,增加图书,删除图书,显示图书,借阅图书,归还图书,退出图书管理系统。

    book包

    user包

    因为不管是管理员还是普通成员,都是使用者,这里我们可以写一个User类,让管理员和普通成员继承它,方便以后的一些操作。

    operation包

     因为这些方法都是对书架上的图书进行一些操作,所以我们可以写一个接口来作为一种规范。让其它方法都继承它。

    具体实现

    main函数

    1. import book.BookList;
    2. import user.AdminUser;
    3. import user.NormalUser;
    4. import user.User;
    5. import java.util.Scanner;
    6. public class Main {
    7. public static User login(){
    8. System.out.println("请输入你的姓名");
    9. Scanner scanner = new Scanner(System.in);
    10. String username = scanner.nextLine();
    11. System.out.println("请输入你的身份:1 - 管理员,0 - 普通用户");
    12. int choice = scanner.nextInt();
    13. if(choice == 1){
    14. return new AdminUser(username);
    15. }else{
    16. return new NormalUser(username);
    17. }
    18. }
    19. public static void main(String[] args) {
    20. //准备数据
    21. BookList bookList = new BookList();
    22. //登录
    23. User user = login();
    24. while(true) {
    25. int choice = user.menu();
    26. user.doOperation(choice, bookList);
    27. }
    28. }
    29. }

    User

    1. package user;
    2. import book.BookList;
    3. import operation.IOperation;
    4. public abstract class User {
    5. protected String name;
    6. protected IOperation[] ioperation;//只是定义了一个数组,没有初始化和分配内存
    7. public User(String name){
    8. this.name = name;
    9. }
    10. public abstract int menu();
    11. public void doOperation(int choice, BookList bookList){
    12. ioperation[choice].work(bookList);
    13. }
    14. }

    AdminUser

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

    NormalUser

    1. package user;
    2. import operation.*;
    3. import java.util.Scanner;
    4. public class NormalUser extends User {
    5. public NormalUser(String name){
    6. super(name);
    7. this.ioperation = 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("0.退出系统");
    18. System.out.println("1.查找图书");
    19. System.out.println("2.借阅图书");
    20. System.out.println("3.归还图书");
    21. System.out.println("***********************************");
    22. System.out.println("请输入你的操作:");
    23. Scanner scanner = new Scanner(System.in);
    24. int choice = scanner.nextInt();
    25. return choice;
    26. }
    27. }

    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;//是否被借出
    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. @Override
    15. public String toString() {
    16. return "Book{" +
    17. "name='" + name + '\'' +
    18. ", author='" + author + '\'' +
    19. ", price=" + price +
    20. ", type='" + type + '\'' +
    21. ((isBorrowed == true)?"已借出":"未借出")+
    22. '}';
    23. }
    24. public String getName() {
    25. return name;
    26. }
    27. public void setName(String name) {
    28. this.name = name;
    29. }
    30. public String getAuthor() {
    31. return author;
    32. }
    33. public void setAuthor(String author) {
    34. this.author = author;
    35. }
    36. public int getPrice() {
    37. return price;
    38. }
    39. public void setPrice(int price) {
    40. this.price = price;
    41. }
    42. public String getType() {
    43. return type;
    44. }
    45. public void setType(String type) {
    46. this.type = type;
    47. }
    48. public boolean isBorrowed() {
    49. return isBorrowed;
    50. }
    51. public void setBorrowed(boolean borrowed) {
    52. isBorrowed = borrowed;
    53. }
    54. }

    BookList

    1. package book;
    2. public class BookList {
    3. Book[] books = new Book[10];
    4. private int usedsize = 10;
    5. //预先存放几本书
    6. public BookList(){
    7. books[0] = new Book("三国演义","罗贯中",50,"小说");
    8. books[1] = new Book("西游记","吴承恩",40,"小说");
    9. books[2] = new Book("红楼梦","曹雪芹",50,"小说");
    10. usedsize = 3;
    11. }
    12. public int getUsedsize() {
    13. return usedsize;
    14. }
    15. public void setUsedsize(int usedsize) {
    16. this.usedsize = usedsize;
    17. }
    18. public Book getPos(int pos){
    19. return books[pos];
    20. }
    21. //存储一本书到指定的位置
    22. //当前最后可以存储的位置
    23. public void setBooks(Book book,int pos){
    24. books[pos] = book;
    25. }
    26. }

    FindOperation

    1. package operation;
    2. import book.Book;
    3. import book.BookList;
    4. import java.util.Scanner;
    5. public class FindOperation 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. System.out.println("找到了");
    17. System.out.println(book);
    18. return;
    19. }
    20. }
    21. System.out.println("没有这本书");
    22. }
    23. }

    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. System.out.println("请输入书的名字");
    10. Scanner scanner = new Scanner(System.in);
    11. String name = scanner.nextLine();
    12. System.out.println("请输入书的作者");
    13. String author = scanner.nextLine();
    14. System.out.println("请输入书的价格");
    15. int price = scanner.nextInt();
    16. scanner.nextLine();//读enter建
    17. System.out.println("请输入书的类型");
    18. String type = scanner.nextLine();
    19. Book book = new Book(name,author,price,type);
    20. int currentSize = bookList.getUsedsize();
    21. bookList.setBooks(book,currentSize);
    22. currentSize++;
    23. bookList.setUsedsize(currentSize);
    24. }
    25. }

    DelOperation

    1. package operation;
    2. import book.Book;
    3. import book.BookList;
    4. import java.util.Scanner;
    5. public class DelOperation 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 n = -1;
    13. int currentSize = bookList.getUsedsize();
    14. for (int i = 0; i < currentSize; i++) {
    15. Book book = bookList.getPos(i);
    16. if(name.equals(book.getName())){
    17. n = i;
    18. break;
    19. }
    20. }
    21. if(n == -1){
    22. System.out.println("没有你要删除的这本书");
    23. return;
    24. }
    25. for (int i = n; i < currentSize-1; i++) {
    26. Book book = bookList.getPos(i+1);
    27. bookList.setBooks(book,i);
    28. }
    29. //每次删除都要把最后一本书置空
    30. bookList.setBooks(null,currentSize-1);
    31. bookList.setUsedsize(currentSize-1);
    32. System.out.println("删除成功");
    33. }
    34. }

    DisplayOperation

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

    ExitOperation

    1. package operation;
    2. import book.BookList;
    3. public class ExitOperation implements IOperation{
    4. @Override
    5. public void work(BookList bookList) {
    6. System.out.println("退出系统");
    7. System.exit(0);
    8. }
    9. }

    BorrowOperation

    1. package operation;
    2. import book.Book;
    3. import book.BookList;
    4. import java.util.Scanner;
    5. public class BorrowOperation 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
    14. Book book = bookList.getPos(i);
    15. if(name.equals(book.getName())){
    16. if(book.isBorrowed() == true){
    17. System.out.println("该书已经被借出");
    18. return;
    19. }else{
    20. book.setBorrowed(true);
    21. System.out.println("借书成功");
    22. return;
    23. }
    24. }
    25. }
    26. System.out.println("没有你要借的图书");
    27. }
    28. }

    ReturnOperation

    1. package operation;
    2. import book.Book;
    3. import 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
    14. Book book = bookList.getPos(i);
    15. if(name.equals(book.getName())){
    16. if(book.isBorrowed() == true){
    17. book.setBorrowed(true);
    18. System.out.println("还书成功");
    19. return;
    20. }else{
    21. System.out.println("该书已被归还");
    22. return;
    23. }
    24. }
    25. }
    26. System.out.println("该书不是本图书馆的书,无需归还");
    27. }
    28. }

  • 相关阅读:
    API文档转实体类脚本
    API之 要求接口上传pdf 以 合同PDF的二进制数据,multpart方式上传
    Python 基于docker部署的Mysql备份查询脚本
    机器人制作开源方案 | 齿轮传动轴偏心轮摇杆简易四足
    尚硅谷大叔培训:揭秘Flink四种执行图——ExecutionGraph和物理执行图
    【MySQL】表的增删改查(一)
    rar格式转换zip格式,如何做?
    WPF 控件专题 Ellipse详解
    Android 11.0 系统Settings app详情页增加统计使用时长功能
    C# Onnx LSTR 基于Transformer的端到端实时车道线检测
  • 原文地址:https://blog.csdn.net/qq_62712350/article/details/126542909