首页 > 其他分享 >用一维数组来实现增删改查

用一维数组来实现增删改查

时间:2022-11-15 18:00:58浏览次数:63  
标签:repeatIndex 改查 System println 一维 增删 bookNames 图书 out

private static void libraryDemo() {
        String[] bookNames = {"《西游记》", "《红楼梦》", "《水浒传》", "《三国演义》"};
        String[] bookAuthor = {"吴承恩", "曹雪芹", "施耐庵", "罗贯中"};
        double[] bookPrice = {50, 45, 30, 55};
        Study study = new Study();
        String inputName;
        int repeatIndex;
        boolean flag = true;
        while (flag) {
            System.out.println("1、添加图书");
            System.out.println("2、修改图书");
            System.out.println("3、删除图书");
            System.out.println("4、查看图书");
            System.out.println("0、退出系统");
            int choice = InputUtil.intRegex("请选择您要使用的功能:", "^[0-4]{1}");
            switch (choice) {
                case 1:
                    System.out.println("-->添加图书");
                    String answer;
                    do {
                        System.out.println("请输入需要添加的图书名称:");
                        inputName = input.next();
                        repeatIndex = study.repeat(inputName, bookNames);
                        if (repeatIndex == -1) {
                            bookNames = Arrays.copyOf(bookNames, bookNames.length + 1);
                            bookNames[bookNames.length - 1] = inputName;
                            System.out.println("请输入" + inputName + "的作者");
                            bookAuthor = Arrays.copyOf(bookAuthor, bookNames.length);
                            bookAuthor[bookAuthor.length - 1] = input.next();
                            System.out.println("请输入" + inputName + "的价格");
                            bookPrice = Arrays.copyOf(bookPrice, bookNames.length);
                            bookPrice[bookPrice.length - 1] = input.nextDouble();
                            System.out.println("添加成功");
                        } else System.out.println("图书已存在!");
                        System.out.println("请问是否继续添加图书?y/n");
                        answer = input.next();
                    } while ("y".equals(answer) || "Y".equals(answer));
                    break;
                case 2:
                    System.out.println("-->修改图书");
                    System.out.println("请输入需要修改的图书名称");
                    inputName = input.next();
                    repeatIndex = study.repeat(inputName, bookNames);
                    if (repeatIndex == -1) {
                        System.out.println("图书不存在!请重新输入");
                    } else {
                        System.out.println("请输入修改之后的图书名称");
                        bookNames[repeatIndex] = input.next();
                        System.out.println("请输入" + bookNames[repeatIndex] + "的作者");
                        bookAuthor[repeatIndex] = input.next();
                        System.out.println("请输入" + bookNames[repeatIndex] + "的价格");
                        bookPrice[repeatIndex] = input.nextDouble();
                        System.out.println("修改成功!");
                    }
                    break;
                case 3:
                    System.out.println("-->删除图书");
                    System.out.println("请输入需要删除的图书名称");
                    inputName = input.next();
                    repeatIndex = study.repeat(inputName, bookNames);
                    if (repeatIndex == -1)
                        System.out.println("图书不存在");
                    else {
                        for (int i = repeatIndex; 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, bookNames.length - 1);
                        bookPrice = Arrays.copyOf(bookPrice, bookNames.length - 1);
                        System.out.println("删除成功!");
                    }
                    break;
                case 4:
                    System.out.println("-->查看图书");
                    System.out.println("图书名称\t  作者名\t图书价格");
                    for (int i = 0; i < bookAuthor.length; i++) {
                        System.out.print(bookNames[i] + "\t");
                        System.out.print(bookAuthor[i] + "\t");
                        System.out.print(bookPrice[i] + "\t");
                        System.out.println();
                    }
                    break;
                case 0:
                    System.out.print("-->退出系统");
                    flag = false;
                    break;
            }

        }
    }

    private int repeat(String name, String[] bookNames) {
        int index = 0;
        for (String bookName : bookNames) {

            if (bookName.equals(name)) {
                return index;//相同书籍的索引值
            }
            index++;
        }
        return -1;//不存在这本书
    }

标签:repeatIndex,改查,System,println,一维,增删,bookNames,图书,out
From: https://www.cnblogs.com/Liku-java/p/16893354.html

相关文章