简体   繁体   中英

How do I add information to already existing objects in an ArrayList?

I am in need of help.

I have an ArrayList full with name of clients/customers. What I want to do is to write a code so it asks me for the client name and then I type it in and if it finds it to add a phone number to the client. Once it is added if I want to print out the list the phone number will be next to that specific client.

I have 3 classes. 1 is the client class (with variables - name, address, number) and a JobManager (the whole test program with methods such ass adding a client and removing one).

This is how my adding a client looks like:

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

      }
    }
 }

And this is what my client class looks like:

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

Thanks in advance for the answers guys!

You could do it over here:

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;

    //}
}

And in your Client class add the addNumber to add numberi n list as :

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

If you want to add phone number to specific client first you have to search that client in databse:

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

and then set number:

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

This will add the number to all clients with the given name. To add it to just one, change .forEach() to .findFirst().ifPresent()

Then add a method to Client

public void addNumber(TelephoneNumber number) {
    telephoneNumbers.add(number);
}

I have developed a small sample of the code from your example where I am hard enter code here coding Telephone numbers and in the main method am hard coding 2 as am entering the user input as V3 in my test scenario.

Client Class

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

you can use the same sort of addTelephone number method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM