import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
//Scanner scan = new Scanner(System.in); // creating a scanner to take in the file name
COVIDDataProcessing dp = new COVIDDataProcessing();// creating an instance of the class where you will write the bulk of your code
ArrayList<County> counties = new COVIDDataProcessing().read();
//System.out.println("Please enter the name of your file (without the .csv or .txt extension)");// prompting the user to enter the file name
// calls the Read method and saves the returned ArrayList in the ArrayList
// 'Counties'
//ArrayList Counties = dp.Read(scan.next());
// Find the county with the most COVID cases PER 100,000 and print out the
// information about it
// Calls the method to display the maximum cases
System.out.println("\n\n Maximum cases per 100000 country information: " +
new COVIDDataProcessing().Max(counties));
// Calls the method to display the minimum cases
System.out.println("\n\n Minimum cases per 100000 country information: " +
dp.Min(counties));
// Calls the method to display total case in state as a whole
System.out.println("\n\n Total cases per 100000 in state as a whole: " +
dp.StateCases(counties));
}
}
import java.util.*;
import java.util.Scanner;
import java.io.*;
public class COVIDDataProcessing {
// Method to read file contents and stores it in array list
// and returns the array list
public ArrayList<County> read() {
ArrayList<County> counties = new ArrayList<County>();
Scanner fileRead = null;
try {
fileRead = new Scanner(new File("COVIDbyCounty.txt"));
while(fileRead.hasNextLine()) {
String record = fileRead.nextLine();
String each[] = record.split(", ");
counties.add(new County(each[0], Integer.parseInt(each[1]), Integer.parseInt(each[2])));
}
fileRead.close();
}catch (FileNotFoundException e) {
System.err.println("\n Unable to open the file: COVIDbyCounty.txt for reading.");
}
return counties;
}
// Method to return the country information which has maximum cases per 100000
public County Max(ArrayList<County> county)
{
// Stores the first county name as maximum cases
County maxCases = county.get(0);
// Calculates first county cases as maximum cases
double maxCase = ((double)county.get(0).getTotalCOVIDCases() /
county.get(0).getCountyPopulation()) * 100000;
// Loops till end of the list starting from a position
// because 0th position is considered above as maximum cases
for(int c = 1; c < county.size(); c++)
{
// Calculates current country cases
double currentCase = ((double)county.get(c).getTotalCOVIDCases() /
county.get(c).getCountyPopulation()) * 100000;
// Checks if current country cases is greater than earlier maximum cases
if(currentCase > maxCase)
{
// Assigns the current country data as maximum country object
maxCases = county.get(c);
// Assigns the current country case as maximum cases
maxCase = currentCase;
}
}
// Returns the country object having maximum cases
return maxCases;
}
// Method to return the country information which has minimum cases per 100000
public County Min(ArrayList<County> county)
{
// Stores the first country name as minimum cases
County minCases = county.get(0);
// Calculates first country cases as minimum cases
double minCase = ((double)county.get(0).getTotalCOVIDCases() /
county.get(0).getCountyPopulation()) * 100000;
// Loops till end of the list starting from 1 position
// because 0th position is considered above as minimum cases
for(int c = 1; c < county.size(); c++)
{
// Calculates current county cases
double currentCase = ((double)county.get(c).getTotalCOVIDCases() /
county.get(c).getCountyPopulation()) * 100000;
// Checks if current county cases is less than earlier minimum cases
if(currentCase < minCase)
{
// Assigns the current county data as minimum country object
minCases = county.get(c);
// Assigns the current county case as minimum cases
minCase = currentCase;
}
}
// Returns the country object having minimum cases
return minCases;
}
// Method to return cases per 100000 of the state as whole
public int StateCases(ArrayList<County> counties)
{
int totalCases = 0;
int totalPopulation = 0;
int cases;
// Loops till end of the list
for(int c = 0; c < counties.size(); c++)
{
// Calculates total case in state
totalCases += counties.get(c).getTotalCOVIDCases();
// Calculates total population in state
totalPopulation += counties.get(c).getCountyPopulation();
}
// Calculates the state cases
cases = (totalCases * 100000) / totalPopulation;
// Returns the state cases
return cases;
}
}
class County {
//variables to store county information.
private String Name;
private int totalCOVIDCases;
private int CountyPopulation;
// Parameter constructor
public County(String name, int totalCOVIDCases, int countyPopulation) {
this.Name = name;
this.totalCOVIDCases = totalCOVIDCases;
this.CountyPopulation = countyPopulation;
}
//Getters and Setters
public String getName() {
return Name;
}
public void setName(String name) {
this.Name = name;
}
public int getTotalCOVIDCases() {
return totalCOVIDCases;
}
public void setTotalCOVIDCases(int totalCOVIDCases) {
this.totalCOVIDCases = totalCOVIDCases;
}
public int getCountyPopulation() {
return CountyPopulation;
}
public void setCountyPopulation(int countyPopulation) {
this.CountyPopulation = countyPopulation;
}
@Override
public String toString() {
return "\n Country Name: " + Name +
"\n Total Active COVID Cases: " + totalCOVIDCases +
"\n Country Population: " + CountyPopulation;
}
}
ddflsafkd, 3, 4
dksalfdsj, 3, 3
fdkslajfkds, 4, 5