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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
pub mod mediator_usergroupmediator;
pub mod mediator_userlist;
pub mod mediator_grouplist;
use mediator_usergroupmediator::UserGroupMediator;
fn list_to_string(items: &Vec<String>) -> String {
let mut output = String::new();
for index in 0..items.len() {
if index != 0 {
output.push_str(", ");
}
output.push_str(&items[index]);
}
return output;
}
fn mediator_setup_users(mediator: &mut UserGroupMediator) {
mediator.add_user("Stephen");
mediator.add_user("Gladys");
mediator.add_user("Marvin");
mediator.add_user("Arthur");
}
fn mediator_setup_groups(mediator: &mut UserGroupMediator) {
mediator.add_group("admins");
mediator.add_group("Users");
mediator.add_group("Power Users");
mediator.add_user_to_group("Marvin", "admins");
mediator.add_user_to_group("Arthur", "admins");
mediator.add_user_to_group("Stephen", "Users");
mediator.add_user_to_group("Gladys", "Users");
mediator.add_user_to_group("Arthur", "Power Users");
mediator.add_user_to_group("Marvin", "Power Users");
}
pub fn mediator_exercise() -> Result<(), String> {
println!("");
println!("Mediator Exercise");
let mut mediator = UserGroupMediator::new();
mediator_setup_users(&mut mediator);
mediator_setup_groups(&mut mediator);
println!(" Operation 1: Show all groups");
println!(" All groups: {}", list_to_string(&mediator.get_all_groups()));
println!(" Operation 2: Show all users");
println!(" All users : {}", list_to_string(&mediator.get_all_users()));
println!(" Operation 3: Determine if a user is a member of a specific group.");
let mut user_name = "Arthur";
let mut group_name = "admins";
let is_in_group = match mediator.is_user_in_group(user_name, group_name) {
false => "No",
true => "Yes",
};
println!(" Is user '{0}' in the '{1}' group? {2}", user_name, group_name, is_in_group);
println!(" Operation 4: Show all users in a specific group.");
group_name = "Users";
let mut user_names = mediator.get_users_in_group(group_name);
println!(" All users in '{0}' group: {1}", group_name, list_to_string(&user_names));
println!(" Operation 5: Show all groups containing a specific user.");
user_name = "Marvin";
let mut group_names = mediator.get_groups_with_user(user_name);
println!(" All groups with user '{0}': {1}", user_name, list_to_string(&group_names));
println!(" Operation 6: Remove a user from a group.");
user_name = "Marvin";
group_name = "Power Users";
mediator.remove_user_from_group(user_name, group_name);
println!(" Removed user '{0}' from group '{1}'", user_name, group_name);
group_names = mediator.get_groups_with_user(user_name);
println!(" All groups with user '{0}': {1}", user_name, list_to_string(&group_names));
println!(" Operation 7: Add a user to a group.");
group_name = "Users";
println!(" Adding user '{0}' to group '{1}'.", user_name, group_name);
mediator.add_user_to_group(user_name, group_name);
group_names = mediator.get_groups_with_user(user_name);
println!(" All groups with user '{0}': {1}", user_name,list_to_string(&group_names));
println!(" Operation 8: Remove a user from all groups.");
user_name = "Arthur";
group_names = mediator.get_groups_with_user(user_name);
println!(" Removing user '{0}' from all groups.", user_name);
println!(" Start: all groups with user '{0}': {1}", user_name, list_to_string(&group_names));
println!(" Removing...");
mediator.remove_user_from_all_groups(user_name);
group_names = mediator.get_groups_with_user(user_name);
println!(" End: all groups with user '{0}': {1}", user_name, list_to_string(&group_names));
println!(" Operation 9: Remove a user (also removes the user from all groups).");
user_name = "Marvin";
println!(" Removing user '{0}'.", user_name);
mediator.remove_user(user_name);
println!(" All users : {0}", list_to_string(&mediator.get_all_users()));
group_names = mediator.get_all_groups();
for group_name in group_names.iter() {
user_names = mediator.get_users_in_group(&group_name);
println!(" Users in group '{0}': {1}", group_name, list_to_string(&user_names));
}
println!(" Done.");
Ok(())
}