Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.03 KB | None | 0 0
  1.         /// <summary>
  2.         /// Tries to parse a string into a Room Type.
  3.         /// </summary>
  4.         /// <param name="s">String to parse</param>
  5.         /// <param name="r">Returns the room result</param>
  6.         /// <returns>If the parse was successful.</returns>
  7.         public static bool TryParse(string s, out Room r)
  8.         {
  9.             // Used for empty rooms
  10.             if (string.IsNullOrEmpty(s))
  11.             {
  12.                 r = new Room(null);
  13.                 return true;
  14.             }
  15.             try
  16.             {
  17.                 char dorm;
  18.                 int floor;
  19.                 int room;
  20.  
  21.                 var stringCount = s.Length;
  22.                 if (stringCount >= 2 && stringCount <= 6)
  23.                 {
  24.                     string[] digits = new string[stringCount];
  25.                     for (int i = 0; i < stringCount; i++)
  26.                     {
  27.                         digits[i] = s.Substring(i, 1);
  28.                     }
  29.                    
  30.                     if (char.TryParse(digits[0], out dorm) && char.IsLetter(dorm)) // First letter
  31.                     {
  32.                         dorm = char.ToLower(dorm);
  33.                         int lengthToRoom = 0;
  34.                         string secondDigit = digits[1];
  35.                         if (secondDigit == "-" && stringCount < 5) // B-...
  36.                         {
  37.                             floor = 0;
  38.                             lengthToRoom = 2;
  39.                         }
  40.                         else if (int.TryParse(secondDigit, out floor)) // B0...
  41.                         {
  42.                             if (stringCount == 2)
  43.                             {
  44.                                 floor = 0;
  45.                                 lengthToRoom = 1;
  46.                             }
  47.                             else
  48.                             {
  49.                                 string thirdDigit = digits[2];
  50.                                 if (thirdDigit == "-" && stringCount < 6) // B0-...
  51.                                 {
  52.                                     lengthToRoom = 3;
  53.                                 }
  54.                                 else if (int.TryParse(thirdDigit, out floor)) // B00...
  55.                                 {
  56.                                     if (stringCount == 3)
  57.                                     {
  58.                                         floor = 0;
  59.                                         lengthToRoom = 1;
  60.                                     }
  61.                                     else if (digits[3] == "-") // B00-...
  62.                                     {
  63.                                         lengthToRoom = 4;
  64.                                     }
  65.                                     else
  66.                                     {
  67.                                         throw new Exception("Invalid Input");
  68.                                     }
  69.                                 }
  70.                                 else
  71.                                 {
  72.                                     throw new Exception("Invalid Input");
  73.                                 }
  74.                             }
  75.                            
  76.                         }
  77.                         else
  78.                         {
  79.                             throw new Exception("Invalid Input");
  80.                         }
  81.  
  82.                         // Solve last digits.
  83.                         var roomDigits = stringCount - lengthToRoom;
  84.                         if (!int.TryParse(s.Substring(lengthToRoom, roomDigits), out room))
  85.                         {
  86.                             throw new Exception("Invalid Input");
  87.                         }
  88.  
  89.                         r = new Room(dorm, floor, room);
  90.                         return true;
  91.                     }
  92.                     else
  93.                     {
  94.                         throw new Exception("Invalid Input");
  95.                     }
  96.                 }
  97.             }
  98.             catch { }
  99.             r = new Room('A', 0, 0);
  100.             return false;
  101.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement