-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatus.java
More file actions
142 lines (124 loc) · 4.51 KB
/
Copy pathStatus.java
File metadata and controls
142 lines (124 loc) · 4.51 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
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
import java.time.LocalDateTime;
import java.lang.Math.*;
/**
* Status
*/
public class Status {
private String standort;
private Double temperatur;
private Double luftfeuchte;
private LocalDateTime timestamp;
//initializes with given Standort and generates pseudorandom measurements
public Status(String standort) {
this.temperatur = Math.random() * 30 - 15;
this.standort = standort;
this.luftfeuchte = Math.random() * 100;
this.timestamp = LocalDateTime.now();
}
public Status(String standort, Double temperatur, Double luftfeuchte, LocalDateTime timestamp){
this.temperatur = temperatur;
this.standort = standort;
this.luftfeuchte = luftfeuchte;
this.timestamp = timestamp;
}
//generates new pseudorandom measurements and updates timestamp
public void wertGenerator() {
temperatur = (Math.random() * 2 -1) + temperatur;
this.luftfeuchte = (Math.random() * 4 -2) + luftfeuchte;
timestamp = LocalDateTime.now();
}
public Double getLuftfeuchte() {
return luftfeuchte;
}
public String getStandort() {
return standort;
}
public Double getTemperatur() {
return temperatur;
}
public LocalDateTime getTimestamp() {
return timestamp;
}
public void setStandort(String standort) {
this.standort = standort;
}
public void setLuftfeuchte(Double luftfeuchte) {
this.luftfeuchte = luftfeuchte;
}
public void setTemperatur(Double temperatur) {
this.temperatur = temperatur;
}
public void setTimestamp(LocalDateTime timestamp) {
this.timestamp = timestamp;
}
@Override
//generates readable String representation of Status Object
public String toString() {
String out = "Standort: " + standort + "\n";
out += "Temperatur: " + temperatur + "\n";
out += "Luftfeuchte: " + luftfeuchte + "\n";
out += "Zeit: " + timestamp + "\n";
return out;
}
//generates structured String representation of Status object, key value pairs are associated by -> and delimited by ,
public String toStructuredString() {
String out = "Standort->" + standort;
out += ",Temperatur->" + temperatur;
out += ",Luftfeuchte->" + luftfeuchte;
out += ",Zeit->" + timestamp;
return out;
}
//parses structured String representation of Status to Status object
public static Status parse(String structuredIn) throws IncorrectStatusStringRepresentation{
String[] kVPairs = structuredIn.split(",");
if(kVPairs.length != 4) {
throw new IncorrectStatusStringRepresentation(structuredIn + "Wrong amount of key Value pairs");
}
String[] seqkVPairs = new String[kVPairs.length * 2];
for (int i = 0; i < kVPairs.length; i++) {
String[] temp = kVPairs[i].split("->");
if (temp.length != 2) {
throw new IncorrectStatusStringRepresentation(structuredIn + " Wrong amount of -> in " + kVPairs[i]);
}
seqkVPairs[2*i] = temp[0];
seqkVPairs[2*i +1] = temp[1];
}
int attrFound = 0;
String Standort = "";
Double Temperatur = -10000.0;
Double Luftfeuchte = -10000.0;
LocalDateTime Zeit = LocalDateTime.now();
//parse Standort
for (int i = 0; i < seqkVPairs.length; i++) {
if (seqkVPairs[i].equals("Standort")) {
Standort = seqkVPairs[i+1];
attrFound++;
}
}
//parse Temperatur
for (int i = 0; i < seqkVPairs.length; i++) {
if (seqkVPairs[i].equals("Temperatur")) {
Temperatur = Double.parseDouble(seqkVPairs[i+1]);
attrFound++;
}
}
//parse Luftfeuchte
for (int i = 0; i < seqkVPairs.length; i++) {
if (seqkVPairs[i].equals("Luftfeuchte")) {
Luftfeuchte = Double.parseDouble(seqkVPairs[i+1]);
attrFound++;
}
}
//parse Zeit
for (int i = 0; i < seqkVPairs.length; i++) {
if (seqkVPairs[i].equals("Zeit")) {
Zeit = LocalDateTime.parse(seqkVPairs[i+1]);
attrFound++;
}
}
if (attrFound != 4) {
throw new IncorrectStatusStringRepresentation(structuredIn + " found " + attrFound + " Attributes");
}
return new Status(Standort, Temperatur, Luftfeuchte, Zeit);
}
}