-4
我正在製作一個代碼,其中程序顯示了一些選項,如插入書籍,刪除書籍等。 程序要求我們通過插入方法輸入有關書籍的某些細節。用Java編寫的使用鏈表的書籍應用程序
一旦我輸入詳細信息,它會返回並再次顯示選項。
當我選擇搜索方法時,它會要求我按標題搜索或按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" ;
}
}
是的,最終我得到了答案。謝謝... –