emilia98

Hornet Comm

Aug 9th, 2017
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace _02.HornetComm
  5. {
  6.     class Broadcast
  7.     {
  8.         public string Frequency { get; set; }
  9.         public string Message { get; set; }
  10.  
  11.         public static string ModifyFrequency(string secondQuery)
  12.         {
  13.             var newQuery = String.Empty;
  14.             for (int index = 0; index < secondQuery.Length; index++)
  15.             {
  16.                 var character = secondQuery[index];
  17.  
  18.                 if(character >= 65 && character <= 90)
  19.                 {
  20.                     character = (char)(character + 32);
  21.                 }
  22.                 else if (character >= 97 && character <= 122)
  23.                 {
  24.                     character = (char)(character - 32);
  25.                 }
  26.  
  27.                 newQuery += character;
  28.             }
  29.             return newQuery;
  30.         }
  31.     }
  32.  
  33.     class PrivateMessage
  34.     {
  35.         public string Recipient { get; set; }
  36.         public string Message { get; set; }
  37.  
  38.         public static string ReverseRecipient(string firstQuery)
  39.         {
  40.             var newQuery = String.Empty;
  41.             for (int index = firstQuery.Length - 1; index >= 0; index--)
  42.             {
  43.                 newQuery += firstQuery[index];
  44.             }
  45.             return newQuery;
  46.         }
  47.     }
  48.  
  49.     class HornetComm
  50.     {
  51.         static void Main()
  52.         {
  53.             string input = Console.ReadLine();
  54.             var privateMessages = new List<PrivateMessage>();
  55.             var broadcasts = new List<Broadcast>();
  56.  
  57.             while (input != "Hornet is Green")
  58.             {
  59.                 string[] splitInput = input.Split(new string[] { " <-> "},
  60.                                                   StringSplitOptions.None);
  61.  
  62.                 string firstQuery = string.Empty;
  63.                 string secondQuery = string.Empty;
  64.  
  65.                 bool isFirstOnlyDigits = false;
  66.                 bool isFirstWithoutDigits = false;
  67.                 bool isSecondWithDigisAndLetters = false;
  68.  
  69.                 if (splitInput.Length > 1)
  70.                 {
  71.                     firstQuery = splitInput[0];
  72.                     secondQuery = splitInput[1];
  73.                     isFirstOnlyDigits = IsFirstQueryOnlyDigits(firstQuery);
  74.                     isFirstWithoutDigits = IsFirstQueryWithoutDigits(firstQuery);
  75.                     isSecondWithDigisAndLetters = IsSecondQueryWithDigitsAndLetters(secondQuery);
  76.                 }
  77.  
  78.                 if(isFirstOnlyDigits && isSecondWithDigisAndLetters)
  79.                 {                  
  80.                     var message = new PrivateMessage()
  81.                     {
  82.                         Recipient = PrivateMessage.ReverseRecipient(firstQuery),
  83.                         Message = secondQuery,
  84.                     };
  85.                     privateMessages.Add(message);
  86.                 }
  87.                 else if(isFirstWithoutDigits && isSecondWithDigisAndLetters)
  88.                 {
  89.                     var broadcast = new Broadcast()
  90.                     {
  91.                         Frequency = Broadcast.ModifyFrequency(secondQuery),
  92.                         Message = firstQuery,
  93.                     };
  94.                     broadcasts.Add(broadcast);
  95.                 }
  96.                
  97.                 input = Console.ReadLine();
  98.             }
  99.  
  100.             PrintBroadcasts(broadcasts);
  101.             PrintPrivateMessages(privateMessages);
  102.              
  103.         }
  104.  
  105.         static bool IsFirstQueryOnlyDigits(string firstQuery)
  106.         {
  107.             for (int index = 0; index < firstQuery.Length; index++)
  108.             {
  109.                 char currentSymbol = firstQuery[index];
  110.  
  111.                 if (currentSymbol < '0' || currentSymbol > '9')
  112.                 {
  113.                     return false;
  114.                 }
  115.             }
  116.             return true;
  117.         }
  118.  
  119.         static bool IsFirstQueryWithoutDigits(string firstQuery)
  120.         {
  121.             for (int index = 0; index < firstQuery.Length; index++)
  122.             {
  123.                 char currentSymbol = firstQuery[index];
  124.  
  125.                 if (currentSymbol >= '0' && currentSymbol <= '9')
  126.                 {
  127.                     return false;
  128.                 }
  129.             }
  130.             return true;
  131.         }
  132.  
  133.         static bool IsSecondQueryWithDigitsAndLetters(string secondQuery)
  134.         {
  135.             for (int index = 0; index < secondQuery.Length; index++)
  136.             {
  137.                 char currentSymbol = secondQuery[index];
  138.  
  139.                 if (currentSymbol < 48 || currentSymbol >= 123 ||
  140.                     (currentSymbol >= 58 && currentSymbol < 65) ||
  141.                     (currentSymbol >= 91 && currentSymbol < 97))
  142.                 {
  143.                     return false;
  144.                 }
  145.             }
  146.             return true;
  147.         }
  148.  
  149.         static void PrintBroadcasts(List<Broadcast> broadcasts)
  150.         {
  151.             Console.WriteLine("Broadcasts:");
  152.             if (broadcasts.Count == 0)
  153.             {
  154.                 Console.WriteLine("None");
  155.             }
  156.             else
  157.             {
  158.                 foreach (var broadcast in broadcasts)
  159.                 {
  160.                     string freq = broadcast.Frequency;
  161.                     string message = broadcast.Message;
  162.  
  163.                     Console.WriteLine("{0} -> {1}", freq, message);
  164.                 }
  165.             }
  166.         }
  167.  
  168.         static void PrintPrivateMessages(List<PrivateMessage> privateMessages)
  169.         {
  170.             Console.WriteLine("Messages:");
  171.             if (privateMessages.Count == 0)
  172.             {
  173.                 Console.WriteLine("None");
  174.             }
  175.             else
  176.             {
  177.                 foreach (var privateMessage in privateMessages)
  178.                 {
  179.                     string recipient = privateMessage.Recipient;
  180.                     string message = privateMessage.Message;
  181.  
  182.                     Console.WriteLine("{0} -> {1}", recipient, message);
  183.                 }
  184.             }
  185.         }
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment