This repository was archived by the owner on Jan 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise10.rb
More file actions
78 lines (65 loc) · 1.3 KB
/
Copy pathexercise10.rb
File metadata and controls
78 lines (65 loc) · 1.3 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
# Exercise 10
class Customer
attr_reader :id, :waiting
def initialize(id)
@id=id
@waiting=0
end
def update
@waiting+=1
end
end
class Teller
def initialize(id,client_time)
@id=id
@left=0
@client_time=client_time
end
def process(client)
puts "Hello, Sir #{client.id}! I'm teller #{@id}."
puts "You have been waiting #{client.waiting} minutes!"
@left=@client_time
end
def update
if (@left>0) then @left-=1 end
end
def free?
return @left==0
end
end
class Bank
def initialize(tellers,client_time)
@time=1
@tellers=[]
tellers.times { |i| @tellers << Teller.new(i,client_time) }
@queue=[]
end
def getFreeTellers
result=[]
@tellers.each do |teller|
if(teller.free?)
result << teller
end
end
return result
end
def update
if(@time%3==0) then @queue << Customer.new(@time) end
freeTellers=self.getFreeTellers
freeTellers.each do |tlr|
client=@queue.shift
if(!client.nil?) then tlr.process(client) end
end
@tellers.each { |t| t.update }
@queue.each { |c| c.update }
puts "Time: #{@time}, Size: #{@queue.size}"
@time+=1
sleep(0.1)
end
end
def bankSimulation
bank=Bank.new(5,15) # tellers, time per client
while(true)
bank.update
end
end