Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.IO;
- public class MyJobs{
- JobReceipt _receipt;
- readonly List<string> _activeJobs = new List<string>();
- readonly List<JobCompletedArgs> _voiceMail = new List<JobCompletedArgs>();
- bool _isPhoneOn = false;
- const string MyPhone = "(248) 719-0682";
- public void StartDay(bool notifyIfNoMessages = true){
- _isPhoneOn = true;
- //Handle messages received when the phone was off
- _voiceMail.ForEach(v=> {
- HandleJobCompleted(null,v);
- });
- bool notify = true;
- StringBuilder messageSummary = new StringBuilder();
- if(_voidMail.Count == 0){
- notify = notifyIfNoMessages;
- .AppendLine("Voice Mail is empty");
- }
- else{
- notify = true;
- int msgNumber = 0;
- foreach(var msg in _voiceMail){
- msgNumber++;
- messageSummary.AppendLine("Message # " + msgNumber + " : " + msg.UserNotes);
- }
- }
- if(notify)
- SendTextMessage(MyPhone,messageSummary.ToString());
- _receipt = Jobs.DropOffJob("Andrew Hodgson",MyPhone);
- if(_receipt==nul)
- return;
- _activeJobs.Add(_receipt.TicketNumber);
- _receipt.JobCompleted+= HandleJobCompleted;
- }
- public void EndDay(){
- _isPhoneOn = false;
- }
- void HandleJobCompleted(object sender,JobCompletedArgs e){
- e.UserNotes = "Recieved at " + DateTime.UtcNow + " Utc";
- _activeJobs.Remove(e.TicketNumber);
- if(_isPhoneOn)
- SendTextMessage(e.CustomerPhone,"Job is completed at " + e.DateActualCompletion.ToString());
- else{
- _voiceMail.Add(e);
- }
- }
- void SendTextMessage(string phone,string message){
- Tuple<string,string> tdata = new Tuple<string,string>.Create(phone,message);
- Thread t = new Thread(new Action((state)=>{}
- //send message async
- Tuple<string,string> msgData = state as Tuple<string,string>;
- string p = msgData.Value1;
- string msg = msgData.Value2;
- ));
- t.IsBackground = true;
- t.TheadName = "MyJobs->SendTextMessage() :" + DateTime.UtcNow;
- t.Start(tdata);
- }
- }
- public static class Jobs {
- static readonly List<JobData> _jobs;
- static Jobs(){
- _jobs = new List<JobData>();
- }
- public static JobReceipt DropOffJob(string customerName, string customerPhone){
- JobData data = new JobData();
- data.CustomerName = customerName;
- data.CustomerPhone = customerPhone;
- data.DateDroppedOff=DateTime.Now;
- data.DateExpectedCompletion = DateTime.Today.AddDays(1).AddHours(15); //5 pm next day
- data.Receipt.DateExpectedCompletion = data.DateExpectedCompletion;
- data.Receipt.TicketNumber = Guid.NewGuid().ToString();
- _jobs.Add(data);
- return data.Receipt;
- }
- }
- public class JobData{
- public string JobName;
- public string CustomerName;
- public string CustomerPhone;
- public DateTime? DateDroppedOff;
- public DateTime? DateExpectedCompletion;
- public DateTime? DateActualCompletion;
- public JobReceipt Receipt;
- public bool SetJobAsCompleted(out string message = null){
- message = null;
- if(DateActualCompletion != null){
- message = "Job already completed";
- return false;
- }
- DateActualCompletion = DateTime.Now;
- Receipt.DateActualCompletion = DateActualCompletion;
- //fire completd event, notifies customer of completed
- Receipt.OnJobCompleted(this);
- }
- public JobData(){
- Receipt = new JobReceipt();
- }
- }
- public class JobReceipt{
- public event JobCompletedHandler JobCompleted;
- public string TicketNumber;
- public DateTime? DateExpectedCompletion;
- public DateTime? DateActualCompletion;
- internal void OnJobCompleted(JobData data){
- if(JobCompleted==null)
- return;
- JobCompleted(this,new JobCompletedArgs(){
- TicketNumber = TicketNumber,
- JobName = data.JobName,
- CustomerName = data.CustomerName,
- CustomerPhone= data.CustomerPhone,
- DateActualCompletion = data.DateActualCompletion,
- });
- }
- }
- public delegate JobCompletedHandler(object sender, JobCompletedArgs e);
- public class JobCompletedArgs:EventArgs{
- public string JobName;
- public string TicketNumber;
- public string CustomerName;
- public string CustomerPhone;
- public DateTime? DateActualCompletion;
- public string UserNotes;
- }
Advertisement
Add Comment
Please, Sign In to add comment