import java.util.Arrays; import java.util.Scanner; public class ArrayBook2 { public static void main(String[] args) { //扫描仪 Scanner scanner = new Scanner(System.in); //图书名称 String[] bookNames = {"《西游记》", "《红楼梦》", "《水浒传》", "《三国演义》"}; //图书作者 String[] bookAuthor = {"吴承恩", "曹雪芹", "施耐庵", "罗贯中"}; //图书价格 float[] bookPrice = {100, 80, 90, 101}; 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: System.out.println("图书管理系统>>>添加图书"); while (true) { System.out.println("请输入添加的图书名称:"); String inputName = scanner.next(); System.out.println("请输入" + inputName + "的作者"); String inputAuthor = scanner.next(); System.out.println("请输入" + inputName + "的价格"); float inputPrice = scanner.nextFloat(); //先扩容 bookNames = Arrays.copyOf(bookNames, bookNames.length + 1); bookAuthor = Arrays.copyOf(bookAuthor, bookAuthor.length + 1); bookPrice = Arrays.copyOf(bookPrice, bookPrice.length + 1); //将新的图书存入到最后一个位置 bookNames[bookNames.length - 1] = inputName; bookAuthor[bookAuthor.length - 1] = inputAuthor; bookPrice[bookPrice.length - 1] = inputPrice; System.out.println("[添加图书成功!]"); //是否继续 System.out.println("是否继续添加?(Y/N)"); String isContinue = scanner.next(); if ("n".equals(isContinue) || "N".equals(isContinue)) { break; } } break; case 2: System.out.println("图书管理系统>>>修改图书"); System.out.println("请输入要修改的图书名称:"); String inputUpdateName = scanner.next(); //根据图书名称查找对应的索引 int index = -1; for (int i = 0; i < bookNames.length; i++) { if (inputUpdateName.equals(bookNames[i])) { index = i; break; } } //先判断 if (index == -1) { System.out.println("[修改失败,该图书不存在!]"); } else { //将输入新的图书信息覆盖原有的信息 System.out.println("原图书名称:" + bookNames[index] + "\t请输入新的图书名称:"); bookNames[index] = scanner.next(); System.out.println("原图书作者:" + bookAuthor[index] + "\t请输入新的图书作者:"); bookAuthor[index] = scanner.next(); System.out.println("原图书价格:" + bookPrice[index] + "\t请输入新的图书价格:"); bookPrice[index] = scanner.nextFloat(); System.out.println("[修改图书成功!]"); } break; case 3: System.out.println("图书管理系统>>>删除图书"); System.out.println("请输入要删除的图书名称:"); String inputDeleteName = scanner.next(); //根据图书名称查找对应的索引 int deleteIndex = -1; for (int i = 0; i < bookNames.length; i++) { if (inputDeleteName.equals(bookNames[i])) { deleteIndex = i; } } //先判断 if (deleteIndex == -1) { System.out.println("[删除失败,该图书不存在!]"); } else { //循环覆盖 for (int i = deleteIndex; i < bookNames.length - 1; i++) { //将后一个元素覆盖前一个 bookNames[i] = bookNames[i + 1]; bookAuthor[i] = bookAuthor[i + 1]; bookPrice[i] = bookPrice[i + 1]; } //缩容 bookNames = Arrays.copyOf(bookNames, bookNames.length - 1); bookAuthor = Arrays.copyOf(bookAuthor, bookAuthor.length - 1); bookPrice = Arrays.copyOf(bookPrice, bookPrice.length - 1); System.out.println("[删除图书成功!]"); } break; case 4: System.out.println("图书管理系统>>>查看图书"); System.out.println("序号\t图书名称\t作者\t价格"); for (int i = 0; i < bookNames.length; i++) { System.out.println((i + 1) + "\t" + bookNames[i] + "\t" + bookAuthor[i] + "\t" + bookPrice[i]); } break; case 0: System.out.println("[谢谢使用,系统已退出!]"); b = false; break; } } } }
标签:管理系统,bookAuthor,基础,System,println,bookNames,图书,out From: https://www.cnblogs.com/19981206-zxb/p/16892372.html