From 27955b2e978e44535c44887d10a47bac003da967 Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Wed, 16 Oct 2024 16:14:32 -0700 Subject: [PATCH 1/4] added comments --- src/Main.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Main.java b/src/Main.java index e5571ea..642eab8 100644 --- a/src/Main.java +++ b/src/Main.java @@ -8,9 +8,11 @@ public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); + //takes input from the user System.out.print("Enter the CSV filename: "); String f = s.nextLine(); + //store the info from the text file List> dta = new ArrayList<>(); try (Scanner fs = new Scanner(new File(f))) { fs.nextLine(); @@ -32,6 +34,7 @@ public static void main(String[] args) { return; } + //store info for "id" Map>> mp2 = new HashMap<>(); for (Map d : dta) { String id = d.get("id"); @@ -90,6 +93,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); From ff1cc64a6b6dde990d8171b0c1a431255749e423 Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Wed, 16 Oct 2024 16:32:25 -0700 Subject: [PATCH 2/4] Rename symbol mp1 to dataMap --- src/Main.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Main.java b/src/Main.java index 642eab8..439d7cd 100644 --- a/src/Main.java +++ b/src/Main.java @@ -22,11 +22,11 @@ public static void main(String[] args) { 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); + Map dataMap = new HashMap<>(); + dataMap.put("id", v[0]); + dataMap.put("tm", v[1]); + dataMap.put("chg", String.valueOf(chg)); + dta.add(dataMap); } } catch (FileNotFoundException e) { System.out.println("Error reading the file: " + e.getMessage()); From 59a8e14e94b111eb45de80bc1022d7a8ee2f3f34 Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Wed, 16 Oct 2024 16:33:35 -0700 Subject: [PATCH 3/4] Renamed symbol dta to dataList --- src/Main.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Main.java b/src/Main.java index 439d7cd..51d1fdb 100644 --- a/src/Main.java +++ b/src/Main.java @@ -13,7 +13,7 @@ public static void main(String[] args) { String f = s.nextLine(); //store the info from the text file - List> dta = new ArrayList<>(); + List> dataList = new ArrayList<>(); try (Scanner fs = new Scanner(new File(f))) { fs.nextLine(); @@ -26,7 +26,7 @@ public static void main(String[] args) { dataMap.put("id", v[0]); dataMap.put("tm", v[1]); dataMap.put("chg", String.valueOf(chg)); - dta.add(dataMap); + dataList.add(dataMap); } } catch (FileNotFoundException e) { System.out.println("Error reading the file: " + e.getMessage()); @@ -36,7 +36,7 @@ public static void main(String[] args) { //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) { @@ -53,7 +53,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); From 2c7dd2b5d6907e507c3a37569da81737a67bb6e4 Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Thu, 17 Oct 2024 16:28:10 -0700 Subject: [PATCH 4/4] Fixed bug and changed f to filename --- src/Main.java | 52 +++++++++++++++++++++++++++-------------------- src/MainTest.java | 6 +++++- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/Main.java b/src/Main.java index 51d1fdb..b7245d5 100644 --- a/src/Main.java +++ b/src/Main.java @@ -7,32 +7,14 @@ 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(); - //store the info from the text file - List> dataList = 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 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; - } + //call helper method + List> dataList = parseCSV(f); //store info for "id" Map>> mp2 = new HashMap<>(); @@ -104,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); } }