-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileManager.java
More file actions
102 lines (91 loc) · 4.45 KB
/
FileManager.java
File metadata and controls
102 lines (91 loc) · 4.45 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
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
public class FileManager {
public static final String DATA_FILE = "data.txt"; // raw CSV entries
public static final String HISTORY_FILE = "history.txt"; // formatted recommendations
public static final String ALERT_FILE = "alerts.txt"; // alerts
public static final String DAILY_REPORT = "daily_report.txt";
// Append text safely
public static synchronized void writeToFile(String fileName, String content) {
try (FileWriter fw = new FileWriter(fileName, true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw)) {
out.println(content);
} catch (IOException e) {
System.out.println("Error writing to file (" + fileName + "): " + e.getMessage());
}
}
// Overwrite file with empty content (clear)
public static synchronized void clearFile(String fileName) {
try (PrintWriter pw = new PrintWriter(new FileWriter(fileName, false))) {
// overwrite with nothing
} catch (IOException e) {
System.out.println("Error clearing file (" + fileName + "): " + e.getMessage());
}
}
// Read whole file as string (for GUI display)
public static String readFileAsString(String fileName) {
File f = new File(fileName);
if (!f.exists()) return "";
StringBuilder sb = new StringBuilder();
try (BufferedReader br = new BufferedReader(new FileReader(f))) {
String line;
while ((line = br.readLine()) != null) sb.append(line).append("\n");
} catch (IOException e) {
System.out.println("Error reading file (" + fileName + "): " + e.getMessage());
}
return sb.toString();
}
// Compute today's stats by reading DATA_FILE (CSV lines). Then write daily_report.txt
public static synchronized Map<String, Integer> computeTodayStats() {
Map<String, Integer> m = new HashMap<>();
m.put("total", 0);
m.put("needWater", 0);
m.put("tempWarn", 0);
m.put("alerts", 0);
File f = new File(DATA_FILE);
if (!f.exists()) return m;
String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
try (BufferedReader br = new BufferedReader(new FileReader(f))) {
String line;
while ((line = br.readLine()) != null) {
// CSV: timestamp,moisture,pH,temperature,humidity,light,location
String[] p = line.split(",", 7);
if (p.length < 7) continue;
String ts = p[0];
if (!ts.startsWith(today)) continue; // only today's records
m.put("total", m.get("total") + 1);
double moisture = Double.parseDouble(p[1]);
double temp = Double.parseDouble(p[3]);
double ph = Double.parseDouble(p[2]);
if (moisture < 40) m.put("needWater", m.get("needWater") + 1);
if (temp > 35) m.put("tempWarn", m.get("tempWarn") + 1);
// quick alert checks
if (moisture < 20 || temp > 40 || ph < 4.5 || ph > 9.0) {
m.put("alerts", m.get("alerts") + 1);
}
}
} catch (IOException e) {
System.out.println("Error computing stats: " + e.getMessage());
} catch (NumberFormatException e) {
System.out.println("Malformed data entry while computing stats: " + e.getMessage());
}
return m;
}
// Recompute and write daily report from DATA_FILE
public static synchronized void updateDailyReport() {
Map<String, Integer> stats = computeTodayStats();
try (PrintWriter pw = new PrintWriter(new FileWriter(DAILY_REPORT, false))) {
String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
pw.println("Date: " + today);
pw.println("Total Records Today: " + stats.getOrDefault("total", 0));
pw.println("Fields Needing Water (moisture < 40): " + stats.getOrDefault("needWater", 0));
pw.println("Temperature Warnings (temp > 35): " + stats.getOrDefault("tempWarn", 0));
pw.println("Alerts Today: " + stats.getOrDefault("alerts", 0));
pw.println("--------------------------");
} catch (IOException e) {
System.out.println("Error writing daily report: " + e.getMessage());
}
}
}