diff --git a/src/Main.java b/src/Main.java index e5571ea..f96ac12 100644 --- a/src/Main.java +++ b/src/Main.java @@ -6,58 +6,48 @@ public class Main { public static void main(String[] args) { + + //Instantiating new scanner Scanner s = new Scanner(System.in); + //Ask user for the file name 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(","); + List> data = parseCSV(f); - 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; - } - Map>> mp2 = new HashMap<>(); - for (Map d : dta) { + //Create new map that organize commits by ID + //If list is empty, it will add it into a new arrayList + Map>> forkCommits = new HashMap<>(); + for (Map d : data) { String id = d.get("id"); - List> lst = mp2.get(id); + List> lst = forkCommits.get(id); if (lst == null) { lst = new ArrayList<>(); - mp2.put(id, lst); + forkCommits.put(id, lst); } lst.add(d); } - int cnt = mp2.size(); + int cnt = forkCommits.size(); + //Print out the forks avalaible 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(); + // Select which commits to analyze dependent on the user input List> sel; if (inp.equalsIgnoreCase("all")) { - sel = dta; + sel = data; } else { String id = "fork" + inp; - sel = mp2.get(id); + sel = forkCommits.get(id); } int sz = sel.size(); + // Determine the most recent commit DateTimeFormatter f1 = DateTimeFormatter.ISO_DATE_TIME; LocalDateTime lat = null; for (Map d : sel) { @@ -69,6 +59,7 @@ public static void main(String[] args) { DateTimeFormatter f2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); String latT = lat.format(f2); + //Calculate statistics from the commits chosen double tot = 0.0; int tlc = 0; for (Map d : sel) { @@ -76,20 +67,26 @@ public static void main(String[] args) { tot += lc; tlc += lc; } + //Calculate average lines changes double avg = tot / sz; int mx = Integer.MIN_VALUE; int mn = Integer.MAX_VALUE; for (Map d : sel) { + //Calculate changed lines in csv int chg = Integer.parseInt(d.get("chg")); + //Check if change is greater than max + //If change is greater than max, max = change if (chg > mx) { mx = chg; } + //If change is less than minimum, min = change if (chg < mn) { mn = chg; } } + //Print Results System.out.println("\nStatistics:"); System.out.println("Number of commits: " + sz); System.out.println("Most recent commit timestamp: " + latT); @@ -98,6 +95,34 @@ 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); + //Close scanner s.close(); } -} + + //Helper method for parsing the CSV file that the user inputs + public static List> parseCSV(String filename){ + List> data = new ArrayList<>(); + + //Read in the csv files + try (Scanner fs = new Scanner(new File(filename))) { + fs.nextLine(); + while (fs.hasNextLine()) { + //Split the data apart by "," + String[] v = fs.nextLine().split(","); + + //split values and store them into data + int chg = Integer.parseInt(v[2]); + Map mp2 = new HashMap<>(); + mp2.put("id", v[0]); + mp2.put("tm", v[1]); + mp2.put("chg", String.valueOf(chg)); + data.add(mp2); + } + //Throw error if file is not found + } catch (FileNotFoundException e) { + System.out.println("Error reading the file: " + e.getMessage()); + } + //return the data List + return data; + } + }