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

和一个命令提示符:

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

我有 4 个链接列表:

  1. 电话号码
  2. 账户余额

我相信你可以假设它们包含什么......

假设我已经添加了一条新记录。

我试图弄清楚如何制作一种方法,在我进行更改或删除它时保持选中节点。

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*/
}

我已经理解了我找到的解释,但是这些解释没有任何语法来帮助我实际实现这一点。有人可以告诉我它是如何完成的吗?

4

1 回答 1

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);
        }

    }
于 2013-03-28T03:50:48.897 回答