Total Complexity | 9 |
Total Lines | 35 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | /* |
||
11 | public class Interpreter implements InterpreterInterface { |
||
12 | private final List<AlbumInterface> registry = new ArrayList<>(); |
||
13 | |||
14 | @Override |
||
15 | public String interpret(String input) { |
||
16 | String[] exploded = input.split(" "); |
||
17 | |||
18 | for (String value : exploded) { |
||
19 | if (this.isNumeric(value)) { |
||
20 | int number = Integer.parseInt(value) - 1; |
||
21 | return this.getDataFromRegistry(exploded, this.registry.get(number)); |
||
22 | } |
||
23 | } |
||
24 | |||
25 | return input; |
||
26 | } |
||
27 | |||
28 | public void addAlbumToRegistry(AlbumInterface album) { |
||
29 | this.registry.add(album); |
||
30 | } |
||
31 | |||
32 | private boolean isNumeric(String str) { |
||
33 | return str != null && str.matches("[-+]?\\d*\\.?\\d+"); |
||
34 | } |
||
35 | |||
36 | private String getDataFromRegistry(String[] exploded, AlbumInterface item) |
||
37 | { |
||
38 | StringBuilder output = new StringBuilder(); |
||
39 | |||
40 | for (String value : exploded) { |
||
41 | if (value.equals("album")) output.append(item.getName()).append(" "); |
||
42 | if (value.equals("author")) output.append(item.getAuthor()).append(" "); |
||
43 | } |
||
44 | |||
45 | return output.toString(); |
||
46 | } |
||
48 |