Results 1 to 5 of 5
- 03-07-2008, 08:20 PM #1
Member
- Join Date
- Mar 2008
- Posts
- 4
- Rep Power
- 0
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??
- 03-07-2008, 10:08 PM #2
"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.
- 03-07-2008, 10:53 PM #3
Member
- Join Date
- Mar 2008
- Posts
- 4
- Rep Power
- 0
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?
- 03-07-2008, 11:59 PM #4
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.
- 03-08-2008, 12:47 AM #5
Member
- Join Date
- Mar 2008
- Posts
- 4
- Rep Power
- 0
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
-
Increment a Variable
By rhm54 in forum New To JavaReplies: 2Last Post: 06-14-2008, 02:57 AM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM -
object instantiation and arrayList
By lockmac in forum New To JavaReplies: 5Last Post: 08-09-2007, 06:25 PM -
how to return an object from an arraylist
By elizabeth in forum New To JavaReplies: 1Last Post: 07-30-2007, 06:57 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks