public static MobileAccount GetByName(string adress, string name) { XDocument xdoc = XDocument.Load(adress); XElement Account = xdoc.Element("MobileOperator").Element("Accounts"); foreach (XElement MobileAccounts in Account.Elements("MobileAccount")) { XElement NameElement = MobileAccounts.Element("Name"); XElement LastNameElement = MobileAccounts.Element("LastName"); XElement BirthDateElement = MobileAccounts.Element("BirthDate"); XElement EmailElement = MobileAccounts.Element("Email"); XElement NumberElement = MobileAccounts.Element("Number"); if (name == NameElement.Value) { MobileAccount user = new MobileAccount(); user.Name = NameElement.Value; user.LastName = LastNameElement.Value; user.BirthDate = DateTime.Parse(BirthDateElement.Value); user.Email = EmailElement.Value; user.Number = Int32.Parse(NumberElement.Value); return(user); } } return(default(MobileAccount)); }
private void TrySendSms(object sender, AccountEventArgs e) { LogSmsEvent("Try to send sms.", e, MessageType.Message); try { mobileAccountSender = sender as MobileAccount; mobileAccountReceiver = MobileAccounts.FirstOrDefault(mobileAccount => e.ReceiverNumber.Equals(mobileAccount.Account.Number)); if (string.IsNullOrEmpty(mobileAccountSender?.ToString()) || string.IsNullOrEmpty(mobileAccountReceiver?.ToString())) { throw new NullReferenceException(); } if (mobileAccountSender.Account.Number == 2219324 || mobileAccountSender.Account.Number == 2219325 || mobileAccountReceiver.Account.Number == 2219324 || mobileAccountReceiver.Account.Number == 2219325) { throw new ArgumentException(); } } catch (NullReferenceException) { if (mobileAccountSender != null) { mobileAccountSender.OnSmsHandler -= ReceiveSms; } LogSmsEvent("Sms wasn't send.", e); return; } catch (ArgumentException) { if (mobileAccountSender != null) { mobileAccountSender.OnSmsHandler -= ReceiveSms; } LogSmsEvent("Sms wasn't send.", e); return; } catch (Exception) { if (mobileAccountSender != null) { mobileAccountSender.OnSmsHandler -= ReceiveSms; } LogSmsEvent("Sms wasn't send.", e); return; } LogSmsEvent("Sending.", e, MessageType.Message); mobileAccountReceiver.ReceiveSms(mobileAccountSender.Account.Number); }
public MobileAccount CreateMobileAccount() { var mobileAccount = new MobileAccount(); MobileAccounts.Add(mobileAccount); mobileAccount.OnCallHandler += EndCall; mobileAccount.OnCallHandler += TryMakeCall; mobileAccount.OnSmsHandler += ReceiveSms; mobileAccount.OnSmsHandler += TrySendSms; return(mobileAccount); }
public static List <string> Sample(string adress, string fieldName) { List <string> fields = new List <string>(); XDocument xdoc = XDocument.Load(adress); XElement Account = xdoc.Element("MobileOperator").Element("Accounts"); foreach (XElement MobileAccounts in Account.Elements("MobileAccount")) { XElement SomeField = MobileAccounts.Element(fieldName); fields.Add(SomeField.Value.ToString()); } return(fields); }
public MobileAccount FindMobileAccountByName(string name) { var mobileAccount = MobileAccounts.SingleOrDefault(mobileAcc => mobileAcc.Account.Name == name); if (mobileAccount == null) { return(null); } mobileAccount.OnCallHandler += TryMakeCall; mobileAccount.OnCallHandler += EndCall; mobileAccount.OnSmsHandler += TrySendSms; mobileAccount.OnSmsHandler += ReceiveSms; return(mobileAccount); }
public static void RemuveByName(string adress, string name) { XDocument xdoc = XDocument.Load(adress); XElement Account = xdoc.Element("MobileOperator").Element("Accounts"); foreach (XElement MobileAccounts in Account.Elements("MobileAccount")) { XElement NameElement = MobileAccounts.Element("Name"); if (name == NameElement.Value) { MobileAccounts.RemoveAll(); Console.WriteLine(name + " was successfully removed"); } } xdoc.Save("removed" + adress); }
private void TryMakeCall(object sender, AccountEventArgs e) { LogCallEvent("Try to call.", e, MessageType.Call); try { mobileAccountSender = sender as MobileAccount; mobileAccountReceiver = MobileAccounts.FirstOrDefault(mobileAccount => e.ReceiverNumber.Equals(mobileAccount.Account.Number)); if (string.IsNullOrEmpty(mobileAccountSender?.ToString()) || string.IsNullOrEmpty(mobileAccountReceiver?.ToString())) { throw new NullReferenceException(); } if (mobileAccountSender.Account.Number == 2219324 || mobileAccountSender.Account.Number == 2219325 || mobileAccountReceiver.Account.Number == 2219324 || mobileAccountReceiver.Account.Number == 2219325) { throw new ArgumentException(); } } catch (NullReferenceException) { e.IsHandled = true; LogCallEvent("Call crashed.", e); return; } catch (ArgumentException) { e.IsHandled = true; LogCallEvent("Call crashed.", e); return; } catch (Exception) { e.IsHandled = true; LogCallEvent("Call crashed.", e); return; } LogCallEvent("Speaking.", e, MessageType.Call); mobileAccountReceiver.ReceiveCall(mobileAccountSender.Account.Number); }
public static void FindByName(string adress, string name) { XDocument xdoc = XDocument.Load(adress); XElement Account = xdoc.Element("MobileOperator").Element("Accounts"); foreach (XElement MobileAccounts in Account.Elements("MobileAccount")) { XElement NameElement = MobileAccounts.Element("Name"); XElement LastNameElement = MobileAccounts.Element("LastName"); XElement BirthDateElement = MobileAccounts.Element("BirthDate"); XElement EmailElement = MobileAccounts.Element("Email"); XElement NumberElement = MobileAccounts.Element("Number"); if (name == NameElement.Value) { Console.WriteLine("Name: {0} Last name: {1} Birth date: {2} Email: {3} Number: {4}", NameElement.Value, LastNameElement.Value, BirthDateElement.Value, EmailElement.Value, NameElement.Value); } } }
private void TrySendSms(object sender, AccountEventArgs e) { try { mobileAccountSender = sender as MobileAccount; mobileAccountReceiver = MobileAccounts.FirstOrDefault(mobileAccount => e.ReceiverNumber.Equals(mobileAccount.Account.Number)); if (string.IsNullOrEmpty(mobileAccountSender?.ToString()) || string.IsNullOrEmpty(mobileAccountReceiver?.ToString())) { throw new NullReferenceException(); } if (mobileAccountSender.Account.Number == 2219324 || mobileAccountSender.Account.Number == 2219325 || mobileAccountReceiver.Account.Number == 2219324 || mobileAccountReceiver.Account.Number == 2219325) { throw new ArgumentException(); } } catch (NullReferenceException) { if (mobileAccountSender != null) { mobileAccountSender.OnSmsHandler -= ReceiveSms; } LogSmsEvent("Sms wasn't send.", true); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Sms wasn't send."); Console.WriteLine(); Console.ForegroundColor = ConsoleColor.White; return; } catch (ArgumentException) { if (mobileAccountSender != null) { mobileAccountSender.OnSmsHandler -= ReceiveSms; } LogSmsEvent("Sms wasn't send.", true); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Sms wasn't send."); Console.WriteLine(); Console.ForegroundColor = ConsoleColor.White; return; } catch (Exception standartException) { if (mobileAccountSender != null) { mobileAccountSender.OnSmsHandler -= ReceiveSms; } LogSmsEvent("Sms wasn't send.", true); Console.WriteLine(standartException.Message + Environment.NewLine); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Sms wasn't send."); Console.WriteLine(); Console.ForegroundColor = ConsoleColor.White; return; } LogSmsEvent("Try to send sms"); mobileAccountReceiver.ReceiveSms(mobileAccountSender.Account.Number); }
private void TryMakeCall(object sender, AccountEventArgs e) { try { mobileAccountSender = sender as MobileAccount; mobileAccountReceiver = MobileAccounts.FirstOrDefault(mobileAccount => e.ReceiverNumber.Equals(mobileAccount.Account.Number)); if (string.IsNullOrEmpty(mobileAccountSender?.ToString()) || string.IsNullOrEmpty(mobileAccountReceiver?.ToString())) { throw new NullReferenceException(); } if (mobileAccountSender.Account.Number == 2219324 || mobileAccountSender.Account.Number == 2219325 || mobileAccountReceiver.Account.Number == 2219324 || mobileAccountReceiver.Account.Number == 2219325) { throw new ArgumentException(); } } catch (NullReferenceException) { if (mobileAccountSender != null) { mobileAccountSender.OnCallHandler -= EndCall; } LogCallEvent("Call crashed.", true); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Call crashed"); Console.WriteLine(); Console.ForegroundColor = ConsoleColor.White; return; } catch (ArgumentException) { if (mobileAccountSender != null) { mobileAccountSender.OnCallHandler -= EndCall; } LogCallEvent("Call crashed.", true); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Call crashed"); Console.WriteLine(); Console.ForegroundColor = ConsoleColor.White; return; } catch (Exception exceptionStandart) { if (mobileAccountSender != null) { mobileAccountSender.OnCallHandler -= EndCall; } LogCallEvent("Call crashed.", true); Console.WriteLine(exceptionStandart.Message); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Call crashed"); Console.WriteLine(); Console.ForegroundColor = ConsoleColor.White; return; } LogCallEvent("Try to call"); mobileAccountReceiver.ReceiveCall(mobileAccountSender.Account.Number); }