• Post Reply Bookmark Topic Watch Topic
  • New Topic

Employee Database  RSS feed

 
Daniel Jenkins
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

So, I am a first time programmer and this is literally my first programing assignment ever. I have to create an employee database application using eclipse. See the below instructions. I need some help on how to get started. I know i will be using strings, the scanner class, and arrays.... thats all I know. For now, I need help planning this......how its going to be structured and what classes I should use. Once i have some code started I can post it for feedback and suggestions.


Assignment
Create the initial code for an employee database application.

The required functionality & components are:
1. An Employee class that contains data about individual employees
  a. Fields (all Strings): Last name, first name, department, and job
  b. Methods:
     i. Set: One “set” method (e.g., setLastName) for each field, which accepts a String object as a parameter and sets the field to the same value as that String
     ii. Get: One “get” method (e.g., getLastName) for each field, which returns a String object and leaves the field unchanged
2. An EDA class that maintains records of employees:
   a. Fields:
      i. an array of Employee objects (initial size set to 100)
      ii. a counter (type int) for the current Employee count
   b. Methods:
      i. Modify: Requires a field name, a match value, and a new value. For any Employee objects whose field matches the match value, the field will be changed to the new value using the appropriate set method
     ii. Add (full): Requires four Strings, which will be used to create a new Employee object at the lowest-index unused array entry. Creation will be accomplished via a constructor method. Counter will be incremented.
        1. Add (partial 1): Create Employee object using only last name & first name. Department and job will be set to “Unassigned”. Counter will be incremented.
         2. Add (partial 2): Create Employee object using only last name, first name, & department. Job will be set to “Unassigned”. Counter will be incremented.
     iii. Delete: Requires four Strings, which will be used to identify the correct Employee object. Any Employee objects for which all fields match these four fields will be deleted. The array will be compressed and the counter will be decremented.
     iv. Read: Requires a field name and a match value. For any Employee objects whose field matches the match value, the full set of four fields will be displayed on a new line on the console.
c. Inputs:
     i. All inputs and output should take place using the Eclipse console
     ii. Input format: Words in a multi-word input should be separated by a single space character and be on a single line (i.e., only one Enter keystroke per input)
     iii. Inputs should be read using a Scanner object
     iv. Inputs should support using a wildcard character “*” as a match value (which will in effect display all Employee records)


Program Code Constraints
The program should be written as a single .java file (the Employee class should be included in the EDA.java file, after the EDA class definition). Each of the required functionalities should be implemented as separate methods (as far as possible) and operate by passing parameters and/or returning values. Comments should be provided for the class and for each variable and method, explaining
 
Carey Brown
Bartender
Posts: 3021
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your instructions are pretty detailed. You should be able to start at the top and follow them. For your first pass you might not worry about the body of the methods, just stub them out. Let's see what you can put together and let us know where specifically you are stuck.
 
Carey Brown
Bartender
Posts: 3021
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know what getters and setters are?
 
salvin francis
Bartender
Posts: 1612
36
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Daniel Jenkins,
Welcome to coderanch

Lets get started with your assignment. I am sure you are as excited about it as I was when I got my first assignment ;)

First of all, are you able to write a Hello world program ? (Pardon me for asking, since you said that this is literally your first assignment)
If yes, lets start by writing it here.
 
Daniel Jenkins
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok here is what i have for part 1 A & B. Is this correct?


 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's very good, except you weren't asked for any constructors.
Try and avoid the temptation (at least for now) of putting in more than is being asked for in the requirements.
You run the risk of clogging up your code with things that aren't actually going to be used.

One (very very minor) comment.
lastname should probably be lastName.
Same goes for firstname.
Camel case works better than all lower case.  It's easier to read.

If you do change that, don't forget to change the set and get methods names to reflect that.
 
Liutauras Vilda
Marshal
Posts: 4666
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also keep your instance variables private, so nobody could access them directly, but rather only through setter methods which you created already.

And welcome to the Ranch
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Also keep your instance variables private, so nobody could access them directly, but rather only through setter methods which you created already.


And this is a good example of why an extra pair of eyes helps.
I completely missed that!
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:That's very good, except you weren't asked for any constructors. . . . Camel case works better than all lower case.  It's easier to read.

If you do change that, don't forget to change the set and get methods names to reflect that.
I would take ti for granted that you supply a constructor for every class myself, but I would delete that no‑arguments constructor. It allows somebody to instantiate your class without initialising all fields to correct values.
We usually warn against using IDEs at this stage in people's careers, but an IDE probably has a refactoring tool which allows you to do all those name changes very quickly.

And welcome again
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I would take ti for granted that you supply a constructor for every class myself, but I would delete that no‑arguments constructor. It allows somebody to instantiate your class without initialising all fields to correct values.


There's a lot of tools that rely on a no args constructor.
Hibernate for example.

My point, however, was that this was not part of the requirements, and the OP is a beginner who needs to get to grips with the idea of following requirements.
When they have a bit more under their belt then "assumed requirements" (essentially standard design decisions) can start to appear.
 
Daniel Jenkins
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have fixed the code according to all of your suggestions, I am not sure if I did what you guys meant so let me know if I did something wrong. Once I get confirmation that this is correct I will move on to the next steps of the assignment.
 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The above looks good.  You have this in the instructions, though:
The program should be written as a single .java file (the Employee class should be included in the EDA.java file, after the EDA class definition). 

Luckily, you can copy and paste this class into your EDA.java class, with one change: the class can no longer be public.
 
Daniel Jenkins
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain to me how to do this ? ^^
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the EDA.java file:
 
Liutauras Vilda
Marshal
Posts: 4666
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A bit more on that: within the *.java (source) file, only one class can be public and the class declaration needs to match with the *.java filename. Nevertheless, one source file (*.java) can have many classes inside, but again only one can be public and the others must be without public access modifier (in such cases, declared class names do not match source filename).
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:. . . There's a lot of tools that rely on a no args constructor.
Hibernate for example. . . .
But I thought at this stage he hadn't heard of them, nor of the Bean pattern which uses the no‑arg constructor and all those setXXX and getXXX methods.
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Dave Tolls wrote:. . . There's a lot of tools that rely on a no args constructor.
Hibernate for example. . . .
But I thought at this stage he hadn't heard of them, nor of the Bean pattern which uses the no‑arg constructor and all those setXXX and getXXX methods.


Well yes, but he's also learning how to go through requirements, and so adding bits before you know what you're doing is probably not a good idea.
We see it often enough here, after all.

To be honest, he'd have to stick the constructor back in anyway when he got round to 2b(ii), so it's all a little moot.
 
Daniel Jenkins
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for Part 2 is this a good start? how do I add "a counter (type int) for the current Employee count" as stated in the instructions

 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Jenkins wrote:for Part 2 is this a good start? . . .
No. If I were marking, I would take off lots of marks for style because you appear to be using two statements per line. If you want good marks, you will have to past correctly indented code (following your local code conventions).

You also have code which probably won't compile because person. appears mysteriously in line 9. If I can't see it anywhere else, nor can the compiler, and it won't be pleased. Have you got your Employee class working correctly? If not, don't attempt part 2. Finish part 1. Also read the interchange between DT and myself, which will tell you wnat you have forgotten to implement (at least I think you have, from today's code).
 
Daniel Jenkins
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok is this better?

 
Daniel Jenkins
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The employee class is all good to go as far as i know.. Does anyone know how to add a counter (type int) for the current Employee count,   as stated in the instructions ?
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does that even compile?
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Employee will compile but the other class won't.
Please remind us what the instructions said about a counter. (And remind yourself.)
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I think Employee will compile but the other class won't.


The whole thing is now in a single .java file, so nothing's going to get produced.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just because the whole code has been posted in one pair of code tags doesn't mean it is all in the same file. So I tried it and found lots more errors I hadn't expected.
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Knute posted earlier from the requirements:
The program should be written as a single .java file (the Employee class should be included in the EDA.java file, after the EDA class definition).

So this is a single file (now).
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some requirements are written so as to be a help to the students, and some are simply a hindrance. I am afraid that one file requirement comes into the second category. But there is nothing we can do about it now.
 
Daniel Jenkins
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Just because the whole code has been posted in one pair of code tags doesn't mean it is all in the same file. So I tried it and found lots more errors I hadn't expected.


can you or anyone fix the errors for me and repost the code?? i have no clue how to fix them......i tried
 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To start with, any code that's not a declaration needs to be in a method.  So your for loop should be in a method.  Next, if this class is going to run on its own, you'll need a main method that looks like this:

A common mistake is to put all your code in the main method.  The main method should do two things: create an object and run a method.  It might look like this:

For this to work, you will need a run method in EDA.  Create that and put your for loop in it.  See if it compiles.  Post your new code in a reply.
 
Daniel Jenkins
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok i did what you said but still I have errors on line 16 32,34,35,36,37. do you know how to fix it?


 
Daniel Jenkins
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do I add a counter to count the number of employees?
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by writing down with pencil and paper what a counter is supposed to do. Once you have worked that out, it should become easy enough to work out which class to put it in.
 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok i did what you said but still I have errors on line 16 32,34,35,36,37. do you know how to fix it? 


Is the for loop inside a method?
 
Daniel Jenkins
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am done with the assignment. How does it look? any feedback or suggestions ?

 
Carey Brown
Bartender
Posts: 3021
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Print formatting on line 260 is repeated several times. I would move that to an Employee.toString() method.

Line 229: Create an Employee.equals() method and use it here in place of the multiple equals() calls.

Move line 255, toLowerCase(), outside of loop.

In your loop starting at line 254 you have multiple tests for matchValue=='*'. These do nothing but cause the entire Employee array to be printed. I'd do this test outside of your loop and have it conditionally run a simple print loop or the loop you have now (sans matchValue=='*').
 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice Job! 

Here's a minor thing:  when you have field.equals("literal") it's often good to write:

This looks odd, but it makes sure that you can't have a null pointer exception.

It's recommended that you write an if statement with braces even if there is only one statement:

this is called "defensive programming" as it makes it harder for a later editor (maybe even you!) to add a statement which is outside the if.

The above is called a "magic number", that is, what is 4?  It's a good idea to get in the habit of creating constants and using them instead of magic numbers:

(Looking at the code, this will not be as simple as that to implement, but the note stands.)

Here's something about comments:

You have stated in the comment what can plainly be seen in the code, but what is the significance of "*"?  I can guess that it means "match all", but it would be nice to put that in the comment.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!