From 8ae27381366c9f30d973268b73fd87ec470dcd42 Mon Sep 17 00:00:00 2001 From: Poral Date: Sat, 12 Aug 2023 14:42:39 +0800 Subject: [PATCH 01/17] Project updated. --- .gitignore | 29 +++++++++++++++++++ .idea/misc.xml | 6 ++++ .idea/modules.xml | 8 ++++++ .idea/vcs.xml | 6 ++++ .idea/workspace.xml | 68 +++++++++++++++++++++++++++++++++++++++++++++ CSV-Validation.iml | 11 ++++++++ src/Main.java | 50 +++++++++++++++++++++++++++++++++ src/Validation.java | 45 ++++++++++++++++++++++++++++++ 8 files changed, 223 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100644 CSV-Validation.iml create mode 100644 src/Main.java create mode 100644 src/Validation.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..cf9abe6 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..e28e859 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..19944f2 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + + + + + + + + { + "associatedIndex": 4 +} + + + + + { + "keyToString": { + "RunOnceActivity.OpenProjectViewOnStart": "true", + "RunOnceActivity.ShowReadmeOnStart": "true", + "git-widget-placeholder": "master", + "onboarding.tips.debug.path": "C:/Users/alejo/IdeaProjects/CSV-Validation/src/Main.java", + "project.structure.last.edited": "Project", + "project.structure.proportion": "0.0", + "project.structure.side.proportion": "0.0" + } +} + + + + + + + + + + 1691739596638 + + + + \ No newline at end of file diff --git a/CSV-Validation.iml b/CSV-Validation.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/CSV-Validation.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..0b98973 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,50 @@ +/* + * GOGO IT Lab, Inc. skill assessment for Java Developer position. + * Author: Alejo Rostata + * Description: This program reads a CSV file, validates the input data, and outputs the data to standard output. + * It performs validation on fields like id, name, birthday, gender, and phone number based on specific rules. + * The validated data is output along with error messages for invalid data. The header of the CSV file is excluded + * from validation. + * OUTLINE: https://drive.google.com/drive/folders/1n2_GFtIUoR0O2T_m51p6ALWnSkkWJ6_r + * + * SAMPLE INPUT: writers.csv + * id,name,birthday,gender,phone-number + * 1,Liam,1980/02/29,male,09011112222 + * 2,Austin,1994/10/10,female,08033334444 + * 3,"Jonathan,Jr",1989/01/23,female,0355556666 + */ + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; + +public class Main { + public static void main(String[] args) { + String csvFilePath = "C:\\Users\\alejo\\IdeaProjects\\CSV-Validation\\out\\production\\CSV-Validation\\writers.csv"; + + try (BufferedReader br = new BufferedReader(new FileReader(csvFilePath))) { + String header = br.readLine(); // Read and print the header + System.out.println(header); //print the header as-is. + br.lines() + .map(line -> line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)", -1)) + .map(Main::validateFields) //Validate each field in the line. + .forEach(System.out::println); //Print the validated line. + } catch (IOException e) { + e.printStackTrace(); //It's usually better to use a more robust logging, but for simplicity we'll just use printStackTrace(). + } + } + + private static String validateFields(String[] fields) { + StringBuilder outputLine = new StringBuilder(); + + for (int i = 0; i < fields.length; i++) { //We loop through the fields on the line. + String fieldValue = fields[i]; + String validatedValue = Validation.validateField(i, fieldValue); + if (i > 0) + outputLine.append(","); + outputLine.append(validatedValue); + } + return outputLine.toString(); //return the validated line. + } +} + diff --git a/src/Validation.java b/src/Validation.java new file mode 100644 index 0000000..7d1efe1 --- /dev/null +++ b/src/Validation.java @@ -0,0 +1,45 @@ +/* + * Validation Class + * Author: Alejo Rostata + * Description: This class provides validation logic for the fields in a CSV file. It performs checks for id, name, + * birthday, gender, and phone number based on specific rules. If the validation fails, appropriate error messages + * are returned. If validation passes, the original value is returned. + */ + +import java.util.regex.Pattern; + +public class Validation { + + //Based on the outline, we only have 3 different error message. It's better to put it into string array. + private static final String[] validationErrors = { + "Error required", + "Error limit over", + "Error format" + }; + + //I usually used lambda, it's beautiful, short and clean. + public static String validateField(int index, String value) { + return switch (index) { + case 0 -> // id + value.matches("\\d+") ? value : validationErrors[2]; + case 1 -> { // name + if (value.isEmpty()) + yield validationErrors[0]; + yield value.length() > 100 ? validationErrors[1] : value; + } + case 2 -> { // birthday + if (value.isEmpty()) + yield validationErrors[0]; + yield Pattern.matches("\\d{4}(/|-)\\d{2}(/|-)\\d{2}", value) ? value : validationErrors[2]; + } + case 3 -> // gender + (!value.isEmpty() && value.matches("(?i)male|female")) ? value : validationErrors[2]; + case 4 -> { // phone-number + if (value.isEmpty()) + yield validationErrors[0]; + yield Pattern.matches("\\d{3}(-|\\s)?\\d{3,4}(-|\\s)?\\d{4}|\\d{4}-\\d{2}-\\d{4}|\\d{10,11}", value) ? value : validationErrors[2]; + } + default -> value; + }; + } +} From 03c8bfc5bedfc3ddcc78923a04f926c4725f9da7 Mon Sep 17 00:00:00 2001 From: Poral Date: Sat, 12 Aug 2023 14:54:25 +0800 Subject: [PATCH 02/17] Project updated. --- .idea/workspace.xml | 46 ++++++++++++++++++++++++++------------------- src/Main.java | 1 - 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 19944f2..bb79890 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -4,15 +4,9 @@