From 921862083eb1f33d48003766427b4c5d7a3d11d6 Mon Sep 17 00:00:00 2001 From: Hasini Samarathunga Date: Sun, 3 Oct 2021 16:39:04 +0530 Subject: [PATCH] Input validation refractored --- src/main/java/com/rgsystem/Main.java | 5 ++- .../com/rgsystem/input/CommandLineInputs.java | 27 ++++--------- .../validation/CMDInputValidations.java | 39 +++++++++++++++++++ .../rgsystem/validation/InputValidations.java | 12 ++++++ 4 files changed, 63 insertions(+), 20 deletions(-) create mode 100644 src/main/java/com/rgsystem/validation/CMDInputValidations.java create mode 100644 src/main/java/com/rgsystem/validation/InputValidations.java diff --git a/src/main/java/com/rgsystem/Main.java b/src/main/java/com/rgsystem/Main.java index 3489b91..ed64935 100644 --- a/src/main/java/com/rgsystem/Main.java +++ b/src/main/java/com/rgsystem/Main.java @@ -8,6 +8,8 @@ import com.rgsystem.input.CommandLineInputs; import com.rgsystem.ui.CmdLineUI; import com.rgsystem.ui.UI; +import com.rgsystem.validation.CMDInputValidations; +import com.rgsystem.validation.InputValidations; public class Main { @@ -20,7 +22,8 @@ public static void main(String[] args){ ); Database database = new SQLDatabase(); - Inputs inputs = new CommandLineInputs(args); + InputValidations validations = new CMDInputValidations(args); + Inputs inputs = new CommandLineInputs(args, validations); UI ui = new CmdLineUI(); ReportGeneratorApp app = new ReportGeneratorApp(connection, database, inputs, ui); diff --git a/src/main/java/com/rgsystem/input/CommandLineInputs.java b/src/main/java/com/rgsystem/input/CommandLineInputs.java index 592565a..8ef0076 100644 --- a/src/main/java/com/rgsystem/input/CommandLineInputs.java +++ b/src/main/java/com/rgsystem/input/CommandLineInputs.java @@ -1,27 +1,23 @@ package com.rgsystem.input; +import com.rgsystem.validation.InputValidations; + public class CommandLineInputs implements Inputs { private final String[] args; + private final InputValidations validations; - public CommandLineInputs(String[] args) { + public CommandLineInputs(String[] args, InputValidations validations) { this.args = args; + this.validations = validations; } @Override public String getReportType() throws InvalidInputException { - String reportType = args[0]; //Make sure to validate the arguments before using... - if (reportType == null) { - throw new InvalidInputException("Please provide the report type as an argument"); - } - - if (!(reportType.equals("daily-sales") || reportType.equals("user-signups"))) { - throw new InvalidInputException - ("Please provide daily-sales or user-signups as the report type for first argument"); - } - return reportType; + validations.validateReportType(); + return args[0]; } @Override @@ -30,15 +26,8 @@ public String getStartDate() throws InvalidInputException { String startDate = args[1]; //Make sure to validate the arguments before using... - if (startDate == null) { - throw new InvalidInputException("Please provide the start date as the second argument"); - } - - /*if(!(isDateValid(startDate))){ - throw new InvalidInputException("Please provide the start date in dd/mm/yyyy format"); - } */ - return startDate; + return args[1]; } @Override diff --git a/src/main/java/com/rgsystem/validation/CMDInputValidations.java b/src/main/java/com/rgsystem/validation/CMDInputValidations.java new file mode 100644 index 0000000..826c885 --- /dev/null +++ b/src/main/java/com/rgsystem/validation/CMDInputValidations.java @@ -0,0 +1,39 @@ +package com.rgsystem.validation; + +import com.rgsystem.input.InvalidInputException; + +public class CMDInputValidations implements InputValidations{ + + @Override + public void validateReportType(String reportType) throws InvalidInputException { + + if (reportType == null) { + throw new InvalidInputException("Please provide the report type"); + + }else (!(reportType.equals("daily-sales") || reportType.equals("user-signups"))) { + throw new InvalidInputException + ("Please provide daily-sales or user-signups as the report type for first argument"); + } + } + + @Override + public void validateDate(String date) throws InvalidInputException { + + if (date == null) { + throw new InvalidInputException("Please provide the start and end dates"); + }else (!(date.matches("([0-9]{4})/([0-9]{2})/([0-9]{2})"))){ + throw new InvalidInputException("Please provide the date in dd/mm/yyyy format"); + } + + } + + @Override + public void validateOutputFormat() throws InvalidInputException { + return null; + } + + @Override + public void validateEmail() throws InvalidInputException { + return null; + } +} diff --git a/src/main/java/com/rgsystem/validation/InputValidations.java b/src/main/java/com/rgsystem/validation/InputValidations.java new file mode 100644 index 0000000..cb2f3e1 --- /dev/null +++ b/src/main/java/com/rgsystem/validation/InputValidations.java @@ -0,0 +1,12 @@ +package com.rgsystem.validation; + +import com.rgsystem.input.InvalidInputException; + +public interface InputValidations { + + void validateReportType(String reportType) throws InvalidInputException; + void validateDate(String date) throws InvalidInputException; + void validateOutputFormat(String outputFormat) throws InvalidInputException; + void validateEmail(String email) throws InvalidInputException; + +}