# Program which calculates earnings (with some difficulties)

**URL:** <https://rubytalk.org/t/program-which-calculates-earnings-with-some-difficulties/40291>\
**Category:** ruby-talk\
**Created:** [31 August 2007 12:08 UTC](https://rubytalk.org/t/program-which-calculates-earnings-with-some-difficulties/40291 "2007-08-31T12:08:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joop\_Van\_den\_tillaar](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@Joop\_Van\_den\_tillaar](https://rubytalk.org/u/Joop_Van_den_tillaar)\
**Post date:** [31 August 2007 12:08 UTC](https://rubytalk.org/t/program-which-calculates-earnings-with-some-difficulties/40291/1 "2007-08-31T12:08:59Z")

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [31 August 2007 12:48 UTC](https://rubytalk.org/t/program-which-calculates-earnings-with-some-difficulties/40291/2 "2007-08-31T12:48:36Z")

</div>

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 Gray II

> **···**
>
> 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.

---

<div class="post-metadata">

**Author:** ![Thomas\_P](https://avatars.discourse-cdn.com/v4/letter/t/8797f3/32.png) [@Thomas\_P](https://rubytalk.org/u/Thomas_P)\
**Post date:** [31 August 2007 13:39 UTC](https://rubytalk.org/t/program-which-calculates-earnings-with-some-difficulties/40291/3 "2007-08-31T13:39:59Z")

</div>

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

> **···**
>
> On 31/08/2007, Joop Van den tillaart \<tillaart36@hotmail.com\> 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
> 
> --  
> Thomas Preymesser  
> thopre@gmail.com  
> thomas@thopre.com  
> 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:** ![Jeremy\_Woertink1](https://avatars.discourse-cdn.com/v4/letter/j/6a8cbe/32.png) [@Jeremy\_Woertink1](https://rubytalk.org/u/Jeremy_Woertink1)\
**Post date:** [7 September 2007 00:19 UTC](https://rubytalk.org/t/program-which-calculates-earnings-with-some-difficulties/40291/4 "2007-09-07T00:19:35Z")

</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)  
&nbsp;&nbsp;@payrate = earnings \* 1.85 if NIGHT\_SHIFT.include?(time)  
&nbsp;&nbsp;@payrate = earnings \* 1.35 if EARLY\_SHIFT.include?(time)  
&nbsp;&nbsp;@payrate = earnings \* 1.00 if DAY\_SHIFT.include?(time)  
&nbsp;&nbsp;@payrate = earnings \* 1.35 if LATE\_SHIFT.include?(time)  
&nbsp;&nbsp;@payrate = earnings \* 1.60 if EVENING\_SHIFT.include?(time)  
&nbsp;&nbsp;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|  
&nbsp;&nbsp;daily\_pay \<\< get\_pay\_rate((time.to\_s.rjust(2, "0") \<\< ":00"),  
earnings)  
end

~Jeremy

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Joop\_Van\_den\_tillaar](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@Joop\_Van\_den\_tillaar](https://rubytalk.org/u/Joop_Van_den_tillaar)\
**Post date:** [31 August 2007 13:05 UTC](https://rubytalk.org/t/program-which-calculates-earnings-with-some-difficulties/40291/5 "2007-08-31T13:05:24Z")

</div>

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

> **···**
>
> > On Aug 31, 2007, at 7:08 AM, Joop Van den tillaart wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Jeremy\_Woertink1](https://avatars.discourse-cdn.com/v4/letter/j/6a8cbe/32.png) [@Jeremy\_Woertink1](https://rubytalk.org/u/Jeremy_Woertink1)\
**Post date:** [7 September 2007 01:04 UTC](https://rubytalk.org/t/program-which-calculates-earnings-with-some-difficulties/40291/6 "2007-09-07T01:04:00Z")

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
