diff --git a/src/Main.java b/src/Main.java index e5571ea..8121161 100644 --- a/src/Main.java +++ b/src/Main.java @@ -6,34 +6,18 @@ 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<>(); - 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; - } + parseCSV(f); + // 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 +26,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 +47,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 +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); + // perform some basic math to grab stats double tot = 0.0; int tlc = 0; for (Map d : sel) { @@ -90,6 +81,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); @@ -100,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; + } }