1

Hello I'm a bit confused with some coding problem I am trying to solve.

I have a few string arrays:

String[] firstNames= {"Fred","John","Amir", "James","Bob","Jay","Amber"};
String[] lastNames = {"Bond","Kates","Memar", "White","Marley","Brown","Nogofski"};
String[] idNumbers = {"R111111","A222222","AB11111", "KR22121","V311133","L242434","P102432"};
String[] employeeNum = {"1111","2222","3333", "4444","5555","6666","7777"};

I have to create one array and somehow organize the corresponding pieces of information provided above in the method Employee[] list = new Employee[firstNames.length]; list = listOfEmployees(firstNames,lastNames,idNumbers); // create the list of employees in one array

I started writing out the method:

public static Employee[] listOfEmployees(String[] firstName, String[]             
lastName, String[] idNumber){

}

but not sure how to approach this. also not sure if my parameters are correct.

the end result is supposed to look like this:

Employee #1 
    first name:Fred Last Name:Bond
    Id number:R111111

. . .

Employee #2 
    first name:John Last Name:Kates
    Id number:A222222

and so on..

thanks in advance.

EDIT:

Employee class:

public class Employee{
    private String firstName;
    private String lastName;
    private String idNumber;
    private String employeeNumber;
    private int employeeCount;

    /**
     * Constructor
     * @param firstName first name
     * @param lastName last name
     * @param idNumber id number 
     */
    public Employee(String firstName, String lastName, String idNumber){
        this.firstName = firstName;
        this.lastName = lastName;
        this.idNumber = idNumber;
        employeeCount = 0;
    }
    /**
     * Accessors here
     */

    public String getFirstName(){
        return firstName;
    }

    public String getLastName(){
        return lastName;
    }

    public String getIdNumber(){
        return idNumber;
    }

    public String getEmployeeNumber(){
        return employeeNumber;
    }

    // mutators here

    /**
     * @param firstName first name
     */
    public void setFirstName(String firstName){
        this.firstName = firstName;
    }

    /**
     * @param lastName last name
     */
    public void setLastName(String lastName){
        this.lastName = lastName;
    }

    /** 
     * @param idNumber id number
     */
    public void setIdNumber(String idNumber){
        this.idNumber = idNumber;
    }

    /**
     * @param employeeNumber employee number
     */
    public void setEmployeeNumber(String employeeNumber){
        this.employeeNumber = "";
    }
    @Override
    public String toString(){
        String result = "First name: " + getFirstName() + "\nLast name: " + getLastName()
                + "\nId number: " + getIdNumber() + "\nEmployee number: ";
        if(getEmployeeNumber() == null){
        return result + "No employee number has been assigned yet!";
    }
        return result + getEmployeeNumber();
    }

}
5
  • 1
    Where is the defintion of the Employee class Commented Jul 7, 2016 at 2:22
  • @QPaysTaxes i tried several for loops but couldnt tie them together. Commented Jul 7, 2016 at 2:24
  • @ScaryWombat i have the employee class but its quite long, wasnt sure if to add or not. Commented Jul 7, 2016 at 2:24
  • If the purpose of employeeCount is to keep track of how many instances are being created then you should declare it as a static variable. Commented Jul 7, 2016 at 2:33
  • My OCD is telling me I don't like this Datastructure. I am not sure why you group all names to names-array, lname to lnam-array etc.. Its not code friendly and hard to maintain. Commented Jul 7, 2016 at 3:06

3 Answers 3

3

Please try the following:

private static Employee[] listOfEmployees(String[] firstNames, String[] lastNames, String[] idNumbers){     
   Employee[] list = new Employee[firstNames.length]; 

   for(int i=0; i<list.length; i++){
    list[i]=new Employee(firstNames[i], lastNames[i], idNumbers[i]);
   }    

  return list;
 }

To print the array returned by the above function, you may use:

private static void printEmployees(Employee[] employees){
    for (Employee employee : employees) {
        System.out.println("ID: "+employee.getIdNumber());
        System.out.println("Name : "+employee.getFirstName()+" "+employee.getLastName());
        System.out.println("------------------------------------");
    }
}

And call them by following statement:

printEmployees(listOfEmployees(firstNames,lastNames,idNumbers));

Sign up to request clarification or add additional context in comments.

5 Comments

Will crash if the arrays do not have the same size though.. Edit: More precisely if firstNames length is higher that another array length
Sure @SnoozeTime but for this simple scenario and problem that (variable sizes of individual array or any of them is null or not ) has not been considered. Should we? What do you think?
If the code is shared with other people, I'm pretty sure someone is going to call this function with arrays of different size. Of course if the code is just a simple scenarios, it should be ok =)
@SanjeevSaha how would i go about printing these?
Would you like to print the array of Employee on the console?
0

Do a for loop and use the Employee constructor to initialize the objects:

Employee[] list = new Employee[firstNames.length];
for (int i = 0; i < firstName.length; i++) {
    list[i] = new Employee(firstName[i], lastName[i] ...
}

Comments

0

Try this

public class Test{
public static void main(String[] args){
String[] firstNames= {"Fred","John","Amir", "James","Bob","Jay","Amber"};
String[] lastNames = {"Bond","Kates","Memar", "White","Marley","Brown","Nogofski"};
String[] idNumbers = {"R111111","A222222","AB11111", "KR22121","V311133","L242434","P102432"};
String[] employeeNum = {"1111","2222","3333", "4444","5555","6666","7777"};
List<Employee> list = new ArrayList<>();
for(int i=0;i<firstName.length();i++){
list.add(new(Employee(firstName[i],lastName[i],idNumbers[i],employeeNumber[i]))}

}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.