From 39c2969092e47451d2947d0fcb88eb775573adf1 Mon Sep 17 00:00:00 2001 From: mudabbir-ahmad Date: Wed, 2 Apr 2025 15:12:28 +0100 Subject: [PATCH] added a counter for max users made so that there are no empty rows. e.g if 3 names but 2 titles, 3rd title uses placeholder label. --- src/main/java/cvbuilder/Main.java | 50 ++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/src/main/java/cvbuilder/Main.java b/src/main/java/cvbuilder/Main.java index 9c032c2..6c0d809 100644 --- a/src/main/java/cvbuilder/Main.java +++ b/src/main/java/cvbuilder/Main.java @@ -10,6 +10,7 @@ public class Main { public static void main(String[] args) { String file = "data/cv_repo_0.csv"; UserGroup userGroup = UserGroup.getInstance(); + int maxUserCount = 0; try (BufferedReader r = new BufferedReader(new FileReader(file))) { // Skip the first line (Section,Sub-Section,Variants) @@ -18,30 +19,57 @@ public class Main { // Read all lines into memory so we can process by column String[] lines = r.lines().toArray(String[]::new); - // Parse each line - for (int lineIndex = 0; lineIndex < lines.length; lineIndex++) { - String[] parts = lines[lineIndex].split(","); + // First pass: determine maximum number of users + for (String line : lines) { + String[] parts = line.split(","); + int userCount = parts.length - 2; // Subtract first two columns + maxUserCount = Math.max(maxUserCount, userCount); + } + + // Create all users with default "Unknown" values + for (int i = 0; i < maxUserCount; i++) { + User user = userGroup.getUser(i); + user.setName("Unknown"); + user.setTitle("Unknown"); + user.setEmail("Unknown"); + user.setPhone("Unknown"); + user.setAddress("Unknown"); + } + + // Second pass: fill in actual values where available + for (String line : lines) { + String[] parts = line.split(","); + + if (parts.length < 3) continue; + + String sectionType = parts[0] + "," + parts[1]; // Process each user column (starting from column 2, index 2) for (int colIndex = 2; colIndex < parts.length; colIndex++) { int userIndex = colIndex - 2; // User index starts from 0 User user = userGroup.getUser(userIndex); + String value = parts[colIndex].trim(); - // Assign attribute based on line content - String sectionType = parts[0] + "," + parts[1]; - switch (sectionType) { - case "User,Name" -> user.setName(parts[colIndex]); - case "User,Title" -> user.setTitle(parts[colIndex]); - case "User,Email" -> user.setEmail(parts[colIndex]); - case "Contact Information,Phone" -> user.setPhone(parts[colIndex]); - case "Contact Information,Address" -> user.setAddress(parts[colIndex]); + // Only update if the value is not empty + if (!value.isEmpty()) { + switch (sectionType) { + case "User,Name" -> user.setName(value); + case "User,Title" -> user.setTitle(value); + case "User,Email" -> user.setEmail(value); + case "Contact Information,Phone" -> user.setPhone(value); + case "Contact Information,Address" -> user.setAddress(value); + } } } } + + System.out.println("Total users created: " + maxUserCount); // console debug } catch (Exception e) { System.out.print(e); } + //outside reader. + new MainViewer(); } } \ No newline at end of file