SHOW:
|
|
- or go back to the newest paste.
1 | #! /usr/bin/ruby | |
2 | class Employee | |
3 | ||
4 | attr_reader :name, :salary | |
5 | ||
6 | def name=(name) | |
7 | if name == "" | |
8 | raise "Name can't be blank!" | |
9 | end | |
10 | @name = name | |
11 | end | |
12 | ||
13 | def salary=(salary) | |
14 | if salary < 0 | |
15 | raise "A salary of #{salary} isn't valid!" | |
16 | end | |
17 | @salary = salary | |
18 | end | |
19 | ||
20 | def initialize(name = "Anonymous", salary = 0.0) | |
21 | @name = name | |
22 | @salary = salary | |
23 | end | |
24 | ||
25 | def print_pay_stub | |
26 | puts "Name: #{@name}" | |
27 | pay_for_period = (@salary / 365) * 14 | |
28 | formatted_pay = format("%.2f", pay_for_period) | |
29 | puts "Pay This Period: $#{formatted_pay}" | |
30 | end | |
31 | ||
32 | end | |
33 | ||
34 | employee = Employee.new("Jane Doe", 50000).print_pay_stub | |
35 | Employee.new("Jane Doe").print_pay_stub | |
36 | employee.print_pay_stub | |
37 | - | Employee.new.print_pay_stub |
37 | + | Employee.new.print_pay_stub |
38 | ||
39 | ||
40 | ||
41 | ./changemore_payroll.rb | |
42 | Name: Jane Doe | |
43 | Pay This Period: $1904.00 | |
44 | Name: Jane Doe | |
45 | Pay This Period: $0.00 | |
46 | ./changemore_payroll.rb:36:in `<main>': undefined method `print_pay_stub' for nil:NilClass (NoMethodError) |