//sets the state of the message depending on the first letter of the header //throws an exception when the header doesnt contain 9 numbers //throws an exception when the header doesnt start with S, T or E public void SetState() { Console.WriteLine(header[0]); if (!header.ValidateNumbers()) { throw new Exception("Header must be in the format S/T/E123456789!"); } if (header[0].ToString().Equals("S")) { currentState = new SMSState(this); } else if (header[0].ToString().Equals("E")) { this.currentState = new EmailState(this); } else if (header[0].ToString().Equals("T")) { this.currentState = new TweetState(this); } else { throw new Exception("Invalid header."); } currentState.ProcessMessage(); }
//chained constructors, pass the actual message public EmailState(MessageState state) : this(state.Message) { }
//chained constructor, set the actual message public TweetState(MessageState state) : this(state.Message) { }
//chained constructor, pass the actual message public SMSState(MessageState state) : this(state.Message) { }