从面向过程到面向对象来进行增删改查

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;//不存在这本书
    }

下面我们用面向对象的形式来重新写这个功能:(一些方法我用的是lombok插件里面的注解写的,不清楚的可以看我之前的博客)

分析业务,实体层—>图书,操作层—>使用图书馆,业务层—>增删改查

实体对象类:

@Setter
@Getter
@ToString
public class Book {
    private int id;
    private String name;
    private double price;
    private String author;
    private String publisher;
    private static int identity = 1000;

    public Book(String name, double price, String author, String publisher) {
        id=identity++;
        this.name = name;
        this.price = price;
        this.author = author;
        this.publisher = publisher;
    }

    public Book() {
        id = identity++;
    }
}

操作层:

public class BookAction {
    private static Scanner input;
    private static BookService bookService;
    private static Book[] books = new Book[3];

    static {
        books[0] = new Book("西游记", 36, "liku", "出版社");
        books[1] = new Book("红楼梦", 55, "liku", "出版社");
        books[2] = new Book("三国演义", 78, "liku", "出版社");
    }
    public static void startMenu() {
        bookService = new BookService();
        input = new Scanner(System.in);
        boolean b = true;
        while (b) {
            System.out.println("欢迎来到liku图书馆,请选择您需要进行的操作:");
            System.out.println("1、查看图书");
            System.out.println("2、修改图书");
            System.out.println("3、删除图书");
            System.out.println("4、增加图书");
            System.out.println("0、退出系统");
            int choice = input.nextInt();
            switch (choice) {
                case 1:
                    bookService.show(books);
                    break;
                case 2:
                    bookService.update(books);
                    break;
                case 3:
                    books= bookService.minus(books);
                    break;
                case 4:
                    books=bookService.add(books);
                    break;
                case 0:
                    System.out.println("谢谢惠顾!欢迎下次光临!");
                    b = false;
                    break;
            }
        }
    }
}

业务层:

public class BookService {
    private static Scanner input = new Scanner(System.in);
    public void show(Book[] books) {
        System.out.println("现有图书信息如下:");
        for (Book book : books) {
            System.out.println(book);
        }
    }
    public Book[] add(Book[] books) {
        Book book = new Book();
        System.out.println("请输入需要添加图书名称:");
        book.setName(input.next());
        System.out.println("请输入需要添加图书的价格:");
        book.setPrice(input.nextDouble());
        System.out.println("请输入需要添加图书的作者:");
        book.setAuthor(input.next());
        System.out.println("请输入需要添加图书的出版社:");
        book.setPublisher(input.next());
        books = Arrays.copyOf(books, books.length + 1);
        books[books.length - 1] = book;
        return books;
    }

    public Book[] minus(Book[] books) {
        System.out.println("请输入需要删除的图书名称:");
        String name = input.next();
        int index = exit(name,books);
        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;
    }

    private int exit(String name,Book[] books) {
        int index;
        for (int i = 0; i < books.length; i++) {
            if (books[i].getName().equals(name)) {
                index = i;
                return index;
            }
        }
        return -1;
    }
    public void update(Book[] books) {
        System.out.println("请输入需要修改的图书名称:");
        String name = input.next();
        int index = exit(name,books);
        if (index == -1) {
            System.out.println("修改失败!图书不存在");
            return;
        }
        Book book=books[index];//因为没有new,因此是将地址里面的值给更改,是有效的
        System.out.println("请输入修改后的图书名称:");
        book.setName(input.next());
        System.out.println("请输入修改后的图书价格:");
        book.setPrice(input.nextDouble());
        System.out.println("请输入修改后的图书作者:");
        book.setAuthor(input.next());
        System.out.println("请输入修改后的图书出版社:");
        book.setPublisher(input.next());
    }
}

利用面向对象,我们可以清晰的将每个过程独立开来,代码就变得很清晰,代码还是要多敲,才会理解并掌握

posted @ 2022-11-15 17:53  Liku007  阅读(31)  评论(0编辑  收藏  举报