2016-02-14 125 views
-2

我正在製作一個程序,其中程序顯示一些選項,如插入書籍,刪除書籍等。該程序要求我們通過插入方法輸入關於書籍的某些細節。使用鏈接列表的Java程序

我已經提出了每種方法,但我對如何製作刪除方法沒有任何意見。這是我在課堂上的任務。首先,我在編程方面非常弱,特別是在鏈接列表中。因此,有關刪除方法的任何幫助將不勝感激。這裏是我的代碼...

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(); 
       viewBook(); 
       break; 
      case 5: 
       viewBook(); 
       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 = 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.getTitle().equals(title)) { 
        System.out.println(pointer.getBook()); 

       } 
       pointer = 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.getISBN().equals(ISBN)) { 
        System.out.println(pointer.getBook()); 
        break; 
       } 
       pointer = 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.getAuthors().contains(authors)) { 
        System.out.println(pointer.getBook()); 
        break; 
       } 
       pointer = pointer.next; 
      } 
     } 
     break; 
    default: 
     System.out.println("PLEASE ENTER VALUE BETWEEN 1 - 5"); 
    } 

} 

static void updateBook() { 
    System.out.println(); 
    System.out.println("1. Update by TITLE"); 
    System.out.println("2. Update by ISBN"); 
    System.out.println("3. Update 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 provide the Title of Book you want to update: "); 
     String another1 = scan.nextLine(); 
     if (head == null) { 
      System.out.println("No Books to Update"); 
      return; 
     } else { 
      Boolean found = false; 

      pointer = head; 
      while (!found & pointer != null) { 
       if (pointer.getTitle().equals(another1)) { 
        found = true; 
        System.out.println("Here is the current book:" + pointer.getBook()); 
        System.out.println("Please enter the new Title:"); 
        pointer.setTitle(scan.nextLine()); 
        System.out.println("Please Enter the new ISBN:"); 
        pointer.setISBN(scan.nextLine()); 
        System.out.println("Please Enter the new Author:"); 
        pointer.setAuthors(scan.nextLine()); 
       } 
       pointer = pointer.next; 
      } 
     } 

     break; 
    case 2: 

     System.out.println("Please provide the ISBN of Book you want to update: "); 
     String ISBN = scan.nextLine(); 
     if (head == null) { 
      System.out.println("No Books to Update!"); 
      return; 
     } else { 
      Boolean found = false; 
      pointer = head; 
      while (!found && pointer != null) { 
       if (pointer.getISBN().equals(ISBN)) { 
        found = true; 
        System.out.println("Here is the current book:" + pointer.getBook()); 
        System.out.println("Please enter the new Title:"); 
        pointer.setTitle(scan.nextLine()); 
        System.out.println("Please Enter the new ISBN:"); 
        pointer.setISBN(scan.nextLine()); 
        System.out.println("Please Enter the new Author:"); 
        pointer.setAuthors(scan.nextLine()); 
       } 
       pointer = pointer.next; 
      } 
     } 
    case 3: 
     System.out.println("Please enter Author(s) of the Book you want to Update: "); 

     String upauthor1 = scan.nextLine(); 
     if (head == null) { 
      System.out.println("List is EMPTY !"); 
      return; 
     } else { 
      Boolean found = false; 
      pointer = head; 
      while (!found && pointer != null) { 
       if (pointer.getAuthors().contains(upauthor1)) { 
        found = true; 
        System.out.println("Here is the current book:" + pointer.getBook()); 
        System.out.println("Please enter the new Title:"); 
        pointer.setTitle(scan.nextLine()); 
        System.out.println("Please Enter the new ISBN:"); 
        pointer.setISBN(scan.nextLine()); 
        System.out.println("Please Enter the new Author:"); 
        pointer.setAuthors(scan.nextLine()); 
       } 
       pointer = pointer.next; 

      } 
      break; 
     } 
    } 

} 

private static void viewBook() { 

    Book element = head; 

    System.out.println(); 
    System.out.println("Printing List"); 

    while (element != null) { 
     System.out.println(element.getBook()); 
     element = element.next; 
    } 

    System.out.println("Book Printing Ended"); 
    System.out.println(); 
} 

private static void deleteBook() { 
    System.out.println(); 
    System.out.println("1. Delete by Title"); 
    System.out.println("2. Delete by ISBN"); 
    System.out.println("3. Delete by Author(s)"); 


     } 


} 

正如你可以看到我已經離開了刪除方法的空白,因爲我不知道如何刪除它插在編譯的開始特定的書。

這裏是我的書類

public class Book { 


private String authors; 
private String ISBN; 
private String title; 
public Book next; 

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

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



public String getAuthors() { 
    return authors; 
} 

public void setISBN(String ISBN){ 
    this.ISBN = ISBN; 
} 

public void setTitle(String title){ 
    this.title = title; 
} 

public void setAuthors(String authors) { 
    this.authors = authors; 
} 



public String getISBN() { 
    return ISBN; 
} 



public String getTitle() { 
    return title; 
} 



public String getBook() { 
    return "Book [authors=" + authors + ", ISBN=" + ISBN + ", title="+ title + "]"; 
} 

}

+0

你有一些錯誤? – Abdelhak

+0

我沒有在提供的代碼中看到你的「鏈接列表」,但基本上你會發現要刪除的書的索引並調用'list.remove(index)' –

+0

我沒有任何錯誤。我只想實現一種刪除方法來刪除任何插入的書籍@Abdelhak –

回答

1

1.定義當前指針的前一指針。

2.remember改變先前的指針時,而週期

3.change先前的指針的未來和當前指針的下一個對手時(刪除指針是或不是頭)

private static void deleteBook() { 
    System.out.println(); 
    System.out.println("1. Delete by Title"); 
    System.out.println("2. Delete by ISBN"); 
    System.out.println("3. Delete by Author(s)"); 

    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 { 
      pointer = head; 
      //define Previous of the current pointer 
      Book prePointer = null; 
      while (pointer != null) { 
       if (pointer.getTitle().equals(title)) { 
        //delete book 
        if(prePointer == null){ 
         //delete head 
         head = pointer.next; 
        }else{ 
         prePointer.next = pointer.next; 
        } 

      break; 
       } 
       pointer = pointer.next; 
       prePointer = pointer; 
      } 
     } 
     break; 
    } 

} 
+0

你發送的程序是一個有點兒車。當我插入一本書,然後嘗試通過選擇刪除方法刪除,輸出如下 1.按標題刪除 2.按ISBN刪除 3.刪除作者 1.插入圖書 2.刪除圖書 3.搜索簿 4.更新書 5.查看書 6.退出 –