Results 1 to 5 of 5
  1. #1
    rugbyGeek is offline Member
    Join Date
    Mar 2008
    Posts
    4
    Rep Power
    0

    Question Object ArrayList - increment solution needed badly!!

    Hey all,

    Im new to java and to java forums and need a dig out with an assignment Im working on.

    I have an Employee Class with data members such as name, age etc. What I am trying to do is give each employee an ID when they are created. This is working: heres some of the construcor code:
    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class Employee implements Serializable{
    	
    	//data members
    	protected String firstname;
    	protected String surname;
    	protected int age;
    	protected char gender;
    	protected double annualSalary;
    	protected double bonus;
    	protected int empID;
    	protected static int noOfEmployees = 0;
    	//public int ID;
    	
    	//constructors
    	
    	//default constructor
    	public Employee(){
    			 	
    	}
    	
    	
    	public Employee(String fName, String sName, int age1, char sex, double salary){
    		
    		firstname = fName;
    		surname = sName;
    		age = age1;
    		gender = sex;
    		annualSalary = salary;
    		
    		empID = ++noOfEmployees;
    	}

    The problem is arising in the main class. I am prompting users to enter employee details, which I am adding to an ArrayList employees. I then output them and the empID is incrementing properly.
    I write them correctly to a .data file. When I read the array back, all the previously enterer employees are there but when I add more employees, the ID is starting again at 1.

    Please suggest a way around this??

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    "noOfEmployees" does not belong in the Employee class. An Employee cannot always know about other employees or how many there are. The static modifier will allow this variable to be changed by any Employee instance. The class that has the ArrayList/Collection of employees is the one who should keep this information, ie, "noOfEmployees". Whoever is adding the employees must/will have acces to this information.

  3. #3
    rugbyGeek is offline Member
    Join Date
    Mar 2008
    Posts
    4
    Rep Power
    0

    Default

    Thanks for reply hardwired. It makes sense not to have the incrementor there. If I have it in my main class, where the Collection is being read in and added to, i can pass the value to the Empoyee object as a parameter yes??

    If so, will i need to write the current value of the increment variable to a seperate file when my loop has finished, and read it in again and assign the current value as the start point of the increment, the next time I run the
    program? I think this will work as the user only has one opportunity to add employees each time they run the program?

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    When I read the array back, all the previously enterer employees are there
    Looks like you may have an opportunity to determine the current number of employees after you have read in the file data, viz, the number of employees read in from the file. If so you can save this as a member variable in your main class and increment/change it as the user adds/deletes entries to your Employee array/ArrayList.

  5. #5
    rugbyGeek is offline Member
    Join Date
    Mar 2008
    Posts
    4
    Rep Power
    0

    Default

    I solved the problem by removing the noOfEmployee variable from the constructor. I added the variable to the main class
    where it was iterated in a loop anytime an Employee was instantsiated.
    To stop the increment starting again at 1, i saved the current variable value (when the add loop was finished) with DataOutputStream, read it back in again, and assigned its value to the variable to be incremented when the loop commenced again.

    Hope this helps anyone with similar problems.

    Would also welcome other suggestions to solve the problem.

Similar Threads

  1. Increment a Variable
    By rhm54 in forum New To Java
    Replies: 2
    Last Post: 06-14-2008, 02:57 AM
  2. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  3. object instantiation and arrayList
    By lockmac in forum New To Java
    Replies: 5
    Last Post: 08-09-2007, 06:25 PM
  4. how to return an object from an arraylist
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 06:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •