From 5a080e088ca86fbd2fcf019df86de35ee5b09500 Mon Sep 17 00:00:00 2001 From: Logen Kain Date: Thu, 8 Apr 2021 16:13:03 -0400 Subject: [PATCH] Finish phase 2 --- java/final-project/CovidDatabase.java | 40 ++++++++++++++---- .../{tests => }/CovidDatabaseJUnit.java | 0 java/final-project/CovidDatabaseTest.java | 41 +++++++++++++++++++ java/final-project/CovidEntry.java | 7 +++- java/final-project/CovidManager.java | 14 +++++-- 5 files changed, 89 insertions(+), 13 deletions(-) rename java/final-project/{tests => }/CovidDatabaseJUnit.java (100%) create mode 100644 java/final-project/CovidDatabaseTest.java diff --git a/java/final-project/CovidDatabase.java b/java/final-project/CovidDatabase.java index 8cf43c1..5bc919a 100644 --- a/java/final-project/CovidDatabase.java +++ b/java/final-project/CovidDatabase.java @@ -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(); } + public ArrayList topTenDeaths(int m, int d){ + ArrayList result = new ArrayList(); + ArrayList 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 safeToOpen(String st){ ArrayList result = null; ArrayList temp = new ArrayList(); @@ -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 result = new ArrayList(); 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,9 +158,11 @@ public class CovidDatabase { int max = 0; for(CovidEntry c : list){ - if( c.getDailyDeaths() > max){ - max = c.getDailyDeaths(); - result = c; + if (c.getMonth() == m && c.getDay() == d) { + if (c.getDailyDeaths() > max) { + max = c.getDailyDeaths(); + result = c; + } } } return result; diff --git a/java/final-project/tests/CovidDatabaseJUnit.java b/java/final-project/CovidDatabaseJUnit.java similarity index 100% rename from java/final-project/tests/CovidDatabaseJUnit.java rename to java/final-project/CovidDatabaseJUnit.java diff --git a/java/final-project/CovidDatabaseTest.java b/java/final-project/CovidDatabaseTest.java new file mode 100644 index 0000000..0d646ff --- /dev/null +++ b/java/final-project/CovidDatabaseTest.java @@ -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"); + } +} diff --git a/java/final-project/CovidEntry.java b/java/final-project/CovidEntry.java index cabc991..b7ddc8a 100644 --- a/java/final-project/CovidEntry.java +++ b/java/final-project/CovidEntry.java @@ -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; + } + } diff --git a/java/final-project/CovidManager.java b/java/final-project/CovidManager.java index fa9b1f2..d644750 100644 --- a/java/final-project/CovidManager.java +++ b/java/final-project/CovidManager.java @@ -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 MISafe = db.safeToOpen("MI"); - for (CovidEntry c: MISafe){ + System.out.println("WA safe to open: "); + ArrayList WASafe = db.safeToOpen("WA"); + for (CovidEntry c: WASafe){ + System.out.println(c); + } + + System.out.println("Top 10 deaths for May 1st"); + ArrayList topTenDeaths = db.topTenDeaths(5, 1); + for (CovidEntry c: topTenDeaths){ System.out.println(c); }