static void Main(string[] args) { MobileOperator mo = new MobileOperator(); MobileAccount acc1 = new MobileAccount(mo, 123); mo.NewCall += acc1.HandleCall; mo.NewSms += acc1.HandleSms; MobileAccount acc2 = new MobileAccount(mo, 345); mo.NewCall += acc2.HandleCall; mo.NewSms += acc2.HandleSms; acc1.SendSms(345, "hello world"); Console.ReadKey(); }
static void Main(string[] args) { MobileOperator mo = new MobileOperator(); MobileAccount acc1 = new MobileAccount(mo, 123); mo.NewCall += acc1.HandleCall; mo.NewSms += acc1.HandleSms; MobileAccount acc2 = new MobileAccount(mo, 345); mo.NewCall += acc2.HandleCall; mo.NewSms += acc2.HandleSms; acc2.AddressBook.Add(123, "Peter Parker"); acc1.SendSms(345, "Hello Peter!"); Console.ReadKey(); }
static void Main(string[] args) { MobileOperator mo = new MobileOperator(); MobileAccount acc1 = new MobileAccount(mo, 123); mo.NewCall += acc1.HandleCall; mo.NewSms += acc1.HandleSms; mo.NewNotif += acc1.HandleNotification; MobileAccount acc2 = new MobileAccount(mo, 345); mo.NewCall += acc2.HandleCall; mo.NewSms += acc2.HandleSms; mo.NewNotif += acc2.HandleNotification; acc1.SendSms(345, "hello world"); // send notification to mobileAccount mo.BroadCastNotification(123, new OperatorNotification("You are warned!")); Console.ReadKey(); }
static void Main(string[] args) { MobileOperator mo = new MobileOperator(); MobileAccount acc1 = new MobileAccount(mo, 123); mo.NewCall += acc1.HandleCall; mo.NewSms += acc1.HandleSms; MobileAccount acc2 = new MobileAccount(mo, 345); mo.NewCall += acc2.HandleCall; mo.NewSms += acc2.HandleSms; acc2.AddressBook.Add(123, "Peter Parker"); acc2.AddressBook.Add(874, "Alan Turing"); acc2.AddressBook.Add(748, "Paul Allan"); acc2.AddressBook.Add(838, "John Doe"); acc2.AddressBook.Add(883, "Niels Bohr"); acc2.AddressBook.Add(432, "Albert Einstein"); acc1.MakeCall(345); acc1.MakeCall(432); acc2.MakeCall(874); acc2.MakeCall(432); acc2.MakeCall(432); acc1.MakeCall(345); acc1.MakeCall(911); acc1.MakeCall(345); acc1.MakeCall(911); acc1.MakeCall(911); acc2.MakeCall(911); acc2.MakeCall(983); acc2.MakeCall(763); acc2.SendSms(6543, "Empty1"); acc2.SendSms(748, "Empty2"); acc1.SendSms(883, "Empty3"); var d = from record in acc2.AddressBook where record.Value.Length > 9 orderby record.Key ascending select record; Console.WriteLine("Address book filtering:"); foreach (var rec in d) { Console.WriteLine("{0} {1}", rec.Key, rec.Value); } Console.WriteLine("\n5 most called numbers:"); var result = from r in mo.Log where r.EventType == MobileOperator.LogItem.Tevent.Call group r by r.ToNumber into g orderby g.Count() descending select new { Num = g.Key, Count = g.Count() }; foreach (var rec in result) { Console.WriteLine("Number: {0} Count: {1} ", rec.Num, rec.Count); } Console.WriteLine("\n5 most active numbers:"); var callTable = from r in mo.Log where r.EventType == MobileOperator.LogItem.Tevent.Call group r by r.FromNumber into g orderby g.Count() descending select new { Num = g.Key, CallCount = g.Count() }; var smsTable = from r in mo.Log where r.EventType == MobileOperator.LogItem.Tevent.Sms group r by r.FromNumber into g orderby g.Count() descending select new { Num = g.Key, SmsCount = g.Count() }; var rating = from c in callTable join s in smsTable on c.Num equals s.Num select new { Num = s.Num, Rate = c.CallCount * 2 + s.SmsCount }; rating = from r in rating orderby r.Rate descending select r; foreach (var rec in rating) { Console.WriteLine("Number: {0} Rating: {1} ", rec.Num, rec.Rate); } Console.ReadKey(); }
public MobileAccount(MobileOperator mo, int number, decimal balance) { MobOp = mo; Number = number; Balance = balance; }
public MobileAccount(MobileOperator mo, int number) { MobOp = mo; Number = number; }
static void Main(string[] args) { MobileOperator mo = new MobileOperator(); // generate a list of random accounts List <MobileAccount> accounts = new List <MobileAccount>(); const int numOfAcc = 100; const int maxNumberValue = 1000000; const int maxAddrBookSize = 30; const int maxPhoneCallNum = 20; Random r = new Random(); var watch = System.Diagnostics.Stopwatch.StartNew(); for (int accNum = 0; accNum < numOfAcc; accNum++) { MobileAccount newMobAcc = new MobileAccount(mo, r.Next(0, maxNumberValue)); for (int i = 0; i < r.Next(maxAddrBookSize); i++) { newMobAcc.AddressBook.Add(r.Next(maxNumberValue), "DefName" + r.Next(maxNumberValue).ToString()); } for (int j = 0; j < maxPhoneCallNum; j++) { newMobAcc.MakeCall(r.Next(maxNumberValue)); } accounts.Add(newMobAcc); } watch.Stop(); Console.WriteLine("Initialization: {0} ms", watch.ElapsedMilliseconds); long jsonTime = 0; long xmlTime = 0; long binTime = 0; long protoTime = 0; for (int iter = 0; iter < 5; iter++) { Console.WriteLine("Iteration № {0}", iter + 1); watch = System.Diagnostics.Stopwatch.StartNew(); // JSON serialization DataContractJsonSerializer jsonFormatter = new DataContractJsonSerializer(typeof(List <MobileAccount>)); using (FileStream fs = new FileStream("accounts.json", FileMode.Create)) { jsonFormatter.WriteObject(fs, accounts); } watch.Stop(); Console.WriteLine(" JSON serialization: {0} ms", watch.ElapsedMilliseconds); jsonTime += watch.ElapsedMilliseconds; watch = System.Diagnostics.Stopwatch.StartNew(); // XML serialization var serializer = new DataContractSerializer(typeof(List <MobileAccount>)); using (FileStream fs = new FileStream("accounts.xml", FileMode.Create)) { serializer.WriteObject(fs, accounts); } watch.Stop(); Console.WriteLine(" XML serialization: {0} ms", watch.ElapsedMilliseconds); xmlTime += watch.ElapsedMilliseconds; watch = System.Diagnostics.Stopwatch.StartNew(); // Binary serialization BinaryFormatter formatter = new BinaryFormatter(); using (FileStream fs = new FileStream("accounts.bin", FileMode.Create)) { formatter.Serialize(fs, accounts); } watch.Stop(); Console.WriteLine(" Binary serialization: {0} ms", watch.ElapsedMilliseconds); binTime += watch.ElapsedMilliseconds; watch = System.Diagnostics.Stopwatch.StartNew(); // ProtoBuf using (FileStream fs = new FileStream("accounts.proto", FileMode.Create)) { Serializer.Serialize(fs, accounts); } watch.Stop(); Console.WriteLine(" ProtoBuf serialization: {0} ms", watch.ElapsedMilliseconds); protoTime += watch.ElapsedMilliseconds; Console.WriteLine(); } Console.WriteLine("Total time: \n JSON: {0} ms \n XML: {1} ms \n binary: {2} ms \n proto: {3} ms", jsonTime, xmlTime, binTime, protoTime); Console.ReadKey(); }