This repository was archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemployee_o_matic_4000.rb
More file actions
176 lines (151 loc) · 4.88 KB
/
employee_o_matic_4000.rb
File metadata and controls
176 lines (151 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
class Employee
attr_accessor :full_name
attr_accessor :id
def initialize(full_name, id)
@full_name = full_name
@id = id
end
def last_name
@full_name.split(' ', 2).last # split into 2 substrings, first and last name, take the latter
end
def first_name
@full_name.split(' ').first
end
end
class Programmer < Employee
attr_accessor :languages
def initialize(full_name, id, languages)
super(full_name, id)
@languages = languages
end
end
class OfficeManager < Employee
attr_accessor :office
def initialize(full_name, id, office)
super(full_name, id)
@office = office
end
end
def add_employee(employees) # adds an employee to array
puts '[Add an employee]' # param +employees+ array of employees
print 'Full name: '
full_name = gets.chomp
print 'ID: '
id = gets.chomp
position = nil
loop do # ask for input until correct format is entered
print 'Is this person an [e]mployee, [p]rogrammer or an [o]ffice manager?'
position = get_action
case position
when 'e' then break
when 'p' then break
when 'o' then break
end
end
case position
when 'e' then
employee = Employee.new(full_name, id)
when 'p' then
print 'Languages: '
languages = gets.chomp
employee = Programmer.new(full_name, id, languages)
when 'o' then
print 'Office: '
office = gets.chomp
employee = OfficeManager.new(full_name, id, office)
end
employees << employee
end
def edit_employee(employees)
puts 'Who do you want to edit?' # edits employees
print 'Full name: ' # param +employees+ array of existing employees
full_name = gets.chomp
print 'ID: '
id = gets.chomp
employee = employees.find do |employee|
employee.full_name == full_name && employee.id == id
end
unless employee.nil?
puts '[Selected employee]'
puts "#{employee.full_name}, #{employee.id}"
print 'New full name: '
new_full_name = gets.chomp
print 'New ID: '
new_id = gets.chomp
if employee.is_a?(OfficeManager)
print 'New office: '
new_office = gets.chomp
employee.office = new_office unless new_office.empty?
elsif employee.is_a?(Programmer)
print 'New languages: '
new_languages = gets.chomp
employee.languages = new_languages unless new_languages.empty?
end
employee.full_name = new_full_name
employee.id = new_id
return # if an edit succeeded, exit method
end
puts 'No matching employee found.'
edit_employee(employees) # else, call method once again
end # it could be done using loop do...end through the whole method,
# but it looks weird, I'm not aware if it could cause any problems
def sort_employees(employees, argument)
employees.sort_by do |employee|
case argument
when 'f' then employee.first_name
when 'l' then employee.last_name
end
end
end
def get_action
gets.downcase[0]
end
def print_employee(employee, argument)
case argument
when 'f' then print "#{employee.first_name} #{employee.last_name}, #{employee.id}" # prints an employee in "first/last name, last/first name, id" format
when 'l' then print "#{employee.last_name}, #{employee.first_name}, #{employee.id}" # param +employee+ employee with according hash you want printed
end # param +argument+ if 'f' then first way of printing is used, if 'l' the other way is used
case employee
when OfficeManager then puts ", #{employee.office}"
when Programmer then puts ", #{employee.languages}"
when Employee then print "\n"
end
end
def view_employees(employees)
argument = nil
loop do # ask for input until correct format is entered
puts 'Sort by [f]irst name or [l]ast name? '
argument = get_action
break if argument == 'f' || argument == 'l' # prints all employees
end # param +employees+ array of employees you want to print
puts '[List of employees]'
sort_employees(employees, argument).each do |employee|
print_employee(employee, argument)
end
end
def quit
puts 'Goodbye!'
exit
end
def print_action_help
puts '[HELP]'
puts 'Enter one of the following: '
puts 'a - to add a new employee'
puts 'e - to edit existing employees'
puts 'v - to view existing employees'
puts 'q - to quit the program'
end
puts 'Employee-o-matic 4000'
employees = []
loop do
print 'What do you want to do? '
action = get_action
case action
when 'a' then add_employee(employees)
when 'e' then edit_employee(employees)
when 'v' then view_employees(employees)
when 'q' then quit
else
print_action_help
end
end