2013-03-28 27 views
1

我正在做一個程序,給你一個菜單:如何讓一個節點保存在一個鏈接列表中,以便進行編輯和/或刪除?

1. Show all records. 
2. Delete the current record 
3. Change the first name in the current record 
4. Change the last name in the current record 
5. Add a new record 
6. Change the phone number in the current record 
7. Add a deposit to the current balance in the current record 
8. Make a withdrawal from the current record if sufficient funds are available. 
9. Select a record from the record list to become the current record. 
10. Quit 

和命令PROMT:

Enter a command from the list above (q to quit): 

我有4個鏈表:

  1. 的firstName
  2. 的lastName
  3. 電話號碼
  4. accountBalance

我敢肯定,你可以假設它們包含的內容...

假設我已經添加了一個新的記錄。

我想弄清楚如何做一個方法,可以保持選擇一個節點,因爲我做了更改或刪除它。

public void numberNine() 
{ 
    System.out.println("Enter first name: "); 
    String fName = keyboard.next(); 
    System.out.println("Enter last name: "); 
    String lName = keyboard.next(); 
    if(firstName.contains(fName)) 
    { 
     if(lastName.contains(lName)) 
     { 
      /*I need to set the current record here. 
      I also need to print out the current record. 
      That means I need to find the corresponding 
      information from the linked lists by checking 
      the index of the first or last name because 
      both share the same index position for the 
      correhlating information of the selected person 
      for the current record.*/ 
     } 
     else 
     { 
      System.out.println("No matching record found."); 
     } 
    } 
    else 
    { 
     System.out.println("No matching record found."); 
    } 
} 

的唯一的事情是,我完全不熟悉的語法來執行,以完成任務,但是從我來環顧四周後明白了,我可能需要看起來有點像方法這樣的:

public void currentRecord(String fName, String lName) 
{ 
    /*check for index of both fName and lName between the two strings containing 
     this information until they match up, then select the telenumber and 
     balance that match the index of the fName and lName and print*/ 
} 

我明白我已經找到了解釋,但一直沒有與這些解釋任何語法來協助我其實實現這個。有人能告訴我它是如何完成的嗎?

回答

1
private static void searchRecord(String firstName, String lastName) { 
     boolean recordFound = false; 
     if(fName.contains(firstName) && lName.contains(lastName)){ 
      int index = -1; 

      for (String fn : fName) { 
       index++; 
       if(fn.equals(firstName)){ 
        String ln = lName.get(index); 
        if(ln.equals(lastName)){ 
         recordFound = true; 
         System.out.println("Record Found"); 
         System.out.println("First Name="+ fName.get(index)); 
         System.out.println("Last Name="+ lName.get(index)); 
         System.out.println("Phone="+ phone.get(index)); 
         System.out.println("Balance="+ balance.get(index)); 
        } 
       } 
      } 

     } 
     if(!recordFound) { 
      System.out.println("No Record found for first name="+ firstName + " and last name="+lastName); 
     } 

    } 
+0

這是教育!不過幾小時後,我已經轉向另一個項目,以一種更有意義的不同方式來做同樣的事情。然而,這種形式使得單個字符串SOOO的更改比我最終做的要容易得多。我知道一些人會很高興知道這一點!非常感謝你 :) – 2013-03-28 12:02:45