From c17800f71af533ee2b1f442affda20cae68521f2 Mon Sep 17 00:00:00 2001 From: didier amyot Date: Mon, 21 Aug 2023 09:11:33 -0400 Subject: [PATCH] added new exercices --- Algorithms/IdCard/IDCard.cs | 25 ++++++++++++++++++++++ Algorithms/IdCard/Name.cs | 33 +++++++++++++++++++++++++++++ Algorithms/IdCard/PostalCode.cs | 17 +++++++++++++++ Algorithms/IdCard/TelephonNumber.cs | 21 ++++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 Algorithms/IdCard/IDCard.cs create mode 100644 Algorithms/IdCard/Name.cs create mode 100644 Algorithms/IdCard/PostalCode.cs create mode 100644 Algorithms/IdCard/TelephonNumber.cs diff --git a/Algorithms/IdCard/IDCard.cs b/Algorithms/IdCard/IDCard.cs new file mode 100644 index 0000000..1d43873 --- /dev/null +++ b/Algorithms/IdCard/IDCard.cs @@ -0,0 +1,25 @@ +using Algorithms.IdCard; + +public class IDCard +{ + private Name name; + private PostalCode postalCode; + private TelephonNumber telephonNumber; + + public IDCard(Name name, + PostalCode postalCode, + TelephonNumber telephonNumber) + { + + this.name = name; + this.postalCode = postalCode; + this.telephonNumber = telephonNumber; + } + + public bool IsValid() + { + return name.IsValid() + && telephonNumber.IsValid() + && postalCode.IsValid(); + } +} \ No newline at end of file diff --git a/Algorithms/IdCard/Name.cs b/Algorithms/IdCard/Name.cs new file mode 100644 index 0000000..3d871ca --- /dev/null +++ b/Algorithms/IdCard/Name.cs @@ -0,0 +1,33 @@ +namespace Algorithms.IdCard; + +using System; + +public class Name +{ + private string firstName; + private string lastName; + + public Name(string firstName, string lastName) + { + this.firstName = firstName; + this.lastName = lastName; + } + + private bool constainsNumber(string word){ + string digits = "0123456789"; + foreach (char digit in digits){ + if(word.IndexOf(digit) != -1){ + return true; + } + } + return false; + } + + public bool IsValid(){ + if(constainsNumber(this.firstName) || constainsNumber(this.lastName)){ + return false; + } + return this.firstName.Length > 1 && this.lastName.Length > 1; + } + +} \ No newline at end of file diff --git a/Algorithms/IdCard/PostalCode.cs b/Algorithms/IdCard/PostalCode.cs new file mode 100644 index 0000000..05f5d1f --- /dev/null +++ b/Algorithms/IdCard/PostalCode.cs @@ -0,0 +1,17 @@ +using System.Text.RegularExpressions; + +namespace Algorithms.IdCard; + +public class PostalCode +{ + private string postalCode; + public PostalCode(string postalCode) + { + this.postalCode = postalCode; + } + + public bool IsValid(){ + Regex rx = new Regex(@"^[A-Z]\d[A-Z] \d[A-Z]\d$"); + return rx.Match(this.postalCode).Success; + } +} \ No newline at end of file diff --git a/Algorithms/IdCard/TelephonNumber.cs b/Algorithms/IdCard/TelephonNumber.cs new file mode 100644 index 0000000..b43e9aa --- /dev/null +++ b/Algorithms/IdCard/TelephonNumber.cs @@ -0,0 +1,21 @@ +using System.Text.RegularExpressions; + +namespace Algorithms.IdCard; + +public class TelephonNumber +{ + private string telephonNumber; + + public TelephonNumber(string telephonNumber) + { + this.telephonNumber = telephonNumber; + } + + + + public bool IsValid(){ + Regex rx = new Regex(@"^(\+?1 )?\d{3} \d{3}-\d{4}$"); + return rx.Match(this.telephonNumber).Success; + } + +} \ No newline at end of file