Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-07-2008, 09:20 PM
Member
 
Join Date: Mar 2008
Posts: 4
rugbyGeek is on a distinguished road
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:
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??
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-07-2008, 11:08 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
"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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-07-2008, 11:53 PM
Member
 
Join Date: Mar 2008
Posts: 4
rugbyGeek is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-08-2008, 12:59 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 03-08-2008, 01:47 AM
Member
 
Join Date: Mar 2008
Posts: 4
rugbyGeek is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Increment a Variable rhm54 New To Java 2 06-14-2008 03:57 AM
Java Project Trouble: Searching one ArrayList with another ArrayList BC2210 New To Java 2 04-21-2008 12:43 PM
object instantiation and arrayList lockmac New To Java 5 08-09-2007 07:25 PM
how to return an object from an arraylist elizabeth New To Java 1 07-30-2007 07:57 PM


All times are GMT +3. The time now is 08:10 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org