Advertisement
jyoung12387

SRP Example - Create User Name

Mar 7th, 2020
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.61 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleUI
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             StandardMessages.WelcomeMessage();
  10.  
  11.             Person user = PersonDataCapture.Capture();
  12.  
  13.             bool isUserValid = PersonValidator.Validate(user);
  14.  
  15.             if (isUserValid == false)
  16.             {
  17.                 StandardMessages.EndApplication();
  18.                 return;
  19.             }
  20.  
  21.             AccountGenerator.CreateAccount(user);
  22.  
  23.             StandardMessages.EndApplication();
  24.         }
  25.     }
  26. }
  27.  
  28. using System;
  29.  
  30. namespace ConsoleUI
  31. {
  32.     class Person
  33.     {
  34.         public string FirstName { get; set; }
  35.         public string LastName { get; set; }
  36.     }
  37. }
  38.  
  39. using System;
  40.  
  41. namespace ConsoleUI
  42. {
  43.     class StandardMessages
  44.     {
  45.         public static void WelcomeMessage()
  46.         {
  47.             Console.WriteLine("Welcome to my application!");
  48.         }
  49.  
  50.         public static void EndApplication()
  51.         {
  52.             Console.ReadLine();
  53.         }
  54.  
  55.         public static void DisplayValidationError(string fieldName)
  56.         {
  57.             Console.WriteLine($"You did not give us a valid {fieldName}!");
  58.         }
  59.     }
  60. }
  61.  
  62. using System;
  63.  
  64. namespace ConsoleUI
  65. {
  66.     class PersonValidator
  67.     {
  68.         public static bool Validate(Person person)
  69.         {
  70.             // Checks to be sure the first and last names are valid
  71.             if (string.IsNullOrWhiteSpace(person.FirstName))
  72.             {
  73.                 StandardMessages.DisplayValidationError("first name");
  74.                 return false;
  75.             }
  76.  
  77.             if (string.IsNullOrWhiteSpace(person.LastName))
  78.             {
  79.                 StandardMessages.DisplayValidationError("last name");
  80.                 return false;
  81.             }
  82.  
  83.             return true;
  84.         }
  85.  
  86.     }
  87. }
  88.  
  89. using System;
  90.  
  91. namespace ConsoleUI
  92. {
  93.     class PersonDataCapture
  94.     {
  95.         public static Person Capture()
  96.         {
  97.             // Ask for user data
  98.             Person output = new Person();
  99.  
  100.             Console.Write("What is your first name: ");
  101.             output.FirstName = Console.ReadLine();
  102.  
  103.             Console.Write("What is your last name: ");
  104.             output.LastName = Console.ReadLine();
  105.  
  106.             return output;
  107.         }
  108.  
  109.     }
  110. }
  111.  
  112. using System;
  113.  
  114. namespace ConsoleUI
  115. {
  116.     class AccountGenerator
  117.     {
  118.         public static void CreateAccount(Person user)
  119.         {
  120.             // Create a username for the person
  121.             Console.WriteLine($"Your username is { user.FirstName.Substring(0, 1) }{ user.LastName }");
  122.         }
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement