Advertisement
grubcho

Phone simulator

Jun 20th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Phone
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string[] phones = Console.ReadLine().Split(' ');
  14.             string[] names = Console.ReadLine().Split(' ');
  15.             string dial = string.Empty;
  16.             while (dial != "done")
  17.             {
  18.                 dial = Console.ReadLine();
  19.                 for (int i = 0; i < names.Length; i++)
  20.                 {
  21.                     if (dial == "call "+names[i] || dial == "call "+phones[i])
  22.                     {
  23.                         if (dial == "call " + names[i])
  24.                         {
  25.                             Console.WriteLine($"calling {phones[i]}...");
  26.                             Console.WriteLine(CallingMessage(phones[i]));
  27.                         }
  28.                         else if (dial == "call " + phones[i])
  29.                         {
  30.                             Console.WriteLine($"calling {names[i]}...");
  31.                             Console.WriteLine(CallingMessage(phones[i]));
  32.                         }
  33.                     }
  34.                     if (dial == "message " + names[i] || dial == "message " + phones[i])
  35.                     {
  36.                         if (dial == "message " + names[i])
  37.                         {
  38.                             Console.WriteLine($"sending sms to {phones[i]}...");
  39.                             Console.WriteLine(SmsMessage(phones[i]));
  40.                         }
  41.                         else if (dial == "message " + phones[i])
  42.                         {
  43.                             Console.WriteLine($"sending sms to {names[i]}...");
  44.                             Console.WriteLine(SmsMessage(phones[i]));
  45.                         }
  46.                         else
  47.                         {
  48.                             break;
  49.                         }
  50.                     }
  51.                 }                
  52.             }
  53.         }
  54.         static string CallingMessage(string phone)
  55.         {
  56.             int sumDigitsPhone = SumDigits(phone);
  57.             string message = string.Empty;
  58.             if (sumDigitsPhone % 2 == 0)
  59.             {
  60.                 int mins = (int)sumDigitsPhone / 60;
  61.                 int secs = (int)sumDigitsPhone % 60;
  62.                 message = $"call ended. duration: {mins:d2}:{secs:d2}";
  63.             }
  64.             else if (sumDigitsPhone % 2 != 0)
  65.             {
  66.                 message = "no answer";
  67.             }
  68.             return message;
  69.         }
  70.         static string SmsMessage(string phone)
  71.         {
  72.             int differenceDigitsPhone = DifferenceDigits(phone);
  73.             string message = string.Empty;
  74.            
  75.             if (Math.Abs(differenceDigitsPhone) % 2 == 0)
  76.             {
  77.                 message = "meet me there";
  78.             }
  79.             else if (Math.Abs(differenceDigitsPhone) % 2 != 0)
  80.             {
  81.                 message = "busy";
  82.             }
  83.             return message;
  84.         }
  85.         static int SumDigits(string phone)
  86.         {
  87.             char[] newPhone = phone.ToCharArray();
  88.             int sumDigits = 0;
  89.             for (int i = 0; i < newPhone.Length; i++)
  90.             {
  91.                 if ((int)newPhone[i] >= 48 && (int)newPhone[i] <= 57)
  92.                 {
  93.                     sumDigits += (int)newPhone[i] - 48;
  94.                 }
  95.             }
  96.             return sumDigits;
  97.         }
  98.         static int DifferenceDigits(string phone)
  99.         {
  100.             char[] newPhone = phone.ToCharArray();
  101.             int differenceDigits = 0;
  102.             for (int i = 0; i < newPhone.Length; i++)
  103.             {
  104.                 if ((int)newPhone[i] >= 48 && (int)newPhone[i] <= 57)
  105.                 {
  106.                     differenceDigits += (int)newPhone[i] - 48;
  107.                 }
  108.             }
  109.             return differenceDigits;
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement