Advertisement
skipter

C# Arrays, Methods, If-else... PhoneBook*

Jun 24th, 2017
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.33 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. namespace _4.Phone
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             string[] phoneNumbers = Console.ReadLine().Split();
  10.             string[] phoneNames = Console.ReadLine().Split();
  11.             string[] inputNames = Console.ReadLine().Split();
  12.             while (inputNames[0] != "done")
  13.             {
  14.                 if (inputNames[0] == "call")
  15.                 {
  16.                     for (int cnt = 0; cnt < phoneNames.Length; cnt++)
  17.                     {
  18.                         if (inputNames[1] == phoneNames[cnt])
  19.                         {
  20.                             Console.WriteLine($"calling {phoneNumbers[cnt]}...");
  21.                             string phoneNum = phoneNumbers[cnt];
  22.                             int digitsSum = GetDigitsSum(phoneNum);
  23.                             if (digitsSum % 2 == 1)
  24.                             {
  25.                                 Console.WriteLine("no answer");
  26.                             }
  27.                             else
  28.                             {
  29.                                 int minutes = digitsSum / 60;
  30.                                 int seconds = digitsSum % 60;
  31.                                 Console.WriteLine($"call ended. duration: {minutes:d2}:{seconds:d2}");
  32.                             }
  33.                         }
  34.                         if (inputNames[1] == phoneNumbers[cnt])
  35.                         {
  36.                             Console.WriteLine($"calling {phoneNames[cnt]}...");
  37.                             string phoneNum = phoneNumbers[cnt];
  38.                             int digitsSum = GetDigitsSum(phoneNum);
  39.                             if (digitsSum % 2 == 1)
  40.                             {
  41.                                 Console.WriteLine("no answer");
  42.                             }
  43.                             else
  44.                             {
  45.                                 int minutes = digitsSum / 60;
  46.                                 int seconds = digitsSum % 60;
  47.                                 Console.WriteLine($"call ended. duration: {minutes:d2}:{seconds:d2}");
  48.                             }
  49.                         }
  50.                     }
  51.                 }
  52.  
  53.                 if (inputNames[0] == "message")
  54.                 {
  55.                     for (int cnt = 0; cnt < phoneNames.Length; cnt++)
  56.                     {
  57.                         if (inputNames[1] == phoneNames[cnt])
  58.                         {
  59.                             Console.WriteLine($"sending sms to {phoneNumbers[cnt]}...");
  60.                             string phoneNum = phoneNumbers[cnt];
  61.                             int phoneNumSum = GetDigitsSum(phoneNum);
  62.                             if (phoneNumSum % 2 == 0)
  63.                             {
  64.                                 Console.WriteLine("meet me there");
  65.                             }
  66.                             else
  67.                             {
  68.                                 Console.WriteLine("busy");
  69.                             }
  70.                         }
  71.                         if (inputNames[1] == phoneNumbers[cnt])
  72.                         {
  73.                             Console.WriteLine($"sending sms to {phoneNames[cnt]}...");
  74.                             string phoneNum = phoneNames[cnt];
  75.                             int phoneNumSum = GetDigitsSum(phoneNum);
  76.                             if (phoneNumSum % 2 == 0)
  77.                             {
  78.                                 Console.WriteLine("meet me there");
  79.                             }
  80.                             else
  81.                             {
  82.                                 Console.WriteLine("busy");
  83.                             }
  84.                         }
  85.                     }
  86.                     inputNames = Console.ReadLine().Split();
  87.                 }
  88.             }
  89.         }
  90.         private static int GetDigitsSum(string phoneNumbera)
  91.         {
  92.             int getPhoneNumberSum = 0;
  93.             for (int indexCounter = 0; indexCounter < phoneNumbera.Length; indexCounter++)
  94.             {
  95.                 if (phoneNumbera[indexCounter] >= 48 && phoneNumbera[indexCounter] <= 57)
  96.                 {
  97.                     getPhoneNumberSum += ((int)phoneNumbera[indexCounter] - 48);
  98.                 }
  99.             }
  100.             return getPhoneNumberSum;
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement