# Program which calculates earnings (with some difficulties)

Hi you all,

I have a ‘problem’ which I cannot solve using spreadsheet software like
Excel.

The problem involves some little program I want to make which can
calculate my earnings of a days work by just inserting the time I start
working and the time I stop working. Because I work with different
earning percentages during a day this isn’t as easy as it sounds (at
least not for me). I tried some different approaches in Excel but
couldn’t get it to work the way I wanted it to.

I had a project once with some work done in Ruby (mostly NOT done by me
so I’m not a pro at Ruby :D) and now I thought maybe you Ruby experts
can come up with an idea to get this done.

Here are the percentages:

Hours between: 00:00 - 05:00 = earnings per hour * 1.85 (night shift)
Hours between: 05:00 - 07:00 = earnings per hour * 1.35 (early morning
shift)
Hours between: 07:00 - 16:00 = earnings per hour * 1.00 (day shift)
Hours between: 16:00 - 18:00 = earnings per hour * 1.35 (late afternoon
shift)
Hours between: 18:00 - 00:00 = earnings per hour * 1.60 (evening shift)

An extra dimension to the problem is added because of the fact that
after each 4 hours I work, half an hour (work break) of the earnings per
hour * percentage (percentage of the 4 hours worked) is not being paid.
So these unpaid breaks are also variable in percentage.

I need a solution where I can input my starting time and ending time and
which calculates my earnings during these hours (working day). Also I
(or the program ;-)) need(s) to be able to put in different starting
times and ending times because I work with a very variable/fluctuating
working schedule…

Maybe it’s a long story but I hope you guys understand what I mean and
maybe some of you experts see a way of how to do this in Ruby. Please
let me know then and I would be higly grateful!!

Greets, tillaart36!

On Aug 31, 2007, at 7:08 AM, Joop Van den tillaart wrote:

I have a ‘problem’ which I cannot solve using spreadsheet software
like
Excel.

Neat problem.

Are you wanting us to solve it for you, or help you solve it? I’ll
hope it’s the latter and try doing that.

The biggest secret to programming is to work in small slices. Break
the problem down as much as you can and then just keep adding bit by
bit until you reach the goal.

So, I’m thinking these are some good steps to work in:

1. Write a program that accepts a start and end time from you and
just prints them to the screen. You could possibly take these in as
command-line arguments or you could prompt the user for input.
2. Modify your program to print the total number of hours between
the entered start and end time. This is a little trickier than
simple subtraction, since you could start working on one day and end
on a different day.
3. Modify your program to divide this total hour count down into the
various shifts. So instead of telling you that you worked five
hours, it would tell you that you worked four night shift hours and
one morning shift hour.
4. Modify your program to remove the half hour breaks as they
occur. Make sure this drops out of the proper shift hours, so the
This step is now just simple math.

Hope that helps. Please do ask us for ideas when you get stuck!

James Edward G. II

James G. wrote:

On Aug 31, 2007, at 7:08 AM, Joop Van den tillaart wrote:

I have a ‘problem’ which I cannot solve using spreadsheet software
like
Excel.

Neat problem.

Are you wanting us to solve it for you, or help you solve it? I’ll
hope it’s the latter and try doing that.

Okay it’s a little bit of both…I’m not asking for a total solution on
the one hand but on the other hand…I have very little expiernce in
programming let alone in working with ruby…So it will be very hard for
me to come with a (pretty) detailed piece of program which has to be
tweaked to do what I want.

Hower thanks very much for your reply and I hope others (or you
or small pieces of code.

For now I’ll dive into your ideas and I am looking forward to other
ideas and/or input from the people on this forum!

Thanks!

On 31/08/2007, Joop Van den tillaart [email protected] wrote:

Hi you all,

I have a ‘problem’ which I cannot solve using spreadsheet software like
Excel.

The problem involves some little program I want to make which can
calculate my earnings of a days work by just inserting the time I start
working and the time I stop working. Because I work with different

you could use a spreadsheet where your insert starting - and ending
hours
and then use a gem (parsexcel or roo) to access the values from the
spreadsheet. The only thing you have to do is to calculate the rate
depending on the values an sum up the final price.

-Thomas

Thomas P.
[email protected]
[email protected]
030- 49 78 37 06
0176 - 75 03 03 04

http://www.thopre.com/

Not sure if you still need this, but if so, then this should give you a
boost in the right direction.

NIGHT_SHIFT = “00:00”…“05:00”
EARLY_SHIFT = “05:01”…“07:00”
DAY_SHIFT = “07:01”…“16:00”
LATE_SHIFT = “16:01”…“18:00”
EVENING_SHIFT = “18:01”…“23:59”

def get_pay_rate(time, earnings)
@payrate = earnings * 1.85 if NIGHT_SHIFT.include?(time)
@payrate = earnings * 1.35 if EARLY_SHIFT.include?(time)
@payrate = earnings * 1.00 if DAY_SHIFT.include?(time)
@payrate = earnings * 1.35 if LATE_SHIFT.include?(time)
@payrate = earnings * 1.60 if EVENING_SHIFT.include?(time)
return @payrate
end

# Start program

puts “Payrate calculator”
puts “Enter a starting time (i.e. 13:00 or 01:59)”
start_time = Time.parse(gets.chomp)

puts “Enter ending time in the same format.”
end_time = Time.parse(gets.chomp)

puts “How much do you make?”
earnings = gets.chomp

daily_pay = Array.new

(start_time.hour…end_time.hour).each do |time|
daily_pay << get_pay_rate((time.to_s.rjust(2, “0”) << “:00”),
earnings)
end

~Jeremy

WOW! yeah, totally forget what I just posted.

note to self: testing is a good idea!

haha. I guess that’s what I get for just writing blindly

sorry,

~Jeremy

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.