From ce88b95551c0f7fc2787541e849bf968a9fde25e Mon Sep 17 00:00:00 2001 From: HumaGitGud Date: Tue, 15 Oct 2024 14:39:23 -0700 Subject: [PATCH 1/2] Renamed the data structure maps to provide more clarity --- src/Main.java | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/Main.java b/src/Main.java index e5571ea..05ce190 100644 --- a/src/Main.java +++ b/src/Main.java @@ -11,7 +11,10 @@ public static void main(String[] args) { System.out.print("Enter the CSV filename: "); String f = s.nextLine(); - List> dta = new ArrayList<>(); + // Initializes an Arraylist containing a map with string key/value, and asks for a file name + // Then, it creates an array that splits the data by commas and puts them in the mp1 + // method throws and exception error if file cannot be found + List> outerMap = new ArrayList<>(); try (Scanner fs = new Scanner(new File(f))) { fs.nextLine(); @@ -20,11 +23,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)); + outerMap.add(dataMap); } } catch (FileNotFoundException e) { System.out.println("Error reading the file: " + e.getMessage()); @@ -32,8 +35,11 @@ public static void main(String[] args) { return; } + // Creates a map name mp2 with string key and list containing map + // loop goes through the map and gets the value from key "id" + // Map>> mp2 = new HashMap<>(); - for (Map d : dta) { + for (Map d : outerMap) { String id = d.get("id"); List> lst = mp2.get(id); if (lst == null) { @@ -42,6 +48,7 @@ public static void main(String[] args) { } lst.add(d); } + System.out.println(mp2); int cnt = mp2.size(); System.out.println("There are " + cnt + " forks available (fork1 to fork" + cnt + ")."); @@ -50,7 +57,7 @@ public static void main(String[] args) { List> sel; if (inp.equalsIgnoreCase("all")) { - sel = dta; + sel = outerMap; } else { String id = "fork" + inp; sel = mp2.get(id); From 091a959877f936982a54f9b3d44914f84ce3b1eb Mon Sep 17 00:00:00 2001 From: HumaGitGud Date: Tue, 15 Oct 2024 15:09:05 -0700 Subject: [PATCH 2/2] Refactored some code, added clarity --- src/Main.java | 85 ++++++++++++++++++++++++++++----------------------- 1 file changed, 46 insertions(+), 39 deletions(-) diff --git a/src/Main.java b/src/Main.java index 05ce190..2e5ca75 100644 --- a/src/Main.java +++ b/src/Main.java @@ -6,63 +6,41 @@ public class Main { public static void main(String[] args) { - Scanner s = new Scanner(System.in); - + Scanner scanner = new Scanner(System.in); System.out.print("Enter the CSV filename: "); - String f = s.nextLine(); - - // Initializes an Arraylist containing a map with string key/value, and asks for a file name - // Then, it creates an array that splits the data by commas and puts them in the mp1 - // method throws and exception error if file cannot be found - List> outerMap = 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)); - outerMap.add(dataMap); - } - } catch (FileNotFoundException e) { - System.out.println("Error reading the file: " + e.getMessage()); - s.close(); - return; - } + String fileName = scanner.nextLine(); + List> outerMap = parseCSV(fileName); + //System.out.println("Outer map: " + outerMap); // Creates a map name mp2 with string key and list containing map // loop goes through the map and gets the value from key "id" // - Map>> mp2 = new HashMap<>(); - for (Map d : outerMap) { - String id = d.get("id"); - List> lst = mp2.get(id); + Map>> forkIdData = new HashMap<>(); + for (Map item : outerMap) { + String id = item.get("id"); + List> lst = forkIdData.get(id); if (lst == null) { lst = new ArrayList<>(); - mp2.put(id, lst); + forkIdData.put(id, lst); } - lst.add(d); + lst.add(item); } - System.out.println(mp2); - int cnt = mp2.size(); + //System.out.println("Mp2: " + mp2); + int cnt = forkIdData.size(); System.out.println("There are " + cnt + " forks available (fork1 to fork" + cnt + ")."); System.out.print("Enter the fork number to analyze (or 'all' for all forks): "); - String inp = s.nextLine(); + String inp = scanner.nextLine(); List> sel; if (inp.equalsIgnoreCase("all")) { sel = outerMap; } else { String id = "fork" + inp; - sel = mp2.get(id); + sel = forkIdData.get(id); } + int sz = sel.size(); DateTimeFormatter f1 = DateTimeFormatter.ISO_DATE_TIME; @@ -105,6 +83,35 @@ public static void main(String[] args) { System.out.println("Max lines changed in a commit: " + mx); System.out.println("Min lines changed in a commit: " + mn); - s.close(); + scanner.close(); + } + + public static List> parseCSV(String filename) { + // Initializes an Arraylist containing a map with string key/value, and asks for a file name + // Then, it creates an array that splits the data by commas and puts them in the mp1 + // method throws and exception error if file cannot be found + List> outerMap = new ArrayList<>(); + try (Scanner fileIn = new Scanner(new File(filename))) { + fileIn.nextLine(); + + while (fileIn.hasNextLine()) { + String[] v = fileIn.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)); + outerMap.add(dataMap); + + //System.out.println("Data map: " + dataMap); + } + } catch (FileNotFoundException e) { + System.out.println("Error reading the file: " + e.getMessage()); + return null; + } + + return outerMap; } -} +} \ No newline at end of file