Home > Software design >  How can I access an object in another method?
How can I access an object in another method?

Time:03-18

import java.util.ArrayList;
import java.util.Scanner;

public class employeePayReport
{
    public static Scanner sc = new Scanner(System.in);
    private static ArrayList<ArrayList<String> > console = new ArrayList<ArrayList<String>>();
    static int currState = 1;
    public employeePayReport() {
        this.console = console; 
        String[] record ={employee.name(),employee.hours(),employee.sales(),employee.rate(), employee.weeklySalary()}; //error: Undeclared Variable: employee (error for all the elements of the array) 
        record.add(employee.name());
        //how to connect this object to the object created in employeeType method
        console.add(record); //error: incompatible types: java.lang.String[] cannot be converted to java.util.ArrayList<java.lang.String>
    }
    public static void print(){
        System.out.println();
        System.out.println("\t  Name                  Class      Hours        Sales       Rate              Weekly Pay");
        System.out.println();
        // I have to add coordinates of the array list 
        System.out.println("\t  ======================================================================================");
        // I have to add coordinates of the array list 
        System.out.println("\t  ======================================================================================");
        // I have to add coordinates of the array list 
        System.out.println();
    }
    public static void employeeType(){
        System.out.println("Enter field type- "); 
        System.out.println("1 - Salaried Employee"); 
        System.out.println("2 - Hourly Employees"); 
        System.out.println("3 - Commission Based Employees"); 
        int type = sc.nextInt();
        if(type == 1){
            salariedEmployees employee = new salariedEmployees(); 
        }else if (type == 2){
            hourlyEmployees employee = new hourlyEmployees();
        }else if (type == 3){
            commissionedEmployees employee = new commissionedEmployees();
        }else{
            System.out.println("Select valid type"); 
        }
    }
    public static void main(String[] args){
//I have to edit this method...
            while(currState == 1){
                print();
                System.out.println("Enter a name: "); 
                String name = sc.next();
                employeeType(); 
            }
        }
    }

If you look at my employeePayReport constructor, I am getting an error for all the employee variable. The error says that I haven't declared the variable employee. I want to connect this variable to the object I created in the employeeType method. How do I give access to the object created employee to my constructor employeePayReport?

CodePudding user response:

I suggest to you to create an abstract class Employee that have the common attributes with your three other classes salariedEmployees, hourlyEmployees, and commissionedEmployees and have also getters and setters like:

public abstract class Employee {

    private String name;
    private String hours;
    private String sales;
    private String rate;
    private String weeklySalary;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    .
    .//getters/setters
    .
}

then create your other three classes that extends this class like :

public class commissionedEmployees extends Employee{
    //specific attribute 
}

And As @OldProgrammer said in comment, in the EmployeePayReport class you can declare your Employee class as Constructor parameter :

public EmployeePayReport(Employee employee) {
        this.console = console; // does not make sense you assign the console to it's self
        String[] record ={employee.getName(),employee.getHours(),employee.getName(),employee.getRate(), employee.getWeeklySalary()}; //error: Undeclared Variable: employee (error for all the elements of the array) 
        //record.add(employee.getName() ""); // you can't do that because does not have function add
        //how to connect this object to the object created in employeeType method
        
        //to avoid this error you can use Arrays.asList(record) to convert array to list
        console.add(new ArrayList<String>(Arrays.asList(record))); //error: incompatible types: java.lang.String[] cannot be converted to java.util.ArrayList<java.lang.String>
}

Nb : I explained the errors that you got in the comments

  • Related