-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTranslatorImpl.java
More file actions
37 lines (31 loc) · 1.07 KB
/
TranslatorImpl.java
File metadata and controls
37 lines (31 loc) · 1.07 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
package com.yurii.salimov.lesson08.task03;
public final class TranslatorImpl implements Translator {
private String[] eng;
private String[] rus;
public TranslatorImpl(final String dictionary) {
splitDictionaries(dictionary);
}
@Override
public String translate(String line) {
final String[] words = line.split(" ");
final StringBuilder sb = new StringBuilder();
for (String word : words) {
for (int i = 0; i < this.eng.length; i++) {
if (this.eng[i].equals(word)) {
sb.append(this.rus[i]).append(" ");
}
}
}
return sb.toString();
}
private void splitDictionaries(final String dictionary) {
final String[] pairs = dictionary.split(";");
this.eng = new String[pairs.length];
this.rus = new String[pairs.length];
for (int i = 0; i < pairs.length; i++) {
final String[] pair = pairs[i].trim().split("=");
this.eng[i] = pair[0];
this.rus[i] = pair[1];
}
}
}