2016-02-14 84 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.退出 –