diff --git a/src/Main.java b/src/Main.java index e5571ea..b7245d5 100644 --- a/src/Main.java +++ b/src/Main.java @@ -7,33 +7,18 @@ public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); + parseCSV("commit_data.csv"); + //takes input from the user System.out.print("Enter the CSV filename: "); String f = s.nextLine(); - List> dta = new ArrayList<>(); - try (Scanner fs = new Scanner(new File(f))) { - fs.nextLine(); - - while (fs.hasNextLine()) { - String[] v = fs.nextLine().split(","); - - int chg = Integer.parseInt(v[2]); - - Map mp1 = new HashMap<>(); - mp1.put("id", v[0]); - mp1.put("tm", v[1]); - mp1.put("chg", String.valueOf(chg)); - dta.add(mp1); - } - } catch (FileNotFoundException e) { - System.out.println("Error reading the file: " + e.getMessage()); - s.close(); - return; - } + //call helper method + List> dataList = parseCSV(f); + //store info for "id" Map>> mp2 = new HashMap<>(); - for (Map d : dta) { + for (Map d : dataList) { String id = d.get("id"); List> lst = mp2.get(id); if (lst == null) { @@ -50,7 +35,7 @@ public static void main(String[] args) { List> sel; if (inp.equalsIgnoreCase("all")) { - sel = dta; + sel = dataList; } else { String id = "fork" + inp; sel = mp2.get(id); @@ -90,6 +75,7 @@ public static void main(String[] args) { } } + //print statistics System.out.println("\nStatistics:"); System.out.println("Number of commits: " + sz); System.out.println("Most recent commit timestamp: " + latT); @@ -100,4 +86,30 @@ public static void main(String[] args) { s.close(); } -} + + public static List> parseCSV(String filename) { + //store the info from the text file + List> dataList = new ArrayList<>(); + try (Scanner fs = new Scanner(new File(filename))) { + fs.nextLine(); + + while (fs.hasNextLine()) { + String[] v = fs.nextLine().split(","); + + int chg = Integer.parseInt(v[2]); + + Map dataMap = new HashMap<>(); + dataMap.put("id", v[0]); + dataMap.put("tm", v[1]); + dataMap.put("chg", String.valueOf(chg)); + dataList.add(dataMap); + } + } catch (FileNotFoundException e) { + System.out.println("Error reading the file: " + e.getMessage()); + // s.close(); + // return; + } + return dataList; + } + +} \ No newline at end of file diff --git a/src/MainTest.java b/src/MainTest.java index 1141d9e..3e0fb52 100644 --- a/src/MainTest.java +++ b/src/MainTest.java @@ -1,9 +1,10 @@ -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.*; import org.junit.jupiter.api.Test; @@ -13,6 +14,8 @@ void testParseCSV() { // You will finish implementing this method in Wave 3 // TODO: Call Main.parseCSV("data/small_commit_data.csv") here and set it to an actual variable + // Call Main.parseCSV + List> actualCommits = Main.parseCSV("data/small_commit_data.csv"); // Sets up the expected value for you. You do not need to edit this part List> expectedCommits = new ArrayList<>(); @@ -37,5 +40,6 @@ void testParseCSV() { // Finish expected value setup // TODO: Assert that the expected equals the actual + assertEquals(expectedCommits, actualCommits); } }