• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help writing a program to add an manage employees  RSS feed

 
Ben Blackburn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, I'm new here and new to java programming and I have a rather large task to complete. I've only just started and got to around number 3) before I started thinking maybe I'm not writing the best code. I'll post the code and if anyone has any tips or spots any errors to please let me know so I can write better code. Also if anyone has any hints for the rest of the questions I would be grateful for any help you can provide as some of the stuff I have not come across before. Thank you
I have to use an array to write a program to store and manage a set of employees working in a small supermarket according to the following requirements:

1) There is a need to record details about each employee in the supermarket.

2) The application must allow for up to a maximum of 10 employees to be working in the supermarket.

3) For each employee, their Name, Address, Date of Birth and Gender must be stored.
Note: Create an array of type Employee where Employee is a class you create containing the name, address, DOB and gender as instance variables. You must check that the Gender has been entered correctly in whatever format you specify. You must also inform the user that if any of the fields are left blank for an employee, the employee's full details will not be saved beyond that current session.

4) The program needs to enable Employee's to be added to the supermarket's details and also to enable Employee's to be deleted from them.
Note: Deleting data from an array may leave a blank space in the array, so keep a count of the number of employees and search for the next free array cell when adding another employee.

5) It must be possible to report (via a print to the screen) on the supermaket. This report should include the name of the supermarket, the name of the supermarket owner, the total number of employees (excluding the owner) that are currently working there and the percentages of the workforce that are male and female.

6) At the end of a session, when the program is being terminated, the supermarket's details (from part 5) ) and the details of each employee must be written to disk. The two sets of details must be saved to two standard text files. The supermarket's details are to be saved to a file named" SupermarketDetails.txt" and the employee details are to be saved to a file named "EmployeeDetails.txt".
Note: Do NOT save blank records to disk, check that there is a valid "Employee" object in each array cell before writing its contents to disk. If any of the fields within an employee's details have been left blank (string length of zero) then that employee is invalid and their details should not be written to disk.

7) When the program is started, it should read from these standard text files to re-populate the application with any previously stored data as a starting point.
Note: Each record requires a new instance of the Employee class to be created and linked to the next available free cell in your array.

8) The program user must also be able to search for an employee by name, causing the sought employee's details to be displayed if present.
Note: You can assume each employee's name will be unique


Employee Class



Supermarket Class


Records Class
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks ok so far.

Here are some things to think about:

You are storing your employees in a variable called 'supermarket'. Consider renaming it.
Date of birth might be better represented as a Date object rather than a string object.
Similarly Gender might be better off as a different data type. I know I would use an Enumeration, but you may not have learned about those yet.
Does a supermarket have details other than its employees? You could perhaps make the Supermarket name and owner instance variables of the Supermarket class.

Rather than having a "printDetails" method, you might consider implementing getter methods or a "toString()" method for each object.
For instance the employee class has private variables (great).
You set these up originally in the constructor, but the only way to get at them right now is to print them out via your printDetails method..
If you instead provide a method toString() which returns a String representation of your object, you can then just for instance



The advantage of that is you can then do things with it OTHER than printing it out.
For instance having some junit tests that make sure everything is working :-)
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Ben Blackburn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help so far guys. I have since changed the name from supermarket to list, since it is basically a list of employees. I was reading about enums just now and you're right we have not come across those yet. I was thinking earlier would it be possible to attach gender to an integer type, e.g. 1=male , 2=female and just enter 1 or 2 when adding an employee? I was wondering if this would also make it easier to verify a valid gender has been entered?

I have never used the Date object up to now but it would be nice to be able to incorporate it into my code. How would I go about writing it?

I have made the company and owner name instance variables of the Supermarket class
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Blackburn wrote: . . . would it be possible to attach gender to an integer type, e.g. 1=male , 2=female and just enter 1 or 2 when adding an employee?
Yes. But it is possible for somebody to enter 3.
I was wondering if this would also make it easier to verify a valid gender has been entered? . . .
No. If you use an enumerated type (SE spelt it wrongly as enumeration) then you don't have to validate anything. You have MALE and FEMALE and that is it. All you need to validate is that you didn't pass null.

I am not convinced that list is a better name for your list of employees; it could be a list of anything. What about staff?
 
Ben Blackburn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Changed the name again to staff. I have since completed 1-3 and some of 4. Have the verification of gender completed and the add method. Now I need a delete method. Bare in mind I have to use only Array, not ArrayList so I don't have some useful methods available to me. Any ideas anyone?
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops. My bad. Yes I did mean Enumerated type, rather than enumeration. That's the problem with thinking of them as "enums"...

According to the specification you have for delete, the delete itself should be easy - just make that array entry null.
It is the repercussions in the add and print methods that will provide the challenge.

What thoughts have you got?
Is the user going to say "Delete Joe"? or "Delete Employee #2"?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you remove a value from an array you have three possibilities that I can think of
  • 1: Simply delete the value by assigning an array element to null. This may mean you have the dangerous situation of having nulls running loose in your program.
  • 2: Create a new array 1 smaller. Copy all elements of the array to the new one, except the one you are deleting. Much more time‑consuming, but always leaves your program in a safe state.
  • 3: Have a counter variable. When you add a new element increment the counter. When you remove an element, reduce the counter by 1 and move all the array elements 1 place up. Don't iterate the whole array, only up to the counter.
  • It is worth knowing about array copying methods. Start here: 1 2.
     
    Ben Blackburn
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yeah I that was my original thought to assign the element to null but I didn't know how to access the element. Like I was thinking I would need the index of the element I wish to nullify and in Array there is no indexOf method, that method is only in ArrayList. We are not to use ArrayList. I wanted the user to call something like Delete Joe Bloggs, can you null an array without finding the index first and only use something like the name of the employee you want to delete? Later in the questions I'll have to add a check to find an empty array cell before adding a new employee to the array so that will keep the amount of nulls low anyway. There is a counter in place to keep track of the amount of employees already in the array. In the add method it increases the counter by one and I was going to have it decrease by 1 after deleting a student. I don't know man this set of questions is pissing me off lol.
     
    Campbell Ritchie
    Marshal
    Posts: 56536
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You would have to search the array to find the index of a particular element.
     
    Campbell Ritchie
    Marshal
    Posts: 56536
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think it would be a bad idea to have to look for null array elements before inserting a new element. I think you are better off with one of the options which don't put nulls in the middle of the array.
     
    Ben Blackburn
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It says in question 4) "Note: Deleting data from an array may leave a blank space in the array, so keep a count of the number of employees and search for the next free array cell when adding another employee". I agree it's probably not the best way but it's what I'm asked to do and may be penalised otherwise.
     
    Campbell Ritchie
    Marshal
    Posts: 56536
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If that is what you have been told to do, then that is what you have to do. You will have to deal with the nulls when you iterate your array; the simplest way to do that is probably wrapping things in
    if (employee != null) {...}
     
    Campbell Ritchie
    Marshal
    Posts: 56536
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I don't think question 4 instructs you to delete an Employee in any particular manner. You would do well to query that.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!