案例--图书管理系统
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;}@Overridepublic 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**/@Overridepublic boolean addBook(Book book) {books.add(book);return true;}/*** @description: 根据id修改图书名称* @author: 憨憨浩浩* @date: 2023/12/17 8:57* @param: [id, name]* @return: boolean**/@Overridepublic 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**/@Overridepublic 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**/@Overridepublic 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.结果展示