Guest User

Untitled

a guest
Aug 20th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3.  
  4. namespace Person
  5. {
  6. class Program
  7. {
  8. static void Main()
  9. {
  10. Person Yo = new Person("Yo", "1.234.567-2"); // True
  11. Person Otro = new Person("Otro", "1.234.567-6"); // False
  12. Yo.IntroduceYourself();
  13. Console.WriteLine(Yo.IsValidID());
  14. Console.WriteLine(Otro.IsValidID());
  15. }
  16. }
  17.  
  18. class Person {
  19.  
  20. public string Name { get; set; }
  21. public string ID { get; set; }
  22. public Person(string name, string id) {
  23. this.Name = name;
  24. this.ID = id;
  25. }
  26.  
  27. public void IntroduceYourself() {
  28. System.Console.WriteLine($"My name is {this.Name} and my ID is {this.ID}");
  29. }
  30.  
  31. // Check wheter the given ID is valid or not
  32. public bool IsValidID() {
  33.  
  34. int[] validationMultipliers = {2,9,8,7,6,3,4}; // given multipliers
  35. var multipliedList = new ArrayList();
  36.  
  37. // loop through each char and if it's digit, multiply it by its corresponding multiplier (by index)
  38. // and add it to multipliedList
  39. int i = 0;
  40. foreach (char strNum in this.ID) {
  41. try {
  42. if (Char.IsDigit(strNum)) {
  43. int digit = (int)Char.GetNumericValue(strNum);
  44. multipliedList.Add(lastNum(digit * validationMultipliers[i]));
  45. i++;
  46. }
  47. }
  48. // last digit raises outofindex, do nothing with it
  49. catch (System.IndexOutOfRangeException) {}
  50. }
  51.  
  52. // sum all numbers from multipliedList
  53. int multipliedSum = 0;
  54. foreach (int x in multipliedList) { multipliedSum += x; }
  55.  
  56. // check if (multipliedSum - nearest greater 10-multiple) matches with verifier digit
  57. // eg: if multipliedSum == 56, verifier = 60 - 54 = 6
  58. int verifier = 10 - (multipliedSum % 10);
  59. int lasDigit = (int)Char.GetNumericValue(ID[ID.Length - 1]);
  60.  
  61. return verifier == lasDigit;
  62. }
  63.  
  64. // from a given integer number, return its last digit
  65. public static int lastNum(int num) { return num % 10; }
  66. }
  67. }
Add Comment
Please, Sign In to add comment