This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Frequency of used object in ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Frequency of used object in ArrayList" Watch "Frequency of used object in ArrayList" New topic

Frequency of used object in ArrayList

Jason Job

Joined: Sep 26, 2011
Posts: 17
Firstly, I don't know if I should make a new thread when I have a new question, or if I should continue posting in my old thread. For now, here is my new thread, and I hope someone can help!

General Overview

The idea of this lab assignment is to generate several employees (of both ProductionWorker and ShiftSupervisor type which are sublcasses of Employee). Then create 14 Shifts of employees (7 days a week, night and day shift), consisting of 5-10 employees and 1 supervisor, whether goals were made, etc.

What the Assignment is Requesting

Part #5: Testing your implementation of all the classes.
Write a program which tests implementation of your classes by computing weekly payroll for the company:
• Create a data structure to contain a week worth of shift data. Assume that the company is open 7 days a week and each day there are two 8 hour shifts.
• Initialize created data structure to contain a week worth of shift data. Use class Random to initialize the fields in the corresponding classes. It is a good idea is to re-use the code which creates an ArrayList of employees in part #3 to store all the employees who work for the company. You may want to make this ArrayList a bit larger than 20 entries. Once such an ArrayList is created you can use it to randomly retrieve employee data to populate the data structure which contains weekly shift information.
• Display the weekly shift data. For each shift display the following information: day, shift (i.e., day or night), names and position of employees who worked during that shift (i.e., production worker or shift supervisor), and whether the production goals were met during this shift
• Compute and display weekly payroll. When computing weekly payroll assume that:
o Night shift production workers, but not the supervisor, earn an extra 20% of their hourly rate. All production workers earn extra 5% percent of their total shift earnings if the production goals were met during their shift.
o The shift supervisor earns a weekly bonus if and only if his or her shift meets production goals 4 or more times a week. The weekly bonus is computed as percentage of weekly salary (assume 52 weeks per year) according to the number of years supervisor was working for the company:
• 0 - 5 years : bonus is 5% of weekly salary
• 6 – 10 years: bonus is 10% of weekly salary
• 11 – 20 years: bonus is 12% of weekly salary
• More than 20 years: bonus is 15% of weekly salary

To compute the payroll, first process the weekly shift data structure updating the weekly earnings for all the production employees and updating the number of times production goals were met for the shift supervisors. You may want to keep a separate data structure which contains information about all employees who were working for the company that week.

Next, you update weekly earnings for shift supervisors (you need to compute the number of times production goals are met that week before you can compute weekly salary for the shift supervisor) and compute the total payroll.

Finally, display all employees who worked for the company during that week (i.e., do not display employees who did not work that week), their weekly earnings, and the total amount of money the company paid to all of its employees that week.


Employee ~ COMPLETED : Intakes a name and the various other fields defined in the project
ProductionWorker ~ COMPLETED : Intakes a name and the various other fields defined in the project
ShiftSupervisor ~ COMPLETED : Intakes a name and the various other fields defined in the project
WorkForce (generates 40 ProductionWorker’s and 5 ShiftSupervisors) ~ COMPLETED, makes two different ArrayList’s, one for PW’s and one for SS’s.
Shift ~ COMPLETED requires a pass of a ShiftSupervisor, the Day, whether it’s day or night, and whether or not goals have been met.
WeeklySchedule (Generates 14 shifts) ~ INTERIM : Currently generates 14 shifts, but I am truly unsure how to figure out how frequently a certain employee has worked with my current setup.
and finally – PayRoll ~ Will compute and display weekly payroll after doing calculations.

My Question
I am wracking my brain trying to figure out how to see how often X employee worked, and what time of night. I imagine I need some other sort of ArrayList in addition to the one that is being made for the weekly shifts, but I really can't wrap my brain around how to set this up. Once I figure this out I feel like I can generate a proper payroll.

The Code
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Jason Job wrote:Firstly, I don't know if I should make a new thread when I have a new question, or if I should continue posting in my old thread. For now, here is my new thread, and I hope someone can help!

If it is a new question then open a new topic. If it is a related question then post in the same topic.

I don't really see your problem. Just iterate through the shifts and create a List per worker with the shifts he/she worked.

Also (dayOrNight == 0 ? true : false) should be written as dayOrNight == 0.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 43905
The hoursWorked field should be part of the Worker class. General design principle: each class is responsible for its own business. The hours worked relate to the Worker, so the Worker should record the hours.

Now that enums are available, it is not a good idea to use ints for day shift/night shift. Create a ShiftType enum and use that to test for day/night. You can of course include things like duration of shift in the enum’s members. You can select branches in a switch-case structure with enum members.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
If it were me, I would not store hoursWorked on the Employee class. I would create an association between the Employee and the hours. I would create a class Work whose attributes are Employee, Shift and hours. This class could be stored in any kind of data structure, and all I would have to do is get all the instances of Work for a particular Employee (maps are handy for this) and sum the hours attribute.

It seems to me this problem is as much about designing a data model as it is using data structures. When you design a class you should always be wary of including attributes that are non-essential. It might help to draw a picture that shows the things in your problem space and their relationships.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19719

In real life it's always about the data model, but not necessarily in homework assignments. So I'm impressed to see a homework assignment which bears some relationship to reality. (Sometimes from what I see in forums, it looks like CS education is mostly for teaching people things which have to be unlearned after they get out of school.)

But +1 to Dennis. Here's the reasoning behind that: You want to know how many hours some employee worked on some shift? Then those hours aren't specific to the employee, because they depend on the shift. And they aren't specific to the shift, because they depend on the employee. So where should they go? Somewhere else, of course. This means we have to create a new place for them.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 43905
I’ll agree with that.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Frequency of used object in ArrayList
It's not a secret anymore!