2015-01-12 210 views
0

我需要幫助。如何將信息添加到ArrayList中已有的對象?

我有一個ArrayList完整的客戶端/客戶名稱。我想要做的是編寫一個代碼,以便它詢問我的客戶名稱,然後輸入它,如果它發現它將一個電話號碼添加到客戶端。一旦添加,如果我想打印列表,電話號碼將在該特定客戶端旁邊。

我有3個班。 1是客戶端類(具有變量 - 名稱,地址,數字)和一個JobManager(整個測試程序使用諸如添加客戶端和刪除一個的方法)。

這是我如何將客戶的樣子:

public static void addClient() { 

    System.out.println("Adding a client: \n"); 

    System.out.print("What is the name of the new client?: "); 
    String name = keyboard.nextLine(); 

    System.out.println(); 

    if (name.length() == 0) { 
     boolean invalid = true; 

     while (invalid) { 
      System.out.println("You cannot enter a blank name for the client. "); 
      System.out.print("Please enter a valid name: "); 
      name = keyboard.nextLine(); 

      if (name.length() > 0) { 
       invalid = false; 
      } 
     } 
    } 

    System.out.print("What is the client's address?: "); 
    String address = keyboard.nextLine(); 

    boolean isDuplicate = false; 

    for (int i = 0; i < clientList.size(); i++) { 
     String listName = clientList.get(i).getName(); 
     String listAddress = clientList.get(i).getAddress(); 

    if (listName.equalsIgnoreCase(name) 
       && listAddress.equalsIgnoreCase(address)) { 
      isDuplicate = true; 

     if (listName.equalsIgnoreCase(name)) { 
      isDuplicate = true; 

     } 
    } 

    if (isDuplicate) { 

     System.out.println(); 
     System.out.println("Error, that client already exists! \n"); 
     isDuplicate = false; 

    } else { 

     Client client = new Client(name, address); 
     clientList.add(client); 
     System.out.println(); 
     System.out.println("Client has been added. \n"); 

     } 
    } 
} 

而這正是我的客戶端類的樣子:

public class Client { 

    private ArrayList<JobManager> jobManager; 
    private ArrayList<TelephoneNumber> telephoneNumbers = new ArrayList<>(); 

    private String name; 
    private String address; 
    private static int number; 

    public Client(String name,String address) { 

     this.name = name; 
     this.address = address;  
    } 

    // public void createNumber() { 
    // TelephoneNumber teleNumber = new TelephoneNumber(number); 
    // telephoneNumbers.add(teleNumber); 

    //} 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getAddress() { 
     return address; 
    } 

    public void setAddress(String address) { 
     this.address = address; 
    } 

    public static int getNumber() { 
     return number; 
    } 

    public void setNumber(int number) { 
     this.number = number; 
    } 

    public String toString() { 
     String result; 

     result = name + "\n" + address; 

     result = (this.getName() + "\t" + this.getAddress()); 

     return result; 
    } 
} 

在此先感謝您的答案傢伙!

+0

你的問題是什麼? – Babel

+0

我只是不知道如何將電話號碼添加到列表中存在的特定客戶端。 – Kanox

+1

你有'Client'中的電話號碼列表,但沒有方法添加一個。你不認爲增加一種方法來將電話號碼添加到「客戶端」實例會有幫助嗎? – Tom

回答

0

我已經開發了從您的示例代碼中的一小我的硬盤enter code here編碼的電話號碼,並在主要方法是在我的測試場景中輸入用戶輸入V3作爲硬編碼2。

客戶端類

public class Client { 

private String name; 
private List<String> telephoneNumber; 

Client(String name) 
{ 
    this.name = name; 
} 

public List<String> getTelephoneNumber() { 
    return telephoneNumber; 
} 

public void setTelephoneNumber(List<String> telephoneNumber) { 
    this.telephoneNumber = telephoneNumber; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public boolean equals(Object obj) 
{ 
    Client c = (Client)obj; 
    return this.name.equals(c.name); 
} 

的TestClass

public class HelloWorld{ 
Scanner keyboard = new Scanner(System.in); 



public static void main(String[] args) throws Exception { 

    List<Client> clientList = new ArrayList<Client>(); 
    Client c1 = new Client("V1"); 
    Client c2 = new Client("V2"); 
    Client c3 = new Client("V3"); 
    Client c4 = new Client("V4"); 

    clientList.add(c1); 
    clientList.add(c2); 
    clientList.add(c3); 
    clientList.add(c4); 

    HelloWorld h = new HelloWorld(); 
    h.addTelephone(clientList); 

    Client cTest = clientList.get(2); 
    System.out.println(cTest.getTelephoneNumber()); 





} 
public void addTelephone(List<Client> clientList) 
{ 
    System.out.print("What is the name of the new client?: "); 
    String name = keyboard.nextLine(); 
    Client c = new Client(name); 
    if(clientList.contains(c)) 
    {//Client Already Exist 
     int i = clientList.indexOf(c); 
     Client c1 = clientList.get(i); 
     List<String> telNumber = new ArrayList<String>(); 
     getTelephoneNumbers(telNumber); 
     c1.setTelephoneNumber(telNumber); 
    } 

} 
} 
public void getTelephoneNumbers(List<String> telephone) 
{ 
    boolean isExit = false; 

    while(!isExit) 
    { 
     System.out.println("Enter Telephone Number or 0 for exiting : "); 
     String telp = keyboard.nextLine(); 
     if("0".equalsIgnoreCase(telp)) { 
      System.out.println("Exiting"); 
      isExit = true; 
     } 
     else { 
      telephone.add(telp); 
     } 
    } 
} 

您可以使用相同的排序addTelephone多個方法。

+0

它確實清除了一些內容,但我使用用戶輸入方式添加電話號碼。 – Kanox

+0

@Cryphisss你能告訴我們你輸入的電話號碼嗎? – robin

+0

這是我到目前爲止:http://pastebin.com/K1Fdn54V 它編譯但它不會添加任何東西。 – Kanox

0

你可以做到這一點在這裏:

if (listName.equalsIgnoreCase(name) 
      && listAddress.equalsIgnoreCase(address)) { 
     isDuplicate = true; 
     do { 
      TelephoneNumber phoneNumber = ..;//ask user for phone number and keep repeating until user enters 0 as one user can have multiple numbers 

      clientList.get(i).addNumber(phoneNumber); 
     } while (!phoneNumber.getNumber().equals("0")) 


    //if (listName.equalsIgnoreCase(name)) { //removed duplicate conditions 
     // isDuplicate = true; 

    //} 
} 

並在您的客戶端類中添加addNumber添加numberiñ名單爲:

public void addNumber(TelephoneNumber phoneNumber) { 
    telephoneNumbers.add(phoneNumber); 
} 
+0

發現這非常有用。仍在研究代碼。將其標誌爲一旦其工作的解決方案! – Kanox

+0

很高興幫助:) – SMA

0

如果你要電話號碼添加到特定的客戶端首先必須在數據庫中搜索該客戶端:

public Client searchByName(String name){// find one client and return client;} 

然後設置編號:

public void addNumber(int number){ Client client=searchByName(); client.setNumber(number);} 
+1

嗯什麼?這不符合法律Java語法。 – sprinter

+0

什麼?他們是2類方法;有什麼問題?也許是搜索方法;我沒有完全寫出它;它必須發送查詢到數據庫並找到1個客戶端;然後設置其號碼 – Nazila

0
public void addNumberToClients(String clientName, TelephoneNumber number) { 
    clientList.stream() 
     .filter(client -> client.getName().equals(clientName)) 
     .forEach(client -> client.addNumber(number); 
} 

這將號碼添加到所有客戶端與給定的名稱。將其添加到只有一個,改變.forEach().findFirst().ifPresent()

的方法然後添加到Client

​​