Skip to content
Open
Show file tree
Hide file tree
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
62 changes: 41 additions & 21 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,25 @@

public class Main {
public static void main(String[] args) {
// Initializing scanner, taking input on the filename
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]);
// call parseCSV method to get all data
List<Map<String, String>> allData = parseCSV(f);

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());
// if allData empty then the file was not found or had problem
if (allData.isEmpty()) {
System.out.println("No data found or error reading the file.");
s.close();
return;
}

// grouping data by id
Map<String, List<Map<String, String>>> mp2 = new HashMap<>();
for (Map<String, String> d : dta) {
for (Map<String, String> d : allData) {
String id = d.get("id");
List<Map<String, String>> lst = mp2.get(id);
if (lst == null) {
Expand All @@ -50,25 +41,27 @@ public static void main(String[] args) {

List<Map<String, String>> sel;
if (inp.equalsIgnoreCase("all")) {
sel = dta;
sel = allData;
} else {
String id = "fork" + inp;
String id = "fork" + inp;
sel = mp2.get(id);
}

int sz = sel.size();

// format timestamp
DateTimeFormatter f1 = DateTimeFormatter.ISO_DATE_TIME;
LocalDateTime lat = null;
for (Map<String, String> d : sel) {
LocalDateTime t = LocalDateTime.parse(d.get("tm"), f1);
LocalDateTime t = LocalDateTime.parse(d.get("tm"), f1);
if (lat == null || t.isAfter(lat)) {
lat = t;
}
}
DateTimeFormatter f2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
String latT = lat.format(f2);

// manipulating chg data
double tot = 0.0;
int tlc = 0;
for (Map<String, String> d : sel) {
Expand All @@ -90,6 +83,7 @@ public static void main(String[] args) {
}
}

// printing out report of requested forks
System.out.println("\nStatistics:");
System.out.println("Number of commits: " + sz);
System.out.println("Most recent commit timestamp: " + latT);
Expand All @@ -100,4 +94,30 @@ public static void main(String[] args) {

s.close();
}
}

public static List<Map<String, String>> parseCSV(String f) {
List<Map<String, String>> allData = new ArrayList<>();
try (Scanner fs = new Scanner(new File(f))) {
// Opens file in scanner, iterates across, initializes ArrayList, coalescses
// stats into a hashmap, then places in an array list
fs.nextLine();

while (fs.hasNextLine()) {
String[] v = fs.nextLine().split(",");

int chg = Integer.parseInt(v[2]);
// one HashMap = one commit
Map<String, String> valueMap = new HashMap<>();
valueMap.put("id", v[0]);
valueMap.put("tm", v[1]);
valueMap.put("chg", String.valueOf(chg));
allData.add(valueMap);
}
}

catch (FileNotFoundException e) {
System.out.println("Error reading the file: " + e.getMessage());
}
return allData;
}
}
2 changes: 1 addition & 1 deletion src/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class MainTest {
void testParseCSV() {
// You will finish implementing this method in Wave 3
// TODO: Call Main.parseCSV("data/small_commit_data.csv") here and set it to an actual variable

List<Map<String,String>> parsed = Main.parseCSV("data/small_commit_data.csv");

// Sets up the expected value for you. You do not need to edit this part
List<Map<String, String>> expectedCommits = new ArrayList<>();
Expand Down