Home > Blockchain >  How to set up get&set Methods in an ArrayList consisting of custom Objects
How to set up get&set Methods in an ArrayList consisting of custom Objects

Time:10-18

I need to create a Java Project, which consists of the following Classes: Main, File, Person, Address. In the Main class, Persons can be created and assigned new Addresses. In the File class the Persons are being saved in an ArrayList. In the Person class there are the attributes constructor for the Person Objects an ArrayList to store the Adresses that are being assigned to each Person in the Main class. The Address Class has the attributes constructor for the Adress objects.

The Address Class has the Attributes zip, city and street and the get set Methods for them.

This is how my code:

public class Address   {
    private int zip;
    private String city;
    private String street;  
     
    public Address(int zip, String city, String street){
        this.zip= zip;
        this.city= city;
        this.street= street;}   

        public  int Getzip() {
        return zip;}        

    public  void setZip(int zip) {
        this.zip= zip;} 

    public String getCity() {
        return city;    }
    
    public void setCity (String city) {
        this.city= city;    }
    
    public String getStreet () {
        return street;  }
    
    public void setStreet(String street) {
                this.street = street;}}

In the Person Class there are the attributes First-Name, Last-Name, and every Person can have multiple Addresses, so once a Person is created, it can have Adresses assigned. So far i have solved this with an ArrayList that saves the adresses, everytime they are being created. Here is my Person Code:

import java.util.ArrayList;
public class Person{
    
    private String firstName;
    private String lastName;    
    private ArrayList<Address> addresses = new ArrayList<Address>();    
    
     Person(String firstName, String lastName){             
        this.firstName = firstName ;
        this.lastName = lastName ;  }

public void addAddress(int zip, String city, String street)
     {Adress ad = new Address (zip, city, street);
      addresses.add(ad);    }
             
     public String getfirstName () {
        return firstName ;  }
     
     public void setfirstName (String firstName) {
            this.firstName= firstName;      }
    
     public String getlastName () {
             return lastName ;  }

     public void setlastName (String lastName ) {
            this.lastName = lastName ;  }}

In the File Class there is the ArrayList that saves the Persons and the Methods to add/remove Persons.

import java.util.ArrayList;
public  class File{
    private ArrayList<Person> persons= new ArrayList<Person>(); 
    private int anzahl = 0;
    
    public void addPerson (Person person) {
        persons.add(person);
        }
    
    public void removePerson (Person person) {
        persons.remove(person);
        }

I create a new File object in the Main Method and some Person objects.

public class Main {
    public static void main(String[] args) {
    
        File listOfPersons= new File();
        Person p1 = new Person ("Michael", "Smith");
        Person p2 = new Person ("James", "Barkley"); 
        Person p3 = new Person ("Olivia", "Atkins");

Then I add the Persons to the File:

listOfPersons.addPerson(p1); 
listOfPersons.addPerson(p2); 
listOfPersons.addPerson(p3); 

I assign each Person an Address:

        p1.addAdress(12345,"New York","Street 1");
        p1.addAdress(23456,"Los Angeles","Street 2");
        p2.addAdress(34567,"Boston","Street 3");
        p3.addAdress(456578,"Miami","Street 4");

So here is my Problem, in the Main Class I have to be able to set or get the attributes of each Person object. I can access and change the attributes firstName and lastName with no problems. But even known I have assigned Adresses to each person, I can´t use the get or set Methods for zip, city or street.

Can anybody help me with that?

Thank you in advance

CodePudding user response:

You will need another method to get the list of addresses assigned to each Person in your Person class. Something like:

public List<Address> getAddresses() {
    return addresses;
}

Then you can access the addresses stored in that list in the Main class:

List<Address> addressList = p1.getAddresses();

for (Address add : addressList) {
     System.out.println(addressList.indexOf(add)   ": "   add.getCity()   ", "   add.getStreet());
}
  • Related