Vinoth
Vinoth

Reputation: 1349

Create objects dynamically. Back to basics

I am writing a simple program to list the name of the companies and the workforce in JAVA.

I would like to create objects dynamically. Below is the code

public class EmployeeRecord {

/**
 * @param args
 */
String company, name;
int employee;
public String input;
public static BufferedReader br;
public int iE;
public static String numberOfCompanies;
String nameOfCompany;*/

public void company(String input) {
    // TODO Auto-generated method stub

    nameOfCompany = input;
}

public void employee(String employeeNumber) {
    // TODO Auto-generated method stub
    iE = Integer.parseInt(employeeNumber);
}*/

public static void main(String[] args) {
    // TODO Auto-generated method stub

    EmployeeRecord qw = new EmployeeRecord ();
    try {
        br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter number of companies: ");
        numberOfCompanies = br.readLine();
        int G = Integer.parseInt(numberOfCompanies);

        for (int i = 1; i <= G; i++) {

            System.out.println("Enter name of the company: ");
            String company = br.readLine();
            qw.company(company);
            System.out.println("Enter Number of employees: ");
            String employeeNumber = br.readLine();
            qw.employee(employeeNumber);

        }

        for (int i = 1; i <= G; i++) {

            qw.sortCompanySummary();
        }
    } catch (IOException io) {
        io.printStackTrace();
    }

}

public void companySummary() {

    System.out.println("Number of companies: " + numberOfCompanies);
    System.out.println("Name of company: "+nameOfCompany);
    System.out.println("Number of employees: "+iE);


}

}

What I would like to do over here is create separate instances of the class EmployeeRecord dynamically. eg

EmployeeRecord qw = new EmployeeRecord();
EmployeeRecord we = new EmployeeRecord();

Upvotes: 0

Views: 631

Answers (3)

Alberto Solano
Alberto Solano

Reputation: 8227

According to the code you posted and to the dynamic creation of objects you mentioned, I think the only way to do it is that you should take a look to the Collections Framework.

Upvotes: 1

user684934
user684934

Reputation:

First, you need to separate out the object code from the controlling code.

Second, you need some sort of collection or array to hold your objects.

Here's an idea of how your code should look:

public class UI{ // <---- this class will control the flow of your program
    public static void main(String[] args){
        private List<Company> company; // <---- this Collection object holds many Company objects
        ...
        for(int i=0;i<company.size();i++){
            Company c = new Company();
            c.setName(br.readLine());
            List<Employee> employee = new ArrayList<Employee>();
            ...
            for(int j=0;j<employee.size();j++){
                Employee e = new Employee();
                e.setName(br.readLine());
                ...
                employee.add(e);
            }
            c.setEmployee(employee);
            company.add(c);
         }
    }
}

public class Company{ // <---- this class will represent the companies
    private List<Employee> employee;
    private String name;
    public void setEmployee(List<Employee> employee){
        this.employee = employee;
    }
    ....
}

public class Employee{ // <----- this class will represent the employees
    private String name;
    private int empNo;
    public int getEmpNo(){
        return empNo;
    }
    ...
}

Upvotes: 2

Zohaib
Zohaib

Reputation: 7116

I did not understand the question correctly, but looking through the code, I believe you need to create objects in a loop as you are taking input from user. This is what you will need to do:

ArrayList<EmployeeRecord> qwList = new ArrayList<EmployeeRecord>();

Declare List before asking for input from user. Now create the objects inside the loop, assign them values and add those objects to the list. This is what you can do inside the list

for (int i = 1; i <= G; i++) {

        EmployeeRecord qw = new EmployeeRecord (); 
        System.out.println("Enter name of the company: ");
        String company = br.readLine();
        qw.company(company);
        System.out.println("Enter Number of employees: ");
        String employeeNumber = br.readLine();
        qw.employee(employeeNumber);
        qwList.add(qw);

    }

For every company a new object has been inserted in the list. Now you can do whatever you want with this list. Either print all the records or sort them.

Upvotes: 2

Related Questions