21--集合小案例

发布时间:2023年12月17日

案例--图书管理系统

1.创建实体类Book

package com.work.pojo;
/**
*@Author: 憨憨浩浩
*@CreateTime: 2023-12-16 17:27
*@Description: Book实体类
*/
public class Book {
    private int id;             // 编号
    private String name;        // 图书名称
    private String author;      // 图书作者
    private double price;       // 图书价格
    private String desc;        // 图书简介

    public Book() {
    }

    public Book(int id, String name, String author, double price, String desc) {
        this.id = id;
        this.name = name;
        this.author = author;
        this.price = price;
        this.desc = desc;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    @Override
    public String toString() {
        return "图书id:" + id +
                ", 图书名称:" + name +
                ", 图书作者:" + author +
                ", 图书价格:" + price +
                ", 图书简介:" + desc;
    }
}

2.创建接口BookService

package com.work.service;

import com.work.pojo.Book;

/**
 * @Author: 憨憨浩浩
 * @CreateTime: 2023-12-16 17:30
 * @Description: book实体类的业务逻辑
 */
public interface BookService {

    // 添加图书
    public boolean addBook(Book book);

    // 根据id修改图书名称
    public boolean updateBook(int id,String name);

    // 根据id删除图书
    public boolean deleteBook(int id);

    // 查询所有书籍
    public void selectBooks();
}

3.实现BookService接口

package com.work.service.impl;

import com.work.pojo.Book;
import com.work.service.BookService;

import java.util.ArrayList;

/**
*@Author: 憨憨浩浩
*@CreateTime: 2023-12-16 20:40
*@Description: BookService接口的实现类
*/
public class BookServiceImpl implements BookService {

    // 创建一个集合接收书籍信息
    private ArrayList books = new ArrayList();

    /**
     * @description: 添加图书
     * @author: 憨憨浩浩
     * @date: 2023/12/17 8:54
     * @param: [book]
     * @return: boolean
     **/
    @Override
    public boolean addBook(Book book) {
        books.add(book);
        return true;
    }

    /**
     * @description: 根据id修改图书名称
     * @author: 憨憨浩浩
     * @date: 2023/12/17 8:57
     * @param: [id, name]
     * @return: boolean
     **/
    @Override
    public boolean updateBook(int id, String name) {
        for (int i = 0; i < books.size(); i++) {
            Book book = (Book)(books.get(i));
            if (id == book.getId()){
                book.setName(name);
                return true;
            }
        }
        return false;
    }

    /**
     * @description: 根据id删除图书
     * @author: 憨憨浩浩
     * @date: 2023/12/17 9:00
     * @param: [id]
     * @return: boolean
     **/
    @Override
    public boolean deleteBook(int id) {
        for (int i = 0; i < books.size(); i++) {
            Book book = (Book)(books.get(i));
            if (id == book.getId()){
                books.remove(book);
                return true;
            }
        }
        return false;
    }

    /**
     * @description: 查询所有图书
     * @author: 憨憨浩浩
     * @date: 2023/12/17 9:00
     * @param: []
     * @return: void
     **/
    @Override
    public void selectBooks() {
        for (Object book : books) {
            System.out.println(book);
        }
    }
}

4.书写测试类

package com.work.test;

import com.work.pojo.Book;
import com.work.service.impl.BookServiceImpl;

import java.util.Scanner;

/**
*@Author: 憨憨浩浩
*@CreateTime: 2023-12-17 08:59
*@Description: 图书测试类
*/
public class BooksTest {
    public static void main(String[] args) {
        // 定义扫描器
        Scanner scan = new Scanner(System.in);
        // 定义bookServiceImpl实现类
        BookServiceImpl bookService = new BookServiceImpl();
        // 打印系统头部
        System.out.println("******************************");
        System.out.println("*********图书管理系统**********");
        System.out.println("******************************");
        while (true){
            System.out.println("1、添加图书 2、根据id修改图书名称 3、根据id删除图书信息 4、查询所有图书 0、退出");
            System.out.println("请输入你操作的编号:");
            // 定义变量接收输入的操作编号
            int num = scan.nextInt();

            switch (num){
                case 1: // 添加图书
                    System.out.println("请输入书籍id:");
                    int id = scan.nextInt();
                    System.out.println("请输入书籍名称:");
                    String name = scan.next();
                    System.out.println("请输入书籍作者:");
                    String author = scan.next();
                    System.out.println("请输入书籍价格:");
                    double price = scan.nextDouble();
                    System.out.println("请输入书籍简介:");
                    String desc = scan.next();
                    Book book = new Book(id,name,author,price,desc);
                    // 添加图书
                    if (bookService.addBook(book)){
                        System.out.println("添加成功");
                    } else {
                        System.out.println("添加失败");
                    }
                    break;
                case 2: // 根据id修改图书名称
                    System.out.println("请输入你要修改的书籍id:");
                    id = scan.nextInt();
                    System.out.println("请输入你要修改的书籍名称:");
                    name = scan.next();
                    // 根据id修改图书名称
                    if (bookService.updateBook(id,name)){
                        System.out.println("修改成功");
                    } else {
                        System.out.println("修改失败");
                    }
                    break;
                case 3: // 根据id删除图书
                    System.out.println("请输入你要删除的书籍id:");
                    id = scan.nextInt();
                    // 根据id删除图书
                    if (bookService.deleteBook(id)){
                        System.out.println("删除成功");
                    } else {
                        System.out.println("删除失败");
                    }
                    break;
                case 4: // 查询所有图书
                    // 查询所有图书
                    bookService.selectBooks();
                    break;
                case 0:
                    System.out.println("你已退出图书管理系统");
                    return;
                default:
                    System.out.println("暂无此操作,请重新输入");
                    break;
            }

        }
    }
}

5.结果展示

文章来源:https://blog.csdn.net/weixin_46612641/article/details/135049828
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。