Book实体类
package com.javasm.book.bean; import lombok.*; @Setter @Getter @AllArgsConstructor @NoArgsConstructor @ToString public class Book { //编号 private int bookId; //名称 private String bookName; //作者 private String bookAuthor; //价格 private float bookPrice; //出版社 private String bookPublish; //图书自增编号 public static int identityID = 1000; /** * 有参构造 * * @param bookName * @param bookAuthor * @param bookPrice * @param bookPublish */ public Book(String bookName, String bookAuthor, float bookPrice, String bookPublish) { this.bookId = identityID++; this.bookName = bookName; this.bookAuthor = bookAuthor; this.bookPrice = bookPrice; this.bookPublish = bookPublish; } }
BookAction:菜单列表,控制程序的走向
package com.javasm.book.action; import com.javasm.book.bean.Book; import com.javasm.book.service.BookService; import java.util.Scanner; public class BookAction { //扫描仪 private static Scanner scanner = new Scanner(System.in); //业务对象 private static BookService bookService = new BookService(); //多本图书 private static Book[] books = new Book[5]; static { System.out.println("图书正在初始化~"); books[0] = new Book("《西游记》", "吴承恩", 100, "清华大学出版社"); books[1] = new Book("《西游记后传》", "钱雁秋", 50, "清华大学出版社"); books[2] = new Book("《三国演义》", "罗贯中", 99, "清华大学出版社"); books[3] = new Book("《水浒传》", "施耐庵", 88, "清华大学出版社"); books[4] = new Book("《红楼梦》", "曹雪芹", 120, "清华大学出版社"); } /** * 启动菜单 */ public static void startMenu() { boolean b = true; while (b) { System.out.println("***************欢迎你使用XXX图书管理系统****************"); System.out.println("\t\t1、查看图书"); System.out.println("\t\t2、添加图书"); System.out.println("\t\t3、删除图书"); System.out.println("\t\t4、修改图书"); System.out.println("\t\t0、退出系统"); System.out.println("请选择:"); int choose = scanner.nextInt(); switch (choose) { case 1: bookService.showBook(books); break; case 2: books = bookService.addBook(books, scanner); break; case 3: books = bookService.deleteBook(books, scanner); break; case 4: bookService.updateBook(books, scanner); break; case 0: System.out.println("[谢谢使用,系统已退出!]"); b = false; break; } } } }
BookService:业务层
package com.javasm.book.service; import com.javasm.book.bean.Book; import java.util.Arrays; import java.util.Scanner; public class BookService { /** * 查看图书 * * @param books */ public void showBook(Book[] books) { System.out.println("图书管理系统>>>查看图书"); for (Book book : books) { System.out.println(book); } } /** * 添加图书 * * @param books * @param scanner * @return */ public Book[] addBook(Book[] books, Scanner scanner) { System.out.println("图书管理系统>>>添加图书"); //准备新书的基本信息 Book book = new Book(); book.setBookId(Book.identityID++); //自增id System.out.println("请输入图书的名称:"); book.setBookName(scanner.next()); System.out.println("请输入图书的作者:"); book.setBookAuthor(scanner.next()); System.out.println("请输入图书的价格:"); book.setBookPrice(scanner.nextFloat()); System.out.println("请输入图书的出版社:"); book.setBookPublish(scanner.next()); //先扩容 books = Arrays.copyOf(books, books.length + 1); //将新书存入到最后一个索引位置 books[books.length - 1] = book; System.out.println("[添加图书成功!]"); return books; } /** * 删除图书 * * @param books * @param scanner * @return */ public Book[] deleteBook(Book[] books, Scanner scanner) { System.out.println("图书管理系统>>>删除图书"); System.out.println("请输入你要删除的图书编号:"); int inputId = scanner.nextInt(); //根据图书编号查找对应的索引位置 int index = getIndexById(books, inputId); //先判断 if (index == -1) { System.out.println("[删除失败,该图书不存在!]"); return books; } //循环覆盖 for (int i = index; i < books.length - 1; i++) { //将后一个元素覆盖前一个 books[i] = books[i + 1]; } //缩容 books = Arrays.copyOf(books, books.length - 1); System.out.println("[删除图书成功!]"); return books; } /** * 修改图书 * * @param books * @param scanner */ public void updateBook(Book[] books, Scanner scanner) { System.out.println("图书管理系统>>>修改图书"); System.out.println("请输入要修改的图书编号:"); int inputId = scanner.nextInt(); //根据图书编号查找对应的索引位置 int index = getIndexById(books, inputId); //先判断 if (index == -1) { System.out.println("[修改失败,该图书不存在!]"); return; } //允许修改 Book updateBook = books[index]; //对象是引用数据类型,引用数据类型传递的是内存地址! // System.out.println("updateBook -->" + updateBook); //修改的就是原地址中的数据 System.out.println("原图书名称:" + updateBook.getBookName() + "\t请输入修改之后的图书名称:"); updateBook.setBookName(scanner.next()); System.out.println("原图书作者:" + updateBook.getBookAuthor() + "\t请输入修改之后的图书作者:"); updateBook.setBookAuthor(scanner.next()); System.out.println("原图书价格:" + updateBook.getBookPrice() + "\t请输入修改之后的图书价格:"); updateBook.setBookPrice(scanner.nextFloat()); System.out.println("原图书出版社:" + updateBook.getBookPublish() + "\t请输入修改之后的图书出版社:"); updateBook.setBookPublish(scanner.next()); System.out.println("[修改图书成功!]"); } /** * 根据编号查找图书对应的索引 * * @param books * @param inputId * @return */ private int getIndexById(Book[] books, int inputId) { int index = -1; for (int i = 0; i < books.length; i++) { if (inputId == books[i].getBookId()) { index = i; } } return index; } }
BookTest:启动程序
package com.javasm.book.test; import com.javasm.book.action.BookAction; public class BookTest { /** * 程序的入口 * * @param args */ public static void main(String[] args) { //启动菜单 BookAction.startMenu(); } }
标签:封装,管理,Book,System,books,println,图书,out From: https://www.cnblogs.com/19981206-zxb/p/16905002.html