-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path$8_Classes.java
More file actions
110 lines (96 loc) · 3.07 KB
/
$8_Classes.java
File metadata and controls
110 lines (96 loc) · 3.07 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
package src;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class $8_Classes {
public static void main(String[] args) {
String inputCsvFilePath = "src/$8_Classes.csv";
String currentLine;
String commaDelimiter = ",";
List<HospitalRecord> hospitalRecords = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(inputCsvFilePath))) {
while ((currentLine = reader.readLine()) != null) {
if (currentLine.startsWith("Date,Department,Visitors")) {
continue;
}
String[] columns = currentLine.split(commaDelimiter);
String firstColumn = columns[0];
String secondColumn = columns[1];
int thirdColumn;
try {
thirdColumn = Integer.parseInt(columns[2]);
} catch (NumberFormatException exception) {
System.err.println("Invalid value for visitors: " + columns[2]);
continue;
}
HospitalRecord hospitalRecord = new HospitalRecord(firstColumn, secondColumn, thirdColumn);
hospitalRecords.add(hospitalRecord);
}
} catch (IOException exceptionObject) {
exceptionObject.printStackTrace();
}
Map<String, Map<String, Integer>> monthlyReportMap = new HashMap<>();
for (HospitalRecord hospitalRecord : hospitalRecords) {
String recordDate = hospitalRecord.getDate();
String[] dateParts = recordDate.split("-");
String month = dateParts[1];
String department = hospitalRecord.getDepartment();
int visitors = hospitalRecord.getVisitors();
if (!monthlyReportMap.containsKey(month)) {
monthlyReportMap.put(month, new HashMap<>());
}
Map<String, Integer> departmentReportMap = monthlyReportMap.get(month);
if (!departmentReportMap.containsKey(department)) {
departmentReportMap.put(department, visitors);
} else {
int totalVisitors = departmentReportMap.get(department) + visitors;
departmentReportMap.put(department, totalVisitors);
}
}
try (FileWriter fileWriter = new FileWriter("src/$8_Classes_monthly_report.csv")) {
fileWriter.write("Month,Department,Visitors\n");
for (String month : monthlyReportMap.keySet()) {
Map<String, Integer> departmentReportMap = monthlyReportMap.get(month);
for (String department : departmentReportMap.keySet()) {
int visitor = departmentReportMap.get(department);
fileWriter.write(month + "," + department + "," + visitor + "\n");
}
}
} catch (IOException exception) {
exception.printStackTrace();
}
}
}
class HospitalRecord {
private String date;
private String department;
private int visitors;
public HospitalRecord(String date, String department, int visitors) {
this.date = date;
this.department = department;
this.visitors = visitors;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public int getVisitors() {
return visitors;
}
public void setVisitors(int visitors) {
this.visitors = visitors;
}
}