From 58da86dcbe85f3c35fba32f4fa50b084614125e3 Mon Sep 17 00:00:00 2001 From: Kimberly Mageary Date: Tue, 15 Oct 2024 14:37:34 -0700 Subject: [PATCH 1/2] Finished Wave 1 --- src/Main.java | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/Main.java b/src/Main.java index e5571ea..1e6ac40 100644 --- a/src/Main.java +++ b/src/Main.java @@ -6,34 +6,45 @@ public class Main { public static void main(String[] args) { + // Creating a scanner object to read in the user input Scanner s = new Scanner(System.in); System.out.print("Enter the CSV filename: "); String f = s.nextLine(); - List> dta = new ArrayList<>(); + // Creates a scanner object that reads a file name + // adds contents from file into a map + List> forkList = new ArrayList<>(); try (Scanner fs = new Scanner(new File(f))) { fs.nextLine(); + // while (fs.hasNextLine()) { + // Read through string and split at each comma, adding + // to a string array 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); + // Adding all three elements to a map with the specific + // key added for each type of variable + Map indiForkMap = new HashMap<>(); + indiForkMap.put("id", v[0]); + indiForkMap.put("tm", v[1]); + indiForkMap.put("chg", String.valueOf(chg)); + forkList.add(indiForkMap); } + // Throws a file not found exception if the file does not exist } catch (FileNotFoundException e) { System.out.println("Error reading the file: " + e.getMessage()); s.close(); return; } + // Filters through the original map and adds a new list to a second map + // along with the id Map>> mp2 = new HashMap<>(); - for (Map d : dta) { + for (Map d : forkList) { String id = d.get("id"); List> lst = mp2.get(id); if (lst == null) { @@ -42,15 +53,20 @@ public static void main(String[] args) { } lst.add(d); } + // checks the size of the second map int cnt = mp2.size(); + // Lets the user know how many forks are available from the file 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(); + // initializes an empty list + // checks to see if the user entered all or a # + // routes them to to the right fork # List> sel; if (inp.equalsIgnoreCase("all")) { - sel = dta; + sel = forkList; } else { String id = "fork" + inp; sel = mp2.get(id); @@ -58,6 +74,7 @@ public static void main(String[] args) { int sz = sel.size(); + // formats the date and time DateTimeFormatter f1 = DateTimeFormatter.ISO_DATE_TIME; LocalDateTime lat = null; for (Map d : sel) { @@ -69,6 +86,7 @@ public static void main(String[] args) { DateTimeFormatter f2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); String latT = lat.format(f2); + // perform some basic math to grab stats double tot = 0.0; int tlc = 0; for (Map d : sel) { @@ -90,6 +108,7 @@ public static void main(String[] args) { } } + // showcases those stats System.out.println("\nStatistics:"); System.out.println("Number of commits: " + sz); System.out.println("Most recent commit timestamp: " + latT); From f766943124f7de3af679a3648e04ecc2c5d69136 Mon Sep 17 00:00:00 2001 From: Kimberly Mageary Date: Tue, 15 Oct 2024 14:45:20 -0700 Subject: [PATCH 2/2] Finished Wave 2 --- src/Main.java | 61 ++++++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/src/Main.java b/src/Main.java index 1e6ac40..8121161 100644 --- a/src/Main.java +++ b/src/Main.java @@ -12,34 +12,7 @@ public static void main(String[] args) { System.out.print("Enter the CSV filename: "); String f = s.nextLine(); - // Creates a scanner object that reads a file name - // adds contents from file into a map - List> forkList = new ArrayList<>(); - try (Scanner fs = new Scanner(new File(f))) { - fs.nextLine(); - - // - while (fs.hasNextLine()) { - // Read through string and split at each comma, adding - // to a string array - String[] v = fs.nextLine().split(","); - - int chg = Integer.parseInt(v[2]); - - // Adding all three elements to a map with the specific - // key added for each type of variable - Map indiForkMap = new HashMap<>(); - indiForkMap.put("id", v[0]); - indiForkMap.put("tm", v[1]); - indiForkMap.put("chg", String.valueOf(chg)); - forkList.add(indiForkMap); - } - // Throws a file not found exception if the file does not exist - } catch (FileNotFoundException e) { - System.out.println("Error reading the file: " + e.getMessage()); - s.close(); - return; - } + parseCSV(f); // Filters through the original map and adds a new list to a second map // along with the id @@ -119,4 +92,36 @@ public static void main(String[] args) { s.close(); } + public static List> parseCSV(String filename){ + + // Creates a scanner object that reads a file name + // adds contents from file into a map + List> forkList = new ArrayList<>(); + try (Scanner fs = new Scanner(new File(f))) { + fs.nextLine(); + Map indiForkMap = new HashMap<>(); + // + while (fs.hasNextLine()) { + // Read through string and split at each comma, adding + // to a string array + String[] v = fs.nextLine().split(","); + + int chg = Integer.parseInt(v[2]); + + // Adding all three elements to a map with the specific + // key added for each type of variable + + indiForkMap.put("id", v[0]); + indiForkMap.put("tm", v[1]); + indiForkMap.put("chg", String.valueOf(chg)); + forkList.add(indiForkMap); + } + // Throws a file not found exception if the file does not exist + } catch (FileNotFoundException e) { + System.out.println("Error reading the file: " + e.getMessage()); + s.close(); + return null; + } + return forkList; + } }