I am new to this forum and I am desperate
I am going batty with what must be a simple problem.
I have a Company class and an Employee class. I also have a Project class that contains main() and instantiates the constructors of Company and Employee. From Project I can successfully get and set values of the private variables of the Company and Employee class. What I cannot figure out how to do is create a constructor and instantiate the constructor so that I can create a Company object within the Employee class.
Below is a snippet of the Company class:
public class Company
{ //beginning of Company class
//declare private variables and initialize values to empty strings
private
String name = "";
private String address = "";
private String city = "";
private String state = "";
private String zip = "";
//constructor method
public Company()
{
}
public Company(String inName, String inAddress, String inCity,
String inState, String inZip)
{
name = inName;
address = inAddress;
city = inCity;
state = inState;
zip = inZip;
}
//define accessor methods for private local variables
public String getName()
{
return name;
}
public String getAddress()
{
return address;
}
public String getCity()
{
return city;
}
public String getState()
{
return state;
}
public String getZip()
{
return zip;
}
//end accessor methods
//begin mutator methods for private local variables
public void setName(String inName)
{
name=inName;
}
public void setAddress(String inAddress)
{
address=inAddress;
}
public void setCity(String inCity)
{
city=inCity;
}
public void setState(String inState)
{
state=inState;
}
public void setZip(String inZip)
{
zip=inZip;
}
//end mutator methods
} //end of Company class
Here is a snippet of the Employee Class:
public class Employee
{ //begin Employee class
//declare private variables and initialize values
private String firstName = "";
private String lastName = "";
private short seniority = 0;
private double baseSalary = 500.0;
private String address = "";
private String city = "";
private String state = "";
private String zip = "";
private Company company;
//constructor methods
public Employee(String inFirstName, String inLastName, short inSeniority,
double inBaseSalary, String inAddress, String inCity,
String inState, String inZip, Company inCompany)
{
firstName = inFirstName;
lastName = inLastName;
seniority = inSeniority;
baseSalary = inBaseSalary;
address = inAddress;
city = inCity;
state = inState;
zip = inZip;
company = inCompany;
}
//define accessor methods for private local variables
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public String getAddress()
{
return address;
}
public String getCity()
{
return city;
}
public String getState()
{
return state;
}
public String getZip()
{
return zip;
}
public short getSeniority()
{
return seniority;
}
public double getBaseSalary()
{
return baseSalary;
}
public Company getCompany()
{
return company;
}
//end accessor methods
//begin mutator methods for private local variables
public void setFirstName(String inFirstName)
{
firstName=inFirstName;
}
public void setLastName(String inLastName)
{
lastName=inLastName;
}
public void setAddress(String inAddress)
{
address=inAddress;
}
public void setCity(String inCity)
{
city=inCity;
}
public void setState(String inState)
{
state=inState;
}
public void setZip(String inZip)
{
zip=inZip;
}
public void setSeniority(short inSeniority)
{
seniority=inSeniority;
}
public void setBaseSalary(double inBaseSalary)
{
baseSalary=inBaseSalary;
}
public void setCompany(Company inCompany)
{
company = inCompany;
}
//end mutator methods
}//end Employee class
Now what I would like to do in Project is instantiate a Company object inside an Employee object so that I could do the following:
//create new Employee object
//this is obviously wrong, but I can figure out what to do, empCompany is
//supposed to instantiate a Company object and all its arguments
Employee salesEmployee = new Employee(empFirstName, empLastName,
empSeniority, empBaseSalary, empAddress, empCity, empState, empZip, empCompany);
salesEmployee.setCompany().setName();
salesEmployee.getCompany().getName();
Thanks in advance. If this is unclear it is due to no sleep in 48 hours, please just ask if you need clarification
Thanks
Dan