# Program which calculates earnings (with some difficulties)

**URL:** <https://www.ruby-forum.com/t/program-which-calculates-earnings-with-some-difficulties/113477>\
**Category:** Ruby\
**Created:** [August 31, 2007, 2:08pm UTC](https://www.ruby-forum.com/t/program-which-calculates-earnings-with-some-difficulties/113477 "2007-08-31T14:08:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tillaart36](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47774/32.png) [@tillaart36](https://www.ruby-forum.com/u/tillaart36)\
**Post date:** [August 31, 2007, 2:08pm UTC](https://www.ruby-forum.com/t/program-which-calculates-earnings-with-some-difficulties/113477/1 "2007-08-31T14:08:54Z")

</div>

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!!

Thanks in ADVANCE!!!

Greets, tillaart36!

---

<div class="post-metadata">

**Author:** ![tillaart36](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47774/32.png) [@tillaart36](https://www.ruby-forum.com/u/tillaart36)\
**Post date:** [August 31, 2007, 2:50pm UTC](https://www.ruby-forum.com/t/program-which-calculates-earnings-with-some-difficulties/113477/2 "2007-08-31T14:50:39Z")

</div>

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  
pay will be adjusted correctly.
5. Modify your program to print money earned, instead of hours.  
This step is now just simple math.

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

James Edward G. II

---

<div class="post-metadata">

**Author:** ![tillaart36](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47774/32.png) [@tillaart36](https://www.ruby-forum.com/u/tillaart36)\
**Post date:** [August 31, 2007, 3:05pm UTC](https://www.ruby-forum.com/t/program-which-calculates-earnings-with-some-difficulties/113477/3 "2007-08-31T15:05:08Z")

</div>

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  
yourself) take the time to think about this problem and come with ideas  
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!

---

<div class="post-metadata">

**Author:** ![tillaart36](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47774/32.png) [@tillaart36](https://www.ruby-forum.com/u/tillaart36)\
**Post date:** [August 31, 2007, 3:40pm UTC](https://www.ruby-forum.com/t/program-which-calculates-earnings-with-some-difficulties/113477/4 "2007-08-31T15:40:47Z")

</div>

On 31/08/2007, Joop Van den tillaart [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.  
removed\_email\_address@domain.invalid  
removed\_email\_address@domain.invalid  
030- 49 78 37 06  
0176 - 75 03 03 04

> **[thopre](https://thopre.wordpress.com/)**
>
> Entwicklungen für die Bühne des Lebens

> **[ThoPre.com is for sale | HugeDomains](https://www.hugedomains.com/domain_profile.cfm?d=thopre.com)**
>
> The leading source brandable domain names. Competitive prices. Excellent service. Get your domain name today.

---

<div class="post-metadata">

**Author:** ![tillaart36](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47774/32.png) [@tillaart36](https://www.ruby-forum.com/u/tillaart36)\
**Post date:** [September 7, 2007, 2:19am UTC](https://www.ruby-forum.com/t/program-which-calculates-earnings-with-some-difficulties/113477/5 "2007-09-07T02:19:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tillaart36](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47774/32.png) [@tillaart36](https://www.ruby-forum.com/u/tillaart36)\
**Post date:** [September 7, 2007, 3:03am UTC](https://www.ruby-forum.com/t/program-which-calculates-earnings-with-some-difficulties/113477/6 "2007-09-07T03:03:46Z")

</div>

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
