school/java/final-project/CovidDatabase.java

213 lines
4.3 KiB
Java

import java.util.ArrayList;
import java.util.Scanner;
import java.io.FileInputStream;
import java.util.Collections;
public class CovidDatabase {
private ArrayList<CovidEntry> list;
private static final int SAFE = 5;
public CovidDatabase() {
list = new ArrayList<CovidEntry>();
}
private void transferCovidData(String filename){
}
public ArrayList <CovidEntry> topTenDeaths(int m, int d){
ArrayList<CovidEntry> result = new ArrayList<CovidEntry>();
ArrayList<CovidEntry> temp = getDailyDeaths(m, d);
Collections.sort(temp);
if (temp.size() ==0){
return result;
}
for (int i = 0; i < 10; i++){
result.add(temp.get(i));
}
return result;
}
public ArrayList <CovidEntry> safeToOpen(String st){
ArrayList<CovidEntry> result = new ArrayList<CovidEntry>();
int match = 0;
int previousDailyInfections = -1;
for (CovidEntry c: list){
if (c.getState().equalsIgnoreCase(st)) {
if (previousDailyInfections == -1) {
previousDailyInfections = c.getDailyInfections();
match += 1;
}
else if (c.getDailyInfections() < previousDailyInfections) {
match += 1;
} else {
match = 1;
result.clear();
}
result.add(c);
previousDailyInfections = c.getDailyInfections();
}
if (match == SAFE) {
return result;
}
}
return null;
}
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.getMonth() == m && c.getDay() == d) {
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);
}
}
}