using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace RemoteController { public class RemoteControl { private readonly ICommand[] _onCommands; private readonly ICommand[] _offCommands; private ICommand _undoCommand; private string _name; private const int _slot_count = 7; //could make this variable at later date public int Available_Slots { get { return _slot_count; } } /// /// Constructs two arrays of command objects, which can be executed. One array stores the OnCommands and the second stores the corresponding off Commands /// public RemoteControl(string Name) { _name = Name; _onCommands = new ICommand[_slot_count]; _offCommands = new ICommand[_slot_count]; ICommand noCommand = new NoCommand(); for (int i = 0; i < _slot_count; i++) { _onCommands[i] = noCommand; _offCommands[i] = noCommand; } _undoCommand = noCommand; } public void setCommand(int slot, ICommand onCommand, ICommand offCommand) { _onCommands[slot] = onCommand; _offCommands[slot] = offCommand; } public void onButtonWasPushed(int slot) { _onCommands[slot].execute(); _undoCommand = _onCommands[slot]; } public void offButtonWasPushed(int slot) { _offCommands[slot].execute(); _undoCommand = _offCommands[slot]; } public void undoButtonWasPushed() { _undoCommand.undo(); } public string Name { get { return _name; } } /// /// Count of all command objects stored in the Remote Control which are active Command objects, as opposed to NoCommands objects. /// public int CommandCount { get { return _onCommands.Count(x => !x.GetType().Equals(new NoCommand().GetType())); } } public override string ToString() { var sb = new StringBuilder(); sb.AppendLine("---------------RemoteControl---------------"); sb.AppendLine("-" + _name + "-"); for (int i = 0; i < _onCommands.Length; i++) { sb.AppendLine("slot: [" + i + "] " +_onCommands[i].GetType().Name + " " + _offCommands[i].GetType().Name); } return sb.ToString(); } } }