使用链表在Java中预订应用程序

时间:2016-02-12 15:38:12

标签: java

我正在制作一个代码,其中程序显示一些选项,如插入书,删除书等。 该程序要求我们通过Insert方法输入有关书籍的某些细节。

输入详细信息后,它会再次显示选项。

当我选择搜索方法时,它会要求我按标题搜索或按ISBN搜索或按作者搜索。我选择一个选项,程序运作完美。

我制作了一个方法updateBook。它工作正常,但当我搜索我的更新信息时,它什么都没有显示。

如何解决此问题?

import java.util.Scanner;

public class BookApplication {
    static Book head, pointer;
    static Scanner scan = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.println("Welcome to Smart Book Store");
        System.out.println("Please choose an option from the list below");
        int choice = 0;
        do {

            System.out.println("1. Insert Book\n2. Delete Book\n3. Search Book\n4. Update Book\n5. View Book\n6. Exit");
            choice = scan.nextInt();
            try {
                choice = Integer.parseInt(scan.nextLine());
            } catch (Exception e) {
                switch (choice) {
                case 1:
                    addBook();
                    break;
                case 2:
                case 3:
                    searchBook();
                    break;
                case 4:
                    updateBook();
                    break;
                case 5:
                    break;
                case 6:
                    scan.close();
                    System.exit(0);
                    break;
                default:
                    System.out.println("Please choose from 1 to 5");
                    break;

                }
            }
        } while (true);
    }

    static void addBook() {
        if (head == null) {
            String details[] = enterDetails();
            pointer = new Book(details[0], details[1], details[2]);
            head = pointer;
            pointer.next = null;
        } else {
            String details[] = enterDetails();

            pointer.next = new Book(details[0], details[1], details[2]);
            pointer = (Book) pointer.next;
        }
    }

    static String[] enterDetails() {
        String[] details = new String[4];
        try {

            String title;
            String ISBN;
            String authors;

            System.out.println("Please enter Book title");
            title = scan.nextLine();

            System.out.println("Please enter ISBN of book");
            ISBN = scan.nextLine();

            System.out.println("Please enter book's Author(s)");
            authors = scan.nextLine();

            details[0] = title;
            details[1] = ISBN;
            details[2] = authors;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return details;
    }

    private static void searchBook() {
        System.out.println();
        System.out.println("1. Search by TITLE");
        System.out.println("2. Search by ISBN");
        System.out.println("3. Search by AUTHOR");

        int choice = 0;
        choice: try {
            choice = Integer.parseInt(scan.nextLine());
        } catch (Exception e) {
            System.out.println();
            System.out.println("PLEASE ENTER VALUE BETWEEN 1 - 3");
            break choice;
        }

        switch (choice) {
        case 1:
            System.out.println("Please enter Title of BOOK");
            String title = scan.nextLine();
            if (head == null) {
                System.out.println("List is EMPTY !");
                return;
            } else {
                System.out.println();
                System.out.println("BOOK(S) IN THE SYSTEM ARE: ");
                System.out.println();
                pointer = head;
                while (pointer != null) {
                    if (pointer.title.equals(title)) {
                        System.out.println(pointer.getBook());

                    }
                    pointer = (Book) pointer.next;
                }
            }
            break;
        case 2:
            System.out.println("Please enter ISBN of BOOK");
            String ISBN = scan.nextLine();
            if (head == null) {
                System.out.println("List is EMPTY !");
                return;
            } else {
                System.out.println();
                System.out.println("BOOK(S) IN THE SYSTEM ARE: ");
                System.out.println();
                pointer = head;
                while (pointer != null) {
                    if (pointer.ISBN.equals(ISBN)) {
                        System.out.println(pointer.getBook());
                        break;
                    }
                    pointer = (Book) pointer.next;
                }
            }
            break;
        case 3:
            System.out.println("Please enter Author(s) of BOOK");
            String authors = scan.nextLine();
            if (head == null) {
                System.out.println("List is EMPTY !");
                return;
            } else {
                System.out.println();
                System.out.println("BOOK(S) IN THE SYSTEM ARE: ");
                System.out.println();
                pointer = head;
                while (pointer != null) {
                    if (pointer.authors.contains(authors)) {
                        System.out.println(pointer.getBook());
                        break;
                    }
                    pointer = (Book) pointer.next;
                }
            }
            break;
        default:
            System.out.println("PLEASE ENTER VALUE BETWEEN 1 - 5");
        }

    }

    static void updateBook() {
        System.out.println();
        System.out.println("1. Update TITLE");
        System.out.println("2. Update ISBN");
        System.out.println("3. Update AUTHOR");

        int choice = 0;
        choice: try {
            choice = Integer.parseInt(scan.nextLine());
        } catch (Exception e) {
            System.out.println();
            System.out.println("PLEASE ENTER VALUE BETWEEN 1 - 3");
            break choice;
        }
        switch (choice) {
        case 1:
            System.out.println("Please update Title of BOOK");
            String another1 = scan.nextLine();
            if (head == null) {
                System.out.println("Title not Updated !");
                return;
            } else {
                System.out.println();
                System.out.println("Your new title is: " + another1);
                System.out.println();
                pointer = head;
                while (pointer != null) {
                    if (pointer.title.equals(another1)) {
                        System.out.println(pointer.getBook());
                        break;
                    }
                    pointer = (Book) pointer.next;
                }
            }
        case 2:

            System.out.println("Please update ISBN of BOOK");
            String ISBN = scan.nextLine();
            if (head == null) {
                System.out.println("Isbn not updated !");
                return;
            } else {
                System.out.println();
                int aISBN = Integer.parseInt(ISBN.trim());

                System.out.println("Your book's updated ISBN is: " + aISBN);
                System.out.println();
                pointer = head;
                while (pointer != null) {
                    if (pointer.ISBN.equals(aISBN)) {
                        System.out.println(pointer.getBook());
                        break;
                    }
                    pointer = (Book) pointer.next;
                }
            }
        case 3:
            System.out.println("Please enter Author(s) of BOOK");

            String upauthor1 = scan.nextLine();
            if (head == null) {
                System.out.println("List is EMPTY !");
                return;
            } else {
                System.out.println();
                System.out.println("Book's Updated author is: " + upauthor1);

                System.out.println();
                pointer = head;
                while (pointer != null) {
                    if (pointer.authors.contains(upauthor1)) {
                        System.out.println(pointer.getBook());

                        break;
                    }
                    pointer = (Book) pointer.next;

                }
                break;
            }
        }
    }
}

这是我的另一堂课。

public class Book {
    String authors;

    final String ISBN;
    final String title;
    public Object next;

    Book(String title, String ISBN, String authors) {
        this.title = title;

        this.authors = authors;
        this.ISBN = ISBN ;
    }

    public String getBook() {
        return "Book Title: " + this.title + "\n" + "Book ISBN: " + this.ISBN + "\n" + "Book Authors: " + this.authors + "\n" ;
    }
}

1 个答案:

答案 0 :(得分:0)

那是因为你不保存新数据。很明显,因为您可以添加一本书并对其进行更新。如果您搜索更新的信息,则没有任何内容,但如果您搜索旧版本(预更新),那么它就是。

pointer = head;
while (pointer != null) {
    if (pointer.title.equals(another1)) {
        System.out.println(pointer.getBook());
        break;
    }
    pointer = (Book) pointer.next;
}

您只需查看列表即可完成更改。

您首先应该询问书籍信息,搜索是否存在,然后询问新数据。