0

So I'm working on a java assignment that reads as follows:

"Write a program that models an employee. An employee has an employee number, a name, an address, and a hire date. A name consists of a first name and a last name. An address consists of a street, a city, a state (2 characters), and a 5-digit zip code. A date consists of an integer month, day and year. Use an Employee class, a Name class, an Address class, and a Date class in your solution. Provide appropriate class constructors, getter methods, setter methods, and any other methods you think are necessary. Your program should prompt the user to enter data for several employees and then display that data. The number of employees to store data for shall be entered from the command line."

Since I am having trouble formatting, I can't post the code, but what I did was create an array of employee objects. The array was titled dbase[]. each Employee object then created a separate object for date, name, and address title date1, name1, and address1. However whenever I try to access one of the getter methods in the date/name/address, using something like this dbase[j].name1.getFirstName I run into an error that says cannot find symbol: name 1 --- location: class Employee.

public class EmployeeProgram {
public static void main (String[] args) {
    int i = Input.getInt ("How many employees would you like to enter? ");
    int j;
    Employee [] dbase = new Employee [i];
    for (j = 0; j < i; j++) {
        String firstName = Input.getString ("What is an employee's first name?");
        String lastName = Input.getString ("What is this employee's last name?");

        String street = Input.getString ("On what street does this employee live?");
        String city = Input.getString ("In which city does this employee live?");
        String state = Input.getString ("In which state does this employee live? (abbreviation)");
        String zipcode = Input.getString ("What is this employee's zip code?");

        int month = Input.getInt ("In what month was this employee born?");
        int day = Input.getInt ("On what day was this employee born?");
        int year = Input.getInt ("In what year was this employee born?");

        int employeeID = Input.getInt ("What should this employee's employee id be?");

        dbase[j] = new Employee(firstName, lastName, street, city, state, zipcode, month, day, year, employeeID);
    }

    for (j = 0; j < i; j++) {
        System.out.print ( "Employee number " + (j + 1) + " is named ");
        System.out.print ( dbase[j].name.getFirst() + " " + dbase[j].name.getLast() + " and lives on " + dbase[j].name.getStreet());
        System.out.print ( " in " + dbase[j].address.getCity() + " " + dbase[j].address.getState() + ", " + dbase[j].address.getZip());
        System.out.print ( ". He will be hired on " + dbase[j].date.GetMonth() + "-" + dbase[j].date.getDay() + "-" + dbase[j].date.getYear() );
        System.out.print ( " and his ID is " + dbase[j].getEmployeeID());
        System.out.println ();
    }
}
}

class Employee {
int employeeID = 0;
Employee( String firstName1, String lastName1, String street1, String city1, String state1, String zipcode1, int month1, int day1, int year1, int employeeID1 ) {
    name name = new name( firstName1, lastName1 );
    address address = new address( street1, city1, state1, zipcode1 );
    date date = new date( month1, day1, year1);
    employeeID = employeeID1;   
}
int getEmployeeID() {
    return employeeID;
}
}

class name {
String firstName = "";
String lastName = "";
name(String newFirstName, String newLastName) {
    firstName = newFirstName;
    lastName = newLastName;
}
String getFirst() {
    return firstName;
}
String getLast() {
    return lastName;
}   
}

I didn't post the address or date classes but they are basically the same as the name class

alex
  • 10,900
  • 15
  • 70
  • 100
  • 3
    Without seeing some representative code, this is going to be tricky to help with. – Oliver Charlesworth Jan 12 '14 at 23:54
  • I can try and post an imgur link of screenshots if that would help – user3188486 Jan 12 '14 at 23:55
  • 2
    Why can't you copy and paste it? – Radiodef Jan 12 '14 at 23:55
  • Why not just post the Employee class? – Ingo Jan 12 '14 at 23:56
  • when I copy pasted it, it did some weird formatting thing where a middle section of the code ended up being formatted correctly but the rest didn't – user3188486 Jan 12 '14 at 23:57
  • BTW, it is too funny, that homeworks invariably seem to read the number of data to enter from stdin. This is so *weltfremd*. Nowhere in the industry is it even done like this (I hope). – Ingo Jan 12 '14 at 23:57
  • Are you sure you have a field called `name1` in your `Employee` class? – Dawood ibn Kareem Jan 12 '14 at 23:57
  • 4
    Just post the code here. We will try to format it. But it sounds like, you have not yet understood the concept of classes and objects. So go back to a simple Java Tutorial and read through those concepts. Having an attribute name1 sounds wrong. Every Person should only have one (first) name in this, right? In the end, there should only be ONE class for each type mentioned (addres, employee, Name....) but more than one instance. – Carsten Hoffmann Jan 12 '14 at 23:57
  • Code on SO must be indented by four spaces. If your IDE can tab multiple lines, pasting it here is easy. Select all -> tab -> copy -> paste it here -> undo the extra tab in your IDE. – Radiodef Jan 12 '14 at 23:58
  • ClassNames should always be upperCase! – Carsten Hoffmann Jan 13 '14 at 00:06

2 Answers2

2

You have created no fields in the Employee class. Just local variables inside the constructor. So add

private Name name;
private Address address;

... (Class names always UpperCase) and add getter and setter for those as well.

In your main you should then use

dbase[j].getName().getFirst()

etc...

Carsten Hoffmann
  • 901
  • 5
  • 14
0

As far as I can see your main problem is that your members of Employee aren't fields, they are local variables in the constructor. This means they basically disappear when the constructor ends. You should be able to just move them so they are fields and the program will compile. Here is the relevant tutorial.

class Employee {
    name name;
    address address;
    date date;
    int employeeID = 0;

    Employee( String firstName1, String lastName1, String street1, String city1, String state1, String zipcode1, int month1, int day1, int year1, int employeeID1 ) {
        name = new name( firstName1, lastName1 );
        address = new address( street1, city1, state1, zipcode1 );
        date = new date( month1, day1, year1);
        employeeID = employeeID1;   
    }
    int getEmployeeID() {
        return employeeID;
    }
}

A couple minor notes:

  • In Java, the convention is that all classes start with an uppercase letter. (class name should be class Name, etc)
  • Classes like your Employee should have their fields as private and accessed through "get" methods. There's actually a standard for a similar kind of class called a Bean. This is not so important for a beginner since encapsulation is something you'll learn about. The tutorial shows a class with the "private with accessors" scheme.
Community
  • 1
  • 1
Radiodef
  • 37,180
  • 14
  • 90
  • 125