Last active
December 22, 2015 13:49
-
-
Save easonhan007/6481564 to your computer and use it in GitHub Desktop.
emplyee pay per month
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#encoding: utf-8 | |
class Employee | |
WORK_DAY_OF_MONTH = 20 | |
DOUBLE = 2 | |
attr_reader :name, :pay_per_day | |
def initialize name, pay_per_day | |
@name = name | |
@pay_per_day = pay_per_day | |
@pay_off = @over_time = 0 | |
end | |
def pay_per_month | |
@pay_per_day * (WORK_DAY_OF_MONTH - @pay_off + @over_time) | |
end | |
def payoff=(day) | |
@pay_off = (day >= WORK_DAY_OF_MONTH)? WORK_DAY_OF_MONTH : day | |
end | |
def overtime=(day) | |
# 加班的话工资double | |
@over_time = day * DOUBLE | |
end | |
end | |
describe Employee do | |
it 'should initialize Employee with name fred' do | |
e = Employee.new 'fred', 1 | |
e.name.should eql('fred') | |
end | |
it 'should initialize Employee with name tom' do | |
e = Employee.new 'tom', 2 | |
e.name.should eql('tom') | |
end | |
it 'should initialize Employee with pay per day' do | |
e = Employee.new 'tom', 100 | |
e.name.should eql('tom') | |
e.pay_per_day.should eql(100) | |
e = Employee.new 'fred', 200 | |
e.pay_per_day.should eql(200) | |
end | |
it 'should calculate pay per month successfuly' do | |
e = Employee.new 'fred', 200 | |
e.pay_per_month.should eql(4000) | |
e = Employee.new 'tom', 100 | |
e.pay_per_month.should eql(2000) | |
end | |
it 'should calculate pay per month when payoff' do | |
e = Employee.new 'fred', 200 | |
e.payoff = 1 | |
e.pay_per_month.should == 3800 | |
e = Employee.new 'tom', 100 | |
e.payoff = 0 | |
e.pay_per_month.should == 2000 | |
end | |
it 'at most 20 days pay off a month' do | |
e = Employee.new 'fred', 200 | |
e.payoff = 100 | |
e.pay_per_month.should == 0 | |
e = Employee.new 'tom', 100 | |
e.payoff = 20 | |
e.pay_per_month.should == 0 | |
end | |
it 'should be double when ot' do | |
e = Employee.new 'fred', 200 | |
e.overtime = 1 | |
e.pay_per_month.should == 4400 | |
e = Employee.new 'tom', 100 | |
e.overtime = 1 | |
e.payoff = 1 | |
e.pay_per_month.should == 2100 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment