Finish phase 2

This commit is contained in:
Logen Kain 2021-04-08 16:13:03 -04:00
parent 46c0a78447
commit 5a080e088c
5 changed files with 89 additions and 13 deletions

View File

@ -1,6 +1,8 @@
import java.util.ArrayList;
import java.util.Scanner;
import java.io.FileInputStream;
import java.util.Collections;
public class CovidDatabase {
@ -12,6 +14,21 @@ public class CovidDatabase {
list = new ArrayList<CovidEntry>();
}
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 = null;
ArrayList<CovidEntry> temp = new ArrayList<CovidEntry>();
@ -20,17 +37,22 @@ public class CovidDatabase {
for (CovidEntry c: list){
if (c.getState().equalsIgnoreCase(st)) {
if (previousDailyInfections != -1 && c.getDailyInfections() < previousDailyInfections) {
if (previousDailyInfections == -1) {
previousDailyInfections = c.getDailyInfections();
match += 1;
}
else if (c.getDailyInfections() < previousDailyInfections) {
match += 1;
temp.add(c);
} else {
match = 0;
match = 1;
temp.clear();
}
temp.add(c);
previousDailyInfections = c.getDailyInfections();
}
if (match == 5) {
if (match == SAFE) {
result = temp;
break;
}
@ -46,7 +68,7 @@ public class CovidDatabase {
ArrayList<CovidEntry> result = new ArrayList<CovidEntry>();
for (CovidEntry c: list){
if (c.getMonth() == m && c.getDay() == d && c.getDailyInfections() <= min){
if (c.getMonth() == m && c.getDay() == d && c.getDailyInfections() >= min){
result.add(c);
}
}
@ -136,11 +158,13 @@ public class CovidDatabase {
int max = 0;
for(CovidEntry c : list){
if( c.getDailyDeaths() > max){
if (c.getMonth() == m && c.getDay() == d) {
if (c.getDailyDeaths() > max) {
max = c.getDailyDeaths();
result = c;
}
}
}
return result;
}
public CovidEntry mostTotalDeaths(){

View File

@ -0,0 +1,41 @@
public class CovidDatabaseTest {
public static void main (String[] args) {
System.out.println ("Testing starts");
CovidDatabase db = new CovidDatabase() ;
db.readCovidData("covid_data.csv");
// check number of records, total infections, and total deaths
assert db.countRecords() == 10346 : "database should have 10,346";
assert db.getTotalDeaths() == 196696 : "Total deaths should be: 196,696";
assert db.getTotalInfections() == 7032090 : "infections should be: 7,032,090";
// check peak daily deaths for 5/5
CovidEntry mostDeaths = db.peakDailyDeaths(5, 5);
assert mostDeaths.getState().equals("PA") : "State with most deaths for 5/5 is PA";
assert mostDeaths.getDailyDeaths() == 554 : "Deaths for 5/5 is PA: 554";
// test other methods
// Check highest deaths
CovidEntry highestTX = db.peakDailyDeaths("TX");
assert highestTX.getDailyDeaths() == 675 : "Highest texas deaths is: 675";
// Most total deaths
assert db.mostTotalDeaths().getTotalDeaths() == 25456 : "Highest deaths is 25456";
assert db.mostTotalDeaths().getState().equals("NY") : "Highest deaths state: NY";
// Peak daily deaths
assert db.peakDailyDeaths("MI").getDailyDeaths() == 169 : "Highest MI Deaths: 169";
assert db.peakDailyDeaths(5, 5).getDailyDeaths() == 554 : "Highest 5/5 deaths: PA";
// Top ten deaths
assert db.topTenDeaths(5, 5).size() == 10 : "Top ten deaths should be 10";
assert db.topTenDeaths(5, 5).get(0).getState().equals("PA") : "First Top ten deaths should be PA";
// Safe to open
assert db.safeToOpen("MI").get(0).getDailyInfections() == 443 : "First entry should have 443 infections";
//list min daily infections
assert db.listMinimumDailyInfections(6,12,1000).get(0).getState().equals("TX") : "First entry should be TX";
System.out.println ("Testing ends");
}
}

View File

@ -1,6 +1,6 @@
import java.text.DecimalFormat;
public class CovidEntry {
public class CovidEntry implements Comparable {
//Private vars
private String state;
@ -43,4 +43,9 @@ public class CovidEntry {
df.format(dailyDeaths) + " deaths";
}
public int compareTo(Object other){
CovidEntry c = (CovidEntry) other;
return c.dailyDeaths - dailyDeaths;
}
}

View File

@ -1,7 +1,7 @@
import java.util.ArrayList;
//time 11:10 - 11:22
//time 12:00
//time 12:00 - 16:10
//time1 - 10:10 10:30
public class CovidManager {
@ -42,9 +42,15 @@ public class CovidManager {
System.out.println(c);
}
System.out.println("MI safe to open: ");
ArrayList<CovidEntry> MISafe = db.safeToOpen("MI");
for (CovidEntry c: MISafe){
System.out.println("WA safe to open: ");
ArrayList<CovidEntry> WASafe = db.safeToOpen("WA");
for (CovidEntry c: WASafe){
System.out.println(c);
}
System.out.println("Top 10 deaths for May 1st");
ArrayList<CovidEntry> topTenDeaths = db.topTenDeaths(5, 1);
for (CovidEntry c: topTenDeaths){
System.out.println(c);
}