Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 46 additions & 22 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Map<String, String>> 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<String, String> 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<String, List<Map<String, String>>> mp2 = new HashMap<>();
for (Map<String, String> d : dta) {
for (Map<String, String> d : forkList) {
String id = d.get("id");
List<Map<String, String>> lst = mp2.get(id);
if (lst == null) {
Expand All @@ -42,22 +26,28 @@ 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<Map<String, String>> sel;
if (inp.equalsIgnoreCase("all")) {
sel = dta;
sel = forkList;
} else {
String id = "fork" + inp;
sel = mp2.get(id);
}

int sz = sel.size();

// formats the date and time
DateTimeFormatter f1 = DateTimeFormatter.ISO_DATE_TIME;
LocalDateTime lat = null;
for (Map<String, String> d : sel) {
Expand All @@ -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<String, String> d : sel) {
Expand All @@ -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);
Expand All @@ -100,4 +92,36 @@ public static void main(String[] args) {

s.close();
}
public static List<Map<String, String>> parseCSV(String filename){

// Creates a scanner object that reads a file name
// adds contents from file into a map
List<Map<String, String>> forkList = new ArrayList<>();
try (Scanner fs = new Scanner(new File(f))) {
fs.nextLine();
Map<String, String> 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;
}
}