andrew4582

MyJobs

Aug 22nd, 2010
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.IO;
  7.  
  8. public class MyJobs{
  9.     JobReceipt _receipt;
  10.     readonly List<string> _activeJobs = new List<string>();
  11.    
  12.     readonly List<JobCompletedArgs> _voiceMail = new List<JobCompletedArgs>();
  13.     bool _isPhoneOn = false;
  14.     const string MyPhone = "(248) 719-0682";
  15.    
  16.     public void StartDay(bool notifyIfNoMessages = true){
  17.         _isPhoneOn  = true;
  18.        
  19.         //Handle messages received when the phone was off
  20.         _voiceMail.ForEach(v=> {
  21.             HandleJobCompleted(null,v);
  22.         });
  23.        
  24.         bool notify = true;
  25.         StringBuilder messageSummary = new StringBuilder();
  26.         if(_voidMail.Count == 0){
  27.             notify = notifyIfNoMessages;
  28.             .AppendLine("Voice Mail is empty");
  29.         }
  30.         else{
  31.             notify = true;
  32.             int msgNumber = 0;
  33.             foreach(var msg in _voiceMail){
  34.                 msgNumber++;
  35.                 messageSummary.AppendLine("Message # " + msgNumber + " : " +  msg.UserNotes);
  36.             }
  37.         }
  38.         if(notify)
  39.             SendTextMessage(MyPhone,messageSummary.ToString());
  40.        
  41.         _receipt = Jobs.DropOffJob("Andrew Hodgson",MyPhone);
  42.         if(_receipt==nul)
  43.             return;
  44.  
  45.         _activeJobs.Add(_receipt.TicketNumber);
  46.        
  47.         _receipt.JobCompleted+= HandleJobCompleted;
  48.     }
  49.     public void EndDay(){
  50.         _isPhoneOn = false;
  51.     }
  52.     void HandleJobCompleted(object sender,JobCompletedArgs e){
  53.         e.UserNotes = "Recieved at " + DateTime.UtcNow + " Utc";
  54.         _activeJobs.Remove(e.TicketNumber);
  55.        
  56.         if(_isPhoneOn)
  57.             SendTextMessage(e.CustomerPhone,"Job is completed at " + e.DateActualCompletion.ToString());
  58.         else{
  59.             _voiceMail.Add(e);
  60.         }
  61.     }
  62.     void SendTextMessage(string phone,string message){
  63.         Tuple<string,string> tdata = new Tuple<string,string>.Create(phone,message);
  64.        
  65.         Thread t = new Thread(new Action((state)=>{}
  66.            
  67.             //send message async
  68.             Tuple<string,string> msgData = state as Tuple<string,string>;
  69.             string p =  msgData.Value1;
  70.             string msg =  msgData.Value2;
  71.            
  72.         ));
  73.         t.IsBackground = true;
  74.         t.TheadName = "MyJobs->SendTextMessage() :" + DateTime.UtcNow;
  75.         t.Start(tdata);
  76.     }
  77. }
  78.  
  79. public static class Jobs {
  80.  
  81.     static readonly List<JobData> _jobs;
  82.    
  83.     static Jobs(){
  84.         _jobs = new List<JobData>();
  85.    
  86.     }
  87.     public static JobReceipt DropOffJob(string customerName, string customerPhone){
  88.        
  89.         JobData data = new JobData();
  90.        
  91.         data.CustomerName = customerName;
  92.         data.CustomerPhone = customerPhone;
  93.        
  94.         data.DateDroppedOff=DateTime.Now;
  95.         data.DateExpectedCompletion = DateTime.Today.AddDays(1).AddHours(15); //5 pm next day
  96.         data.Receipt.DateExpectedCompletion = data.DateExpectedCompletion;
  97.        
  98.         data.Receipt.TicketNumber = Guid.NewGuid().ToString();
  99.         _jobs.Add(data);
  100.        
  101.         return data.Receipt;
  102.     }
  103. }
  104. public class JobData{
  105.     public string JobName;
  106.     public string CustomerName;
  107.     public string CustomerPhone;
  108.     public DateTime? DateDroppedOff;
  109.     public DateTime? DateExpectedCompletion;
  110.     public DateTime? DateActualCompletion;
  111.     public JobReceipt Receipt;
  112.    
  113.     public bool SetJobAsCompleted(out string message = null){
  114.         message = null;
  115.         if(DateActualCompletion != null){
  116.             message = "Job already completed";
  117.             return false;
  118.         }
  119.            
  120.         DateActualCompletion = DateTime.Now;
  121.         Receipt.DateActualCompletion = DateActualCompletion;
  122.        
  123.         //fire completd event, notifies customer of completed
  124.         Receipt.OnJobCompleted(this);
  125.     }
  126.     public JobData(){
  127.         Receipt = new JobReceipt();
  128.     }
  129.    
  130. }
  131. public class JobReceipt{
  132.     public event JobCompletedHandler JobCompleted;
  133.     public string TicketNumber;
  134.     public DateTime? DateExpectedCompletion;
  135.     public DateTime? DateActualCompletion;
  136.    
  137.     internal void OnJobCompleted(JobData data){
  138.         if(JobCompleted==null)
  139.             return;
  140.            
  141.         JobCompleted(this,new JobCompletedArgs(){
  142.             TicketNumber = TicketNumber,
  143.             JobName = data.JobName,
  144.             CustomerName = data.CustomerName,
  145.             CustomerPhone= data.CustomerPhone,
  146.             DateActualCompletion = data.DateActualCompletion,
  147.         });
  148.     }
  149. }
  150. public delegate JobCompletedHandler(object sender, JobCompletedArgs e);
  151. public class JobCompletedArgs:EventArgs{
  152.    
  153.     public string JobName;
  154.     public string TicketNumber;
  155.     public string CustomerName;
  156.     public string CustomerPhone;
  157.     public DateTime? DateActualCompletion;
  158.     public string UserNotes;
  159.    
  160. }
Advertisement
Add Comment
Please, Sign In to add comment