school/java/final-project/CovidDatabase.java

190 lines
3.8 KiB
Java

import java.util.ArrayList;
import java.util.Scanner;
import java.io.FileInputStream;
public class CovidDatabase {
private ArrayList<CovidEntry> list;
private static final int SAFE = 5;
public CovidDatabase(){
list = new ArrayList<CovidEntry>();
}
public ArrayList <CovidEntry> safeToOpen(String st){
ArrayList<CovidEntry> result = null;
ArrayList<CovidEntry> temp = new ArrayList<CovidEntry>();
int match = 0;
int previousDailyInfections = -1;
for (CovidEntry c: list){
if (c.getState().equalsIgnoreCase(st)) {
if (previousDailyInfections != -1 && c.getDailyInfections() < previousDailyInfections) {
match += 1;
temp.add(c);
} else {
match = 0;
temp.clear();
}
previousDailyInfections = c.getDailyInfections();
}
if (match == 5) {
result = temp;
break;
}
}
return result;
}
public ArrayList <CovidEntry>
listMinimumDailyInfections(int m, int d, int min){
ArrayList<CovidEntry> result = new ArrayList<CovidEntry>();
for (CovidEntry c: list){
if (c.getMonth() == m && c.getDay() == d && c.getDailyInfections() <= min){
result.add(c);
}
}
return result;
}
public int countRecords(){
return list.size();
}
public int getTotalDeaths(){
int result = 0;
for (CovidEntry c: list){
result += c.getDailyDeaths();
}
return result;
}
public int getTotalInfections(){
int result = 0;
for (CovidEntry c: list){
result += c.getDailyInfections();
}
return result;
}
public int countTotalInfections(int m, int d){
int result = 0;
for (CovidEntry c: list){
if (c.getMonth() == m && c.getDay() == d){
result += c.getDailyInfections();
}
}
return result;
}
public int countTotalDeaths(int m, int d){
int result = 0;
for (CovidEntry c: list){
if (c.getMonth() == m && c.getDay() == d){
result += c.getDailyDeaths();
}
}
return result;
}
public ArrayList <CovidEntry> getDailyDeaths(int m, int d){
ArrayList<CovidEntry> result = new ArrayList<CovidEntry>();
for (CovidEntry c: list){
if (c.getMonth() == m && c.getDay() == d){
result.add(c);
}
}
return result;
}
//State
public CovidEntry peakDailyDeaths(String st) {
//iterate over our database
//find highest deathcount that matches our state (st)
CovidEntry result = null;
int max = 0;
for(CovidEntry c : list){
if( c.getState().equalsIgnoreCase(st)){
if( c.getDailyDeaths() > max){
max = c.getDailyDeaths();
result = c;
}
}
}
return result;
}
//Month day
public CovidEntry peakDailyDeaths(int m, int d) {
//iterate over our database
//find highest deathcount that matches our state (st)
CovidEntry result = null;
int max = 0;
for(CovidEntry c : list){
if( c.getDailyDeaths() > max){
max = c.getDailyDeaths();
result = c;
}
}
return result;
}
public CovidEntry mostTotalDeaths(){
CovidEntry result = null;
int max = 0;
for(CovidEntry c : list){
if( c.getTotalDeaths() > max){
max = c.getTotalDeaths();
result = c;
}
}
return result;
}
public void readCovidData(String filename){
Scanner scnr = null;
FileInputStream fs = null;
try{
fs = new FileInputStream(filename);
scnr = new Scanner(fs);
scnr.useDelimiter("[,\r\n]+");
scnr.nextLine();
while(scnr.hasNext()){
String state = scnr.next();
int month = scnr.nextInt();
int day = scnr.nextInt();
int di = scnr.nextInt();
int dd = scnr.nextInt();
int ti = scnr.nextInt();
int td = scnr.nextInt();
CovidEntry c = new CovidEntry(state,
month, day, di, dd, ti, td);
list.add(c);
}
fs.close();
}
catch(Exception e){
System.out.println("File error with " + filename);
}
}
}