addAlbumToRegistry(AlbumInterface)   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
1
/*
2
 * @author  : Jagepard <[email protected]>
3
 * @license https://mit-license.org/ MIT
4
 */
5
6
package Behavioral.Interpreter;
7
8
import java.util.ArrayList;
9
import java.util.List;
10
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
    }
47
}
48