• Post Reply Bookmark Topic Watch Topic
  • New Topic

I have an employee class. How do I make an employees class?  RSS feed

 
Steven Greenbaum
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an employee class like so:

when an employee is added or deleted I would like to add or delete from an employees class so that I have a count of the current employees and also so that I can iterate through them.

How would I do this? would I create an employees class or would I use a collection?


 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the collection of employees has no state of its own except for the list of employees, then I'd just use a List implementation.
 
Vaibhav Gargs
Ranch Hand
Posts: 116
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a collection such as List to hold employee objects such as:

 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or just
 
Jeremy Lautman
Greenhorn
Posts: 4
AngularJS Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would use a Collection<Employee>. That seems to best describe the use case you're aiming for. Can you give us more information on what you need the collection to do?
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, there's no get() method if you do that.
 
Steven Greenbaum
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the beginning of my main class

In line 38 I have an array list of employees.


Instead of this I would like to create an Employees calss that would encapsulate this functionality
I would like to have methods in this class to add and delete employees

I was not sure how to set up the Employees class- see stub below
thank you.




 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steven Greenbaum wrote:Instead of this I would like to create an Employees calss that would encapsulate this functionality
I would like to have methods in this class to add and delete employees


Okay; you know that your Employees class has to contain a set of Employee objects. You've chosen an ArrayList, which may or may not be the best choice but you could start with that anyway. And you know two of the methods which the class should have, so you could write stubs for those two methods. Give that a try and let us know how it goes.
 
Steven Greenbaum
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is my attempt
Haven't tested yet with main class

 
Steven Greenbaum
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ran into interesting problem. I forget that I needed to return the size og the Array List see line 18

However I get an error message saying that size is private. How do I get around this?

 
Steven Greenbaum
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Solved the size issue line 16
Now last issue (I hope)

my code for getEmployee  line 13-15 not working
"cannnot find symbol employees(int)"


 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You MUST keep in mind the difference between an instance of your 'Employee' class and the ArrayList that that instance contains. Right now, you mix their uses which lead to errors.

For instance:

Here you have a method 'getEmployee' in you instance, that must retrieve an element of the ArrayList. Therefore, the code should be:
In

you must add 'thisEmployee' to the ArrayList and NOT to the instance, so:
Can you predict what happens if you invoke 'add(newEmployee)' in its current form?
 
Steven Greenbaum
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to Java so am not sure of the answer to your question. Can't experiment because computer off due to hurricane Irma. Am writing this from iPad.

However I have another question. In my main program you can see that I hard coded the inputs. I realize that I would like to have another class that generates employees and then passes the employees object into main. It is not intuitively obvious how to input an instance of the employees class into main so that I don't have to hard code the values. Hope I am being clear. Thank you.
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Steven,

first of all: I hope that you and all yours won't be hurt in any way by this horrible "Irma". You are in our thoughts.

Well, a simple class to generate new Employees could be:

In your Employees main method you can then simply do:

Since it is quite possible to generate employees with equal first- and lastnames, it is wise to add an ID to your Employee class, for instance:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!