0

I am new to objects and classes in java. I want to know how to get data from attribute objects. I have 4 classes

public class Personnel {
  private ArrayList<Employee> employees = new ArrayList<Employee>();

  public void addEmployee(Employee employee){
    employees.add(employee);
  }

  public ArrayList<Employee> getEmployees() {
    return this.employees;
  }

}

public class Employee{
  /**DATAFIELDS*/
  private String name;
  Address address;
  PayInfo pay;

  //Constructor
  //methods
  .....
}

public class PayInfo {
  /**DATAFIELDS*/
  private double salary;
  private boolean isFullTime;

  //Constructors
  //Methods
  ....
}

public class Address {
  /**DATAFIELDS*/
  private String streetAddress;
  private String city;
  private String state;
  private String zipCode;

  //Constructors
  //Methods
  .....
}

I have created payInfo and address objects and added to the employee object. And finally added that employee object to the Personnel object and saved in an ArrayList. But my question is, whether is there any way to retrieve all these information from just Personnel object rather than going through each objects ? Thanks in advance !! I mean i want something like this from my Main method,

public static void main(String[] args) {
    //Create CODPersonnel Object
    CODPersonnel personnelEmployee = new CODPersonnel();
    //Address Object for emp1 Object
    Address addressEmp1 = new Address();
    addressEmp1.setStreetAddress("637 67th Place");
    addressEmp1.setCity("Willowbrook");
    addressEmp1.setState("IL");
    addressEmp1.setZipCode("60527");

    //PayInfo for emp1 object
    PayInfo emp1PayInfo = new PayInfo();
    emp1PayInfo.setFullTime(true);
    emp1PayInfo.setSalary(85000.0);

    //Create the Employee object
    //and set attributes
    Employee emp1 = new Employee();
    emp1.setName("Samantha Simmons");
    emp1.setAddress(addressEmp1);
    emp1.setPay(emp1PayInfo);

    //Add objects to CODPersonnel objects
    personnelEmployee.addEmployee(emp1);
}

And i want to retrieve all information through Personnel object like,
System.out.println(personnelEmployee.getEmployees(......);

or, System.out.println(personnelEmployee.getAddress.streetName..etc,) something like that. I know it may sound stupid but is there any way like that??

Tom
  • 95
  • 8
  • Please do some research about tutorial for beginner about java and you will understand. – JHDev Apr 13 '16 at 18:55

2 Answers2

0

Not sure what you're asking. You can access the information from the Employee objects in the ArrayList in Personnel, assuming you have the appropriate getters and setters in Employee that returns its PayInfo and Address.

ITWorker
  • 965
  • 2
  • 16
  • 39
0

You could put PayInfo and Address into a Details class. Each Employee could be composed of a Details object. You can then access the Details object from the Personnel class:

class Personnel {
    private Map<String, Employee> employees = new HashMap<>();

    public void addEmployee(Employee employee) {
        employees.put(employee.getName(), employee);
    }

    public Details getEmployeeDetails(String name) {
        return employees.get(name).getDetails();
    }
}

class Employee {
    private String name; //could be stored in Details
    private Details details;

    public Employee(String name, Details details) {
        this.details = details;
    }

    public Details getDetails() {
        return details;
    }

    public String getName() {
        return name;
    }
}

class Details {
    private PayInfo payInfo;
    private Address address;

    public Details(PayInfo payInfo, Address address) {
        this.payInfo = payInfo;
        this.address = address;
    }

    public PayInfo getPayInfo() {
        return payInfo;
    }

    public Address getAddress() {
        return address;
    }
}

I switched your List into a Map, so you can easily access each employee by name. You could then do:

public static void main(String[] args) {
    PayInfo info = ...;
    Address address = ...;
    Details details = new Details(info, address);

    String name = ...
    Employee employee = new Employee(name, details);

    Personnel personnel = new Personnel();
    personnel.addEmployee(employee);

    //example
    Details employeeDetails = personnel.getEmployeeDetails("Some Name");
    Address employeeAddress = employeeDetails.getAddress();
    System.out.println(employeeAddress.getStreetAddress());
}

This design strongly violates the Law of Demeter, so if you're looking for a design that doesn't violate it, I'll be more than happy to edit in a stronger design.

Vince
  • 14,470
  • 7
  • 39
  • 84
  • I would be very happy if you can show me one that does not violate any laws. Thank you very much for helping me. – Tom Apr 13 '16 at 19:11
  • @Tom I realized that fixing the violation requires you to rethink your entire design. Your code requires a model-view design (MVC or MVVM), which I feel *is beyond the scope of **this question***. A model-view design will allow you to decouple your program (the model) from the behaviors required to display data to the user (handled by the view). Feel free to post a follow-up question and I'll answer there, but once again it's way beyond the scope of what you are currently doing and, without the proper knowledge, could overcomplicate your development process. – Vince Apr 13 '16 at 21:17
  • These principles exist to help simplify development: they fix certain design problems. But never force it, or you might end up creating cognition problems for yourself. If it ain't broke, don't fix it. If you feel this is a problem and you really want to avoid it, go ahead and post a follow-up question (to keep the Q&A on-topic) :) [This comment](http://stackoverflow.com/questions/36322734/java-accessor-methods-vs-protected-fields#comment60268555_36322734) gives an idea of how Law of Demeter should be dealt with and why it exists (LOD ensures encapsulation) – Vince Apr 13 '16 at 21:19
  • Thank you very much. And yes you are right i am a beginner with java even though i did some MVC pattern with php. I think i need to learn more before thinking MVC in java. Thank you very much for your help this is more than enough considering I am a beginner. – Tom Apr 13 '16 at 21:34