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
69 changes: 44 additions & 25 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,19 @@

public class Main {
public static void main(String[] args) {

// Initializing a scanner that accepts input from the terminal
Scanner s = new Scanner(System.in);

// Prints a prompt for the user to input a file name
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(",");
// The variable takes in the file name inputed by the user as a String
String filename = s.nextLine();

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;
}
List<Map<String, String>> commitStats = parseCSV(filename);

Map<String, List<Map<String, String>>> mp2 = new HashMap<>();
for (Map<String, String> d : dta) {
for (Map<String, String> d : commitStats) {
String id = d.get("id");
List<Map<String, String>> lst = mp2.get(id);
if (lst == null) {
Expand All @@ -50,9 +35,9 @@ public static void main(String[] args) {

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

Expand All @@ -61,7 +46,7 @@ public static void main(String[] args) {
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;
}
Expand Down Expand Up @@ -99,5 +84,39 @@ public static void main(String[] args) {
System.out.println("Min lines changed in a commit: " + mn);

s.close();

}

public static List<Map<String, String>> parseCSV(String filename) {

// Lists all the commits were made and prints out the data of each commit
List<Map<String, String>> commitStats = new ArrayList<>();
// Scans the new file from user's input
try (Scanner fs = new Scanner(new File(filename))) {
fs.nextLine();

// While there's still information in the file, the scanner will continuously
// scan for files
while (fs.hasNextLine()) {

// Array of strings to show the results of scanner that splits each line with a
// comma
String[] v = fs.nextLine().split(",");

int chg = Integer.parseInt(v[2]);

Map<String, String> commits = new HashMap<>();
commits.put("id", v[0]);
commits.put("tm", v[1]);
commits.put("chg", String.valueOf(chg));
commitStats.add(commits);
}
} catch (FileNotFoundException e) {
System.out.println("Error reading the file: " + e.getMessage());
}

return commitStats;

}

}
63 changes: 33 additions & 30 deletions src/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,37 @@
import org.junit.jupiter.api.Test;

public class MainTest {
@Test
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


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

Map<String, String> commit1 = new HashMap<>();
commit1.put("chg", "179");
commit1.put("tm", "2024-10-15T08:02:04Z");
commit1.put("id", "fork1");
expectedCommits.add(commit1);

Map<String, String> commit2 = new HashMap<>();
commit2.put("chg", "1");
commit2.put("tm", "2024-10-12T19:48:59Z");
commit2.put("id", "fork2");
expectedCommits.add(commit2);

Map<String, String> commit3 = new HashMap<>();
commit3.put("chg", "13");
commit3.put("tm", "2024-10-10T21:56:18Z");
commit3.put("id", "fork2");
expectedCommits.add(commit3);
// Finish expected value setup

// TODO: Assert that the expected equals the actual
}
@Test
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>> readCsv = 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<>();

Map<String, String> commit1 = new HashMap<>();
commit1.put("chg", "179");
commit1.put("tm", "2024-10-15T08:02:04Z");
commit1.put("id", "fork1");
expectedCommits.add(commit1);

Map<String, String> commit2 = new HashMap<>();
commit2.put("chg", "1");
commit2.put("tm", "2024-10-12T19:48:59Z");
commit2.put("id", "fork2");
expectedCommits.add(commit2);

Map<String, String> commit3 = new HashMap<>();
commit3.put("chg", "13");
commit3.put("tm", "2024-10-10T21:56:18Z");
commit3.put("id", "fork2");
expectedCommits.add(commit3);
// Finish expected value setup

// TODO: Assert that the expected equals the actual
readCsv = expectedCommits;
}
}