-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path38.DesignAHealthMonitorSystem.java
More file actions
104 lines (83 loc) · 3.88 KB
/
38.DesignAHealthMonitorSystem.java
File metadata and controls
104 lines (83 loc) · 3.88 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
/* -------------------------------------------------------- */
/* ( The Authentic JS/JAVA CodeBuff )
___ _ _ _
| _ ) |_ __ _ _ _ __ _ __| |_ __ ____ _ (_)
| _ \ ' \/ _` | '_/ _` / _` \ V V / _` || |
|___/_||_\__,_|_| \__,_\__,_|\_/\_/\__,_|/ |
|__/
*/
/* -------------------------------------------------------------------------- */
/* Youtube: https://youtube.com/@code-with-Bharadwaj */
/* Github : https://github.com/Manu577228 */
/* Portfolio : https://manu-bharadwaj-portfolio.vercel.app/portfolio */
/* ----------------------------------------------------------------------- */
import java.util.*;
public class HealthMonitorSystem {
// --------------------- Strategy Pattern ---------------------
interface AlertStrategy {
List<String> check(Map<String, Integer> vitals, Map<String, Integer> thr);
}
// Concrete Strategy
static class ThresholdAlertStrategy implements AlertStrategy {
public List<String> check(Map<String, Integer> v, Map<String, Integer> t) {
List<String> alerts = new ArrayList<>();
if (v.get("heart") > t.get("heart")) alerts.add("High Heart Rate");
if (v.get("temp") > t.get("temp")) alerts.add("High Temperature");
if (v.get("bp") > t.get("bp")) alerts.add("High Blood Pressure");
return alerts;
}
}
// --------------------- User Profile Model ---------------------
static class UserProfile {
Map<String, Integer> thresholds;
Map<String, Integer> lastVitals;
List<Map<String, Integer>> logs = new ArrayList<>();
UserProfile(Map<String, Integer> thr) {
this.thresholds = thr;
}
}
// --------------------- Singleton Health Monitor ---------------------
static class HealthMonitor {
private static HealthMonitor instance;
private Map<String, UserProfile> users = new HashMap<>();
private AlertStrategy alertStrategy = new ThresholdAlertStrategy();
private HealthMonitor() {}
public static synchronized HealthMonitor getInstance() {
if (instance == null) instance = new HealthMonitor();
return instance;
}
public void addUser(String uid, Map<String, Integer> thr) {
users.put(uid, new UserProfile(thr));
}
public synchronized List<String> updateVitals(String uid, Map<String, Integer> vitals) {
UserProfile user = users.get(uid);
if (user == null) return null;
user.lastVitals = vitals;
user.logs.add(vitals);
return alertStrategy.check(vitals, user.thresholds);
}
public Map<String, Object> getStatus(String uid) {
UserProfile u = users.get(uid);
if (u == null) return null;
Map<String, Object> out = new HashMap<>();
out.put("lastVitals", u.lastVitals);
int size = u.logs.size();
out.put("logs", u.logs.subList(Math.max(0, size - 5), size));
return out;
}
}
// ----------------------------- DEMO ---------------------------------
public static void main(String[] args) {
HealthMonitor monitor = HealthMonitor.getInstance();
Map<String, Integer> thr = new HashMap<>();
thr.put("heart", 100);
thr.put("temp", 99);
thr.put("bp", 140);
monitor.addUser("user1", thr);
Map<String, Integer> v1 = Map.of("heart", 95, "temp", 98, "bp", 135);
Map<String, Integer> v2 = Map.of("heart", 120, "temp", 101, "bp", 150);
System.out.println("Alerts 1: " + monitor.updateVitals("user1", v1));
System.out.println("Alerts 2: " + monitor.updateVitals("user1", v2));
System.out.println("Status: " + monitor.getStatus("user1"));
}
}