-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
167 lines (130 loc) · 5.59 KB
/
Main.java
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
package org.vladimir.ageev;
import java.util.*;
import java.util.function.*;
/*
The task was to create structure of classes, which pass the tests in main().
*/
public class Main {
public static abstract class Sendable<T> {
private final String to;
private final String from;
private final T content;
public Sendable(String from, String to, T content){
this.to = to;
this.from = from;
this.content = content;
}
public String getFrom(){
return from;
}
public String getTo(){
return to;
}
public T getContent(){
return content;
}
}
public static class MailMessage extends Sendable<String> {
public MailMessage(String from, String to, String content){
super(from,to,content);
}
}
public static class Salary extends Sendable<Integer> {
public Salary(String from, String to, Integer content){
super(from,to,content);
}
}
public static class MailMap<T> extends LinkedHashMap<String, List<T>>{
@Override
public List<T> get(Object key){
if(this.containsKey(key)){
return super.get(key);
}
else
{
return Collections.<T>emptyList();
}
}
}
public static class MailService <T> implements Consumer<Sendable<T>> {
// implement here
private final Map<String, List<T>> mailbox;
public MailService(){
mailbox = new MailMap<>();
}
@Override
public void accept(Sendable<T> o) {
if(mailbox.containsKey(o.getTo())){
mailbox.get(o.getTo()).add(o.getContent());
}
else {
mailbox.put(o.getTo(),new LinkedList<>());
mailbox.get(o.getTo()).add(o.getContent());
}
}
public Map<String, List<T>> getMailBox(){
return mailbox;
}
}
public static void main(String[] args) {
// Random variables
String randomFrom = "Vasia Pupkin"; // Random string
String randomTo = "Petia Petrov"; // Random string
int randomSalary = 100; // Random positive whole number
// Creating list of three messages
MailMessage firstMessage = new MailMessage(
"Robert Howard",
"H.P. Lovecraft",
"This \"The Shadow over Innsmouth\" story is real masterpiece, Howard!"
);
assert firstMessage.getFrom().equals("Robert Howard"): "Wrong firstMessage from address";
assert firstMessage.getTo().equals("H.P. Lovecraft"): "Wrong firstMessage to address";
assert firstMessage.getContent().endsWith("Howard!"): "Wrong firstMessage content ending";
MailMessage secondMessage = new MailMessage(
"Jonathan Nolan",
"Christopher Nolan",
"Брат, почему все так хвалят только тебя, когда практически все сценарии написал я. Так не честно!"
);
MailMessage thirdMessage = new MailMessage(
"Stephen Hawking",
"Christopher Nolan",
"Я так и не понял Интерстеллар."
);
List<MailMessage> messages = Arrays.asList(
firstMessage, secondMessage, thirdMessage
);
// Creating mail service
MailService<String> mailService = new MailService<>();
// Processing of the list of messages by the mail service
messages.stream().forEachOrdered(mailService);
// Getting and checking "mailbox" dictionary
// recipient <-> list of messages he got
Map<String, List<String>> mailBox = mailService.getMailBox();
assert mailBox.get("H.P. Lovecraft").equals(
Arrays.asList(
"This \"The Shadow over Innsmouth\" story is real masterpiece, Howard!"
)
): "wrong mailService mailbox content (1)";
assert mailBox.get("Christopher Nolan").equals(
Arrays.asList(
"Брат, почему все так хвалят только тебя, когда практически все сценарии написал я. Так не честно!",
"Я так и не понял Интерстеллар."
)
): "wrong mailService mailbox content (2)";
assert mailBox.get(randomTo).equals(Collections.<String>emptyList()): "wrong mailService mailbox content (3)";
// Creating list of three salaries
Salary salary1 = new Salary("Facebook", "Mark Zuckerberg", 1);
Salary salary2 = new Salary("FC Barcelona", "Lionel Messi", Integer.MAX_VALUE);
Salary salary3 = new Salary(randomFrom, randomTo, randomSalary);
// Creating service managing salaries
MailService<Integer> salaryService = new MailService<>();
// Processing salaries
Arrays.asList(salary1, salary2, salary3).forEach(salaryService);
// Getting and checking "mailbox" dictionary
// recipient <-> list of salaries he got
Map<String, List<Integer>> salaries = salaryService.getMailBox();
assert salaries.get(salary1.getTo()).equals(Arrays.asList(1)): "wrong salaries mailbox content (1)";
assert salaries.get(salary2.getTo()).equals(Arrays.asList(Integer.MAX_VALUE)): "wrong salaries mailbox content (2)";
assert salaries.get(randomTo).equals(Arrays.asList(randomSalary)): "wrong salaries mailbox content (3)";
}
}