Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- namespace Person
- {
- class Program
- {
- static void Main()
- {
- Person Yo = new Person("Yo", "1.234.567-2"); // True
- Person Otro = new Person("Otro", "1.234.567-6"); // False
- Yo.IntroduceYourself();
- Console.WriteLine(Yo.IsValidID());
- Console.WriteLine(Otro.IsValidID());
- }
- }
- class Person {
- public string Name { get; set; }
- public string ID { get; set; }
- public Person(string name, string id) {
- this.Name = name;
- this.ID = id;
- }
- public void IntroduceYourself() {
- System.Console.WriteLine($"My name is {this.Name} and my ID is {this.ID}");
- }
- // Check wheter the given ID is valid or not
- public bool IsValidID() {
- int[] validationMultipliers = {2,9,8,7,6,3,4}; // given multipliers
- var multipliedList = new ArrayList();
- // loop through each char and if it's digit, multiply it by its corresponding multiplier (by index)
- // and add it to multipliedList
- int i = 0;
- foreach (char strNum in this.ID) {
- try {
- if (Char.IsDigit(strNum)) {
- int digit = (int)Char.GetNumericValue(strNum);
- multipliedList.Add(lastNum(digit * validationMultipliers[i]));
- i++;
- }
- }
- // last digit raises outofindex, do nothing with it
- catch (System.IndexOutOfRangeException) {}
- }
- // sum all numbers from multipliedList
- int multipliedSum = 0;
- foreach (int x in multipliedList) { multipliedSum += x; }
- // check if (multipliedSum - nearest greater 10-multiple) matches with verifier digit
- // eg: if multipliedSum == 56, verifier = 60 - 54 = 6
- int verifier = 10 - (multipliedSum % 10);
- int lasDigit = (int)Char.GetNumericValue(ID[ID.Length - 1]);
- return verifier == lasDigit;
- }
- // from a given integer number, return its last digit
- public static int lastNum(int num) { return num % 10; }
- }
- }
Add Comment
Please, Sign In to add comment