diff --git a/src/Main.java b/src/Main.java index e5571ea..b23f75c 100644 --- a/src/Main.java +++ b/src/Main.java @@ -8,32 +8,15 @@ public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); + // reads data from file and puts into data structures 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; - } + List> forkDirectory = parseCSV(f); + Map>> mp2 = new HashMap<>(); - for (Map d : dta) { + for (Map d : forkDirectory) { String id = d.get("id"); List> lst = mp2.get(id); if (lst == null) { @@ -44,13 +27,14 @@ public static void main(String[] args) { } int cnt = mp2.size(); + // number of forks in the data and select which fork to view 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(); List> sel; if (inp.equalsIgnoreCase("all")) { - sel = dta; + sel = forkDirectory; } else { String id = "fork" + inp; sel = mp2.get(id); @@ -58,6 +42,7 @@ public static void main(String[] args) { int sz = sel.size(); + // formats date DateTimeFormatter f1 = DateTimeFormatter.ISO_DATE_TIME; LocalDateTime lat = null; for (Map d : sel) { @@ -78,6 +63,7 @@ public static void main(String[] args) { } double avg = tot / sz; + // for max and min lines of code changed in a commit int mx = Integer.MIN_VALUE; int mn = Integer.MAX_VALUE; for (Map d : sel) { @@ -90,6 +76,7 @@ public static void main(String[] args) { } } + // display results System.out.println("\nStatistics:"); System.out.println("Number of commits: " + sz); System.out.println("Most recent commit timestamp: " + latT); @@ -100,4 +87,27 @@ public static void main(String[] args) { s.close(); } + + public static List> parseCSV(String filename) { + List> forkDirectory = 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 initialToken = new HashMap<>(); + initialToken.put("id", v[0]); + initialToken.put("tm", v[1]); + initialToken.put("chg", String.valueOf(chg)); + + forkDirectory.add(initialToken); + } + } catch (FileNotFoundException e) { + System.out.println("Error reading the file: " + e.getMessage()); + } + return forkDirectory; + } }