-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHash.java
More file actions
102 lines (85 loc) · 3.46 KB
/
Hash.java
File metadata and controls
102 lines (85 loc) · 3.46 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.util.Map;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
public class Hash {
public static void readFile() {
File data = new File("covid417.csv");
Map<String, Map<String, City>> states = new HashMap<>();
try {
Scanner scan = new Scanner(data);
scan.nextLine(); // Skip header line
while (scan.hasNextLine()) {
String[] row = scan.nextLine().split(",");
if (row.length < 6) {
System.err.println("Skipping invalid row: " + String.join(",", row));
continue;
}
String state = row[1];
String city = row[0];
City value = new City(
row[0], row[1],
Integer.parseInt(row[2]),
Integer.parseInt(row[3]),
Integer.parseInt(row[4]),
Integer.parseInt(row[5])
);
states.putIfAbsent(state, new HashMap<>());
states.get(state).put(city, value);
}
scan.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
}
// User Input Loop
Scanner input = new Scanner(System.in);
while (true) {
System.out.print("Input a state (or type 'exit' to quit): ");
String stateIn = input.nextLine().trim();
if (stateIn.equalsIgnoreCase("exit")) {
break;
}
if (!states.containsKey(stateIn)) {
System.out.println("Error: state not found");
continue;
}
Map<String, City> cities = states.get(stateIn);
// Compute and display total metrics for the state
City totals = getStateTotals(cities);
System.out.println("\nState Totals for " + stateIn + ":");
System.out.println(" Total Cases: " + totals.getCases());
System.out.println(" Total Deaths: " + totals.getDeaths());
System.out.println(" Total Recovered: " + totals.getRecovered());
System.out.println(" Total Hospitalized: " + totals.getHospitalized());
// Prompt for city
System.out.print("\nInput a city: ");
String cityIn = input.nextLine().trim();
if (cities.containsKey(cityIn)) {
City cityData = cities.get(cityIn);
System.out.println("City Data: " + cityData);
} else {
System.out.println("Error: city not found");
}
}
input.close();
}
// Compute total metrics for a state
public static City getStateTotals(Map<String, City> cities) {
int totalCases = 0;
int totalDeaths = 0;
int totalRecovered = 0;
int totalHospitalized = 0;
for (City city : cities.values()) {
totalCases += city.getCases();
totalDeaths += city.getDeaths();
totalRecovered += city.getRecovered();
totalHospitalized += city.getHospitalized();
}
return new City("ALL_CITIES", "STATE_TOTAL", totalCases, totalDeaths, totalRecovered, totalHospitalized);
}
public static void main(String[] args) {
readFile();
}
}