-
Notifications
You must be signed in to change notification settings - Fork 0
/
catshop_clerk_2_2.erl
49 lines (34 loc) · 1.24 KB
/
catshop_clerk_2_2.erl
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
-module(catshop_clerk_2_2).
-behaviour(gen_server).
-import(rand, [uniform/1]).
-export([start_shift/0, order_cat/1, return_cat/2, end_shift/1, handle_call/3, handle_cast/2, init/1]).
%%% Client API
start_shift() ->
{ok, ClerkPID} = gen_server:start_link(?MODULE, [], []),
ClerkPID.
order_cat(ClerkPID) ->
gen_server:call(ClerkPID, {order}).
end_shift(ClerkPID) ->
gen_server:call(ClerkPID, {terminate}).
return_cat(ClerkPID, Cat) ->
gen_server:cast(ClerkPID, {return, Cat}).
%%% Server Funktionen
init([]) -> {ok, []}.
handle_call({order}, _From, State) ->
if
State == [] ->
{reply, make_cat(), State};
State =/= [] ->
{reply, hd(State), tl(State)}
end;
handle_call({terminate}, _From, State) ->
{stop, normal, ok, State}.
handle_cast({return, {cat, Name, Color}}, State) ->
{noreply, [{cat, Name, Color} | State]}.
%%% Private Funktionen
make_cat() ->
Name = lists:nth(uniform(11), names()),
Color = lists:nth(uniform(6), colors()),
{cat, Name, Color}.
names() -> ["Karl", "Peter", "Kai-Uwe", "Uwe-Kai", "Nils", "Martha", "Elisabeth", "Jonas", "Gerda", "Katharina", "Franziska"].
colors() -> [black, blue, white, orange, pink, brown].