• JavaSE——图书管理


    ced485cbb11e458d81a746890b32cf3f.gif

    作者:敲代码の流川枫

    博客主页:流川枫的博客

    专栏:和我一起学java

    语录:Stay hungry stay foolish

    工欲善其事必先利其器,给大家介绍一款超牛的斩获大厂offer利器——牛客网

    点击免费注册和我一起刷题吧  

    文章目录

    前言

    book

    Book

    BookList

    operation

    IOperation

    AddOperation

    BorrowOperation

    DelOperation

    DisplayOperation

    ExitOperation

    FindOperation

    ReturnOperation

    user

    User

    AdminUser

    NormalUser

    Main


    前言

    面向对象的思想找到并且抽象出图书管理所需要的对象,再分析这些类和对象应该具有哪些属性和方法,对象通过“属性”和“方法”来分别对应事物所具有的静态属性和动态属性,最后分析类和类之间具体有什么关系,通过各个对象间的交互,使用对象、类、继承、封装、接口等基本概念完成序设计 

    抽象出来的对象有:书,书架,用户等,分别建立三个包管理 

    book

    Book

    1. package book;
    2. public class Book {
    3. //书名
    4. private String name;
    5. //作者
    6. private String author;
    7. //价格
    8. private int price;
    9. //类型
    10. private String type;
    11. //状态
    12. private boolean isBorrow;
    13. public Book(String name, String author, int price, String type) {
    14. this.name = name;
    15. this.author = author;
    16. this.price = price;
    17. this.type = type;
    18. }
    19. @Override
    20. public String toString() {
    21. return "Book{" +
    22. "name='" + name + '\'' +
    23. ", author='" + author + '\'' +
    24. ", price=" + price +
    25. ", type='" + type + '\'' +
    26. ((isBorrow==true)?"已借出":"未借出")+
    27. '}';
    28. }
    29. public String getName() {
    30. return name;
    31. }
    32. public void setName(String name) {
    33. this.name = name;
    34. }
    35. public String getAuthor() {
    36. return author;
    37. }
    38. public void setAuthor(String author) {
    39. this.author = author;
    40. }
    41. public int getPrice() {
    42. return price;
    43. }
    44. public void setPrice(int price) {
    45. this.price = price;
    46. }
    47. public String getType() {
    48. return type;
    49. }
    50. public void setType(String type) {
    51. this.type = type;
    52. }
    53. public boolean isBorrow() {
    54. return isBorrow;
    55. }
    56. public void setBorrow(boolean borrow) {
    57. isBorrow = borrow;
    58. }
    59. }

    BookList

    先存储三本书:

    1. package book;
    2. public class BookList {
    3. private Book[] books = new Book[10];
    4. private int usedSize;//存储当前书的个数
    5. public BookList() {
    6. books[0] = new Book("三国演义","罗贯中",90,"小说");
    7. books[1] = new Book("红楼梦","曹雪芹",80,"小说");
    8. books[2] = new Book("水浒传","施耐庵",70,"小说");
    9. this.usedSize = 3;
    10. }
    11. public int getUsedSize() {
    12. return usedSize;
    13. }
    14. public void setUsedSize(int usedSize) {
    15. this.usedSize = usedSize;
    16. }
    17. public Book getPos(int pos){
    18. return books[pos];
    19. }
    20. //存储一本书到指定的位置
    21. public void setBooks(Book book,int pos){
    22. books[pos] = book;
    23. }
    24. }
    25. //将对数组的操作写到一个接口中

    operation

    IOperation

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

    AddOperation

    1. package operation;
    2. import book.Book;
    3. import book.BookList;
    4. import java.util.Scanner;
    5. public class AddOperation implements IOperation{
    6. //添加图书
    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. //读取回车
    17. scanner.nextLine();
    18. System.out.println("请输入类型");
    19. String type = scanner.nextLine();
    20. Book book = new Book(name,author,price,type);
    21. //获取可以存放的位置
    22. int currentSize = bookList.getUsedSize();
    23. //放到指定位置
    24. bookList.setBooks(book,currentSize);
    25. //书的个数加一
    26. bookList.setUsedSize(currentSize+1);
    27. }
    28. }

    BorrowOperation

    1. package operation;
    2. import book.Book;
    3. import book.BookList;
    4. import java.util.Scanner;
    5. public class BorrowOperation implements IOperation{
    6. public void work(BookList bookList){
    7. System.out.println("借阅图书");
    8. System.out.println("请输入书名");
    9. Scanner scanner = new Scanner(System.in);
    10. String name = scanner.nextLine();
    11. //遍历是否有要借阅的书并记录下标
    12. int index = -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. if(book.isBorrow()){
    18. System.out.println("已借出");
    19. }
    20. else{
    21. book.setBorrow(true);
    22. System.out.println("借阅成功");
    23. }
    24. return;
    25. }
    26. }
    27. System.out.println("没有要借阅的书");
    28. }
    29. }

    DelOperation

    1. package operation;
    2. import book.Book;
    3. import book.BookList;
    4. import java.util.Scanner;
    5. public class DelOperation implements IOperation{
    6. public void work(BookList bookList){
    7. System.out.println("删除图书");
    8. System.out.println("请输入书名");
    9. Scanner scanner = new Scanner(System.in);
    10. String name = scanner.nextLine();
    11. //遍历是否有要删除的书并记录下标
    12. int index = -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. index = i;
    18. break;
    19. }
    20. }
    21. if(index == -1){
    22. System.out.println("没有要删除的书");
    23. return ;
    24. }
    25. for (int i = index; i < currentSize-1 ; i++) {
    26. Book book = bookList.getPos(i+1);
    27. bookList.setBooks(book,i);
    28. }
    29. bookList.setBooks(null,currentSize-1);
    30. //重置有效长度
    31. bookList.setUsedSize(currentSize-1);
    32. System.out.println("删除成功");
    33. }
    34. }

    假设有四本书,那么i+1最大值就是3,即 i < currentSize-1才不会越界

    删除书之后要置空,否则可能会出现内存泄漏
    一个对象没有人引用才会回收 

    DisplayOperation

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

    ExitOperation

    1. package operation;
    2. import book.BookList;
    3. import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
    4. public class ExitOperation implements IOperation{
    5. public void work(BookList bookList){
    6. System.out.println("退出系统");
    7. System.exit(0);
    8. }
    9. }

    FindOperation

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

    ReturnOperation

    1. package operation;
    2. import book.Book;
    3. import book.BookList;
    4. import java.util.Scanner;
    5. public class ReturnOperation implements IOperation{
    6. public void work(BookList bookList){
    7. System.out.println("归还图书");
    8. System.out.println("请输入书名");
    9. Scanner scanner = new Scanner(System.in);
    10. String name = scanner.nextLine();
    11. int index = -1;
    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.setBorrow(false);
    17. System.out.println("归还成功");
    18. return;
    19. }
    20. }
    21. System.out.println("没有要归还的书");
    22. }
    23. }

    user

    User

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

     需要写一个操作接口数组的方法doOperation()

    AdminUser

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

    NormalUser

    1. package user;
    2. import operation.*;
    3. import java.util.Scanner;
    4. //普通用户
    5. public class NormalUser extends User {
    6. public NormalUser(String name) {
    7. super(name);
    8. this.iOperations = new IOperation[]{
    9. new ExitOperation(),
    10. new FindOperation(),
    11. new BorrowOperation(),
    12. new ReturnOperation()
    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("0. 退出系统");
    22. System.out.println("*************");
    23. System.out.println("请输入你的操作:");
    24. Scanner scanner = new Scanner(System.in);
    25. int choice = scanner.nextInt();
    26. return choice;
    27. }
    28. }

    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. //利用向上转型确定是哪个对象
    8. public static User login(){
    9. System.out.println("请输入你的姓名");
    10. Scanner scanner = new Scanner(System.in);
    11. String userName = scanner.nextLine();
    12. System.out.println("请输入你的身份:1. 管理员 2. 用户");
    13. int choice = scanner.nextInt();
    14. if(choice==1){
    15. return new AdminUser(userName);
    16. }
    17. else{
    18. return new NormalUser(userName);
    19. }
    20. }
    21. public static void main(String[] args) {
    22. //先放三本书
    23. BookList bookList = new BookList();
    24. //登录
    25. User user = login();
    26. while(true){
    27. int choice = user.menu();
    28. user.doOperation(choice,bookList);
    29. }
    30. }
    31. }

    当引用的对象不同时,会打印不同的菜单

     

    “ 本期的分享就到这里了, 记得给博主一个三连哈,你的支持是我创作的最大动力!

    ced485cbb11e458d81a746890b32cf3f.gif

     

  • 相关阅读:
    pinia原理
    前端三剑客 HTML+CSS+JavaScript ⑤ HTML文本标签
    算法 | 算法是什么?深入精讲
    使用requests库解决Session对象设置超时的问题
    JavaSE 集合类详解
    JC/T 2223-2014 室内装饰装修用木塑型材检测
    mindspore训练一段时间后,报内存不够错误
    pytorch学习笔记——timm库
    035——从GUI->Client->Server->driver实现SPI控制DAC芯片
    Macbook Typora+Picgo命令行+github 图床配置
  • 原文地址:https://blog.csdn.net/chenchenchencl/article/details/126316673