• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generate employee id trouble  RSS feed

 
Heb Heather Gagnon
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, very new to this and I would greatly appreciate any help with my basic console java program. I need to generate an employee id. I have an employee class that I will paste here so you can see my fields and constructors.



I need to create a method in my main class to make an employee object that has their first name, last name, and generate an employee id. I have tried several times and I cannot get this to work properly. Here is my main class.





Thank you in advance for any help.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the Ranch!

I see that you have a "nextUniqueId" variable in your code. So far you haven't used it, but I expect your plan was to add 1 to it each time you need a unique employee ID?

That's the right general idea, but you have it as an attribute of your Employee object. It should actually be a static variable in the Employee class. That way it doesn't belong to any particular Employee object. Then when you're constructing an Employee object you just add 1 to nextUniqueId and assign it to the Employee's id variable.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you generating the Employee Id? In your Employee class, you set it to 0 in your default constructor, but no where you have a method or whatever that generates a value for the Id?
 
Heb Heather Gagnon
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the input would you mind showing me an example of how I would generate this id when an user enters their first and last name, that's what I'm having a difficult time with. I learn visually. Thank you very much.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

Make all the fields in the Employee class private (except that Scanner, which you can probably delete altogether since it doesn't appear to be used anywhere).
You have two IDs: one is the ID belonging to a particular Employee which needs to be initialised in the constructor, and the other is the next ID (or count of Employees to date). That latter number is shared between instances and must therefore be static. Every time you create a new Employee instance, you increment that number by 1.
So, id = nextID; sets all the ids to the same value. All you need to do is to add something to that line to increase the value of nextID by 1. You must not try to reset the value of nextID.

You don't actually have a default constructor; you have a no‑arguments constructor. That doesn't seem to set anything useful, and I think you ought to delete it.
You have some confusion about IDs. You can either get the Employee class to set it, which you are doing in the constructor you don't want, or you can get somebody else to set it. In the latter case, how do you know they are not going to duplicate numbers? I think the way you are setting it in the constructor you don't want is better than letting somebody outside the class set the ID.
Similarly, once you have appointed an Employee, is their ID ever going to change? If no, then why have you get a setID method?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heb Heather Gagnon wrote:I need to create a method in my main class to make an employee object that has their first name, last name, and generate an employee id. I have tried several times and I cannot get this to work properly...

Possibly because a unique ID is not an easy thing to generate, even though it may seem like it should be.

As Paul said, you're on the right lines, but even if you make 'nextUniqueID' static (which it probably should be, because you want it to be independent of the instances that you're creating), it will be reset to 0 every time you rerun your program.

One possibility is to store the "last ID generated" somewhere outside your program - for example, in a file - but even that might not work if you have many programs that need to create Employees. And file I/O is also notoriously slow.

If this is just for a class exercise, the main thing I see (other than the other good advice) is to make your sequence number static; but if this is for a real life application, then you might want to look at generating the number from a database.

HIH

Winston
 
Heb Heather Gagnon
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all so much for helping me. This is for a class exercise and unfortunately I am learning just from a book, no lectures, so I am confused on several levels. The exercise asks me to have the user enter their first and last name and generate the id; and you are correct that I need to store that last generated, but I am very unfamiliar with writing to a file and feel that it is somewhat over my head at this point. What I wanted to try was to have the last generated id stored in an array list called employee. I don't see that this program will ever change or update the employee ID, so I would remove the set Id. I certainly feel somewhat lost here, thank you for all of the advice. If I wanted to generate the id in the employee class would I do something like this:


Would I then in my main class still do:

///I have a class that created an employee array with an addEntry method.

I apologize very much I am so lost here: I don't know how I would save the last generated id so that when I add another employee that it wouldn't go back to 0.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you know exactly how to increment the nextID. The only problem is , you are doing it in the wrong place.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code is a lot better (I added code tags, and it looks even better), but you are trying to do too much. Do not try to add Employees to a List at this stage. Play around with one Employee until it is working, then think of adding the List later.
As we have said, you do notwant the ID set by a constructor parameter. In this simple class, set it from nextID.
 
Heb Heather Gagnon
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for all your help I fixed my constructor and got my counter to work! Now on to figuring out the array. Then to punch in and out. I probably will have questions, thanks again.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show us what you have got so far
 
Heb Heather Gagnon
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My main class:

import java.util.Scanner;

/**
*
* @author Owner
*/
public class EmployeeTimeClockApp
{
private static Scanner sc = null;
public static EmployeeFile employeefile = new EmployeeFile();
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
System.out.println(" Welcome ");
System.out.println();

sc = new Scanner(System.in);
displayMenu();

String choice = "";

while (!choice.equalsIgnoreCase("d"))
{
choice = Validator.getString(sc, " Enter a choice: ");
System.out.println();

if ("a".equalsIgnoreCase(choice))
getEmployees();
else if (choice.equalsIgnoreCase("b"))
punchMenu();
else if (choice.equalsIgnoreCase("c"))
System.out.println("You chose to pull a report");
else if (choice.equalsIgnoreCase("d"))
System.out.println("Goodbye");
else
System.out.println("Error! Not a valid command.\n");

}//end while
}//end main
public static void displayMenu()
{
System.out.println(" EMPLOYEE TIME CLOCK");
System.out.println();
System.out.println(" A)Add New employee");
System.out.println(" B)Punch In/Out ");
System.out.println(" C)Report ");
System.out.println(" D)Exit ");
System.out.println(" E)Employee List ");
System.out.println();

}

public static void getEmployees()
{
System.out.println(" Add New Employee ");
System.out.println();

String firstName = Validator.getString(
sc, "Enter First Name: ");
String lastName = Validator.getString(
sc, "Enter Last Name: ");

Employee e = Employee.addEmployees(firstName,lastName);
employeefile.addEntry(e);


System.out.println("Employee Id: " + e.getId() + "\n");
System.out.println();
System.out.println(firstName + " " + lastName +
" has been added.\n");

String answer = Validator.getStringContinue(sc, "Add another employee? (y/n): ");
System.out.println();
if(answer.equalsIgnoreCase("y"))
getEmployees();

}

public static void punchMenu()
{
System.out.println(" PUNCH IN OR OUT ");
System.out.println();
System.out.println(" A) Punch In");
System.out.println(" B) Punch Out");
System.out.println();


}


}//end employeeApp


employee class: I had to make my variable static in order to run my addEmployees() not sure if this is right if I could put it somewhere else but it does count properly when I make an employee object
import java.util.Scanner;


/**
*
* @author Owner
*/
public class Employee
{
private static String firstName;
private static String lastName;
private int id;

public static int nextUniqueId = 0;
public Scanner sc = new Scanner(System.in);
public static EmployeeFile employeefile = new EmployeeFile();

public Employee()
{
firstName = "";
lastName = "";

}

/**
*
* @param firstName
* @param lastName

*/
public Employee(String firstName, String lastName) //constructor
{
this.firstName = firstName;
this.lastName = lastName;
nextUniqueId++;
id = nextUniqueId;
}
public int getId()
{

return id;
}
public void setId( int id )
{
this.id = id;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getFirstName()
{
return firstName;
}
public void setLastName (String lastName)
{
this.lastName = lastName;
}
public String getLastName()
{
return lastName;
}


@Override
public String toString()
{
return "First Name: " + firstName + "\n" +
"Last Name: " + lastName + "\n" +
"Employee ID: " + id + "\n" ;
}

public static Employee addEmployees(String first,String last)
{
Employee employee = new Employee(firstName,lastName);
employee.setFirstName(firstName);
employee.setLastName(lastName);
employee.getId();

return employee;

}


}

employeefile class (my array class)

mport java.util.ArrayList;

public class EmployeeFile
{
public ArrayList<Employee> employees; //instance variable


public EmployeeFile() //constructor no arguments
{
employees = new ArrayList<>();
}

void addEntry(Employee employee) //method to add employee to array
{
this.employees.add(employee);
}


public ArrayList<Employee> getEmployeesList() //method to get employee from array
{
return employees;
}



}

I know that I have had some feedback on the array on another thread but I am still confused. I know it is not working because when I re-run my program the employee count starts at 1 again.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you have got the ID right. I can see lots of other problems, I am afraid. One of them is the no‑arguments Employee() constructor, which you should delete.
 
Heb Heather Gagnon
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just when I thought something was going right, could you please tell me what else is wrong?
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Employee class only looked at.

Three fields labelled public which ought to be private.
Two fields marked static which should NOT be static (both names) unless you want all Employees to have the same name
Why is the List of Employees inside the Employee class? It ought to be elsewhere. Do you actually use it in the Employee class? You appear not to, so you can lose that field and have an Employee File field in another class.
You haven't got rid of the setID method nor the no‑ arguments constructor.
The addEmployee method doesn't seem to do anything which you can't do simply by saying
Employee emp = new Employee("Heb Heather", "Gagnon");nbsp; … and that method appears to set the same name twice. I think you can delete that method.
 
Heb Heather Gagnon
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you again for your wonderful feedback! I have made all those changes and I think I am on the right track. I need to make an array that will store my employee objects. I think I have attempted this in a different manner than last presented. I don't know if this is an affective approach or not, because a file is another option but I don't know how to do that at all. My goal is to have the user enter their first and last name, have the program generate the id and then store this in an array, that way, when I attempt (which is a joke because I don't know where to start) to build a punch in and out screen, I assume that I would need to find an object in my array by going off of the employee id number, then having that employee object punch in or out. Could you please look at my attempt? The getEmployees() is within my main program. I know methods should be short but this is all I can do for now to try to get this to work.

public static void getEmployees()
{
System.out.println(" Add New Employee ");
System.out.println();

String firstName = Validator.getString(
sc, "Enter First Name: ");
String lastName = Validator.getString(
sc, "Enter Last Name: ");

ArrayList<Employee> employees = new ArrayList<>(); //create array

Employee emp = new Employee(firstName,lastName); //create employee object
employees.add(emp); //add employee object to array

System.out.println("Employee ID: " + emp.getId()); //have employee id display on the screen
System.out.println(firstName + " " + lastName +
" has been added.\n");

String answer = Validator.getStringContinue(sc, "Add another employee? (y/n): ");
System.out.println();
if(answer.equalsIgnoreCase("y"))
getEmployees();
if(answer.equalsIgnoreCase("n"))
for(int cnt = 0; cnt < employees.size(); cnt++){
System.out.println("Total additions:" + employees.get(cnt));
}

}

I can't understand what I am doing wrong or if I should just try to write to a file, this is my output and when I try to see array list on screen.

Welcome

EMPLOYEE TIME CLOCK

A)Add New employee
B)Punch In/Out
C)Report
D)Exit

Enter a choice: A

Add New Employee

Enter First Name: Ronald
Enter Last Name: McDonald
Employee ID: 1
Ronald McDonald has been added.

Add another employee? (y/n): y

Add New Employee

Enter First Name: Jack
Enter Last Name: Black
Employee ID: 2
Jack Black has been added.

Add another employee? (y/n): n

Total additions:First Name: Jack
Last Name: Black
Employee ID: 2

Enter a choice:
 
Heb Heather Gagnon
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My improved Employee class

package employeetimeclock;

public class Employee
{
//private variables
private String firstName;
private String lastName;
private int id;

public static int nextUniqueId = 0;

/**
*
* @param firstName
* @param lastName

*/

//constructor
public Employee(String firstName, String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
nextUniqueId++;
id = nextUniqueId;
}

//get the value of id
public int getId()
{

return id;
}

//set the value of first name
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
//get the value of first name
public String getFirstName()
{
return firstName;
}
//set the value of last name
public void setLastName (String lastName)
{
this.lastName = lastName;
}
//get the value of last namesw
public String getLastName()
{
return lastName;
}


@Override
public String toString()
{
return "First Name: " + firstName + "\n" +
"Last Name: " + lastName + "\n" +
"Employee ID: " + id + "\n" ;
}




}
 
Heb Heather Gagnon
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I got it, I added the new array to the main class, put in a while loop in my getEmployees method and now its printing the array. So I think I got that right.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Difficult to tell, because you haven't found the code button yet. Please use the button to add code tags, with the code button, around each of your classes.

Beware of the keyword static. If you don't know why you made something static, it is probably a mistake. If you added it because of a compiler complaint about the main method, it is definitely a mistake. Have a look at Winston's FAQ about it, and when you have understood that, remove every occurrence of static from that class, unless you can find it in that FAQ. You will have to move 98% of the current main method into a different method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!