Пример #1
0
        public void Start()
        {
            while (true)
            {
                Console.Clear();
                if (devices.Count == 0)
                {
                    Console.WriteLine("You have no devices yet");
                }
                else
                {
                    foreach (var device in devices)
                    {
                        Console.WriteLine("Name: " + device.Key + "\n" + device.Value);
                    }
                }
                Console.WriteLine();
                Console.WriteLine("Type in the command or press <Enter> for help: ");
                string[] commands = Console.ReadLine().ToLower().Split(' ');

                if (commands.Length != 3 && commands.Length != 2 & commands.Length != 1)
                {
                    Help();
                    continue;
                }

                switch (commands[0])
                {
                    case "add":
                        if (commands.Length != 3)
                        {
                            Help();
                            continue;
                        }
                        if (!devices.ContainsKey(commands[2]))
                        {
                            if (commands[1] == "fridge")
                            {
                                Fridge f = new Fridge();
                                devices.Add(commands[2], f);

                            }
                            else if (commands[1] == "garage")
                            {
                                Garage g = new Garage();
                                devices.Add(commands[2], g);

                            }
                            else if (commands[1] == "camera")
                            {
                                Camera c = new Camera();
                                devices.Add(commands[2], c);

                            }
                            else if (commands[1] == "airconditioner")
                            {
                                AirConditioner a = new AirConditioner();
                                devices.Add(commands[2], a);

                            }
                            else if (commands[1] == "tv")
                            {
                                TV t = new TV();
                                devices.Add(commands[2], t);

                            }

                            else if (commands[1] == "panasonicstereo")
                            {
                                PanasonicStereoSystem p = new PanasonicStereoSystem(new PanasonicLoudspeakers());
                                devices.Add(commands[2], p);

                            }

                            else if (commands[1] == "panasonicspeakers")
                            {
                                PanasonicLoudspeakers pl = new PanasonicLoudspeakers();
                                devices.Add(commands[2], pl);

                            }
                            else
                            {
                                Console.WriteLine(commands[1] + " is not a device. " +
                                                  "Press <Enter> to continue");
                                Console.ReadLine();

                                Help();
                                continue;
                            }
                        }
                        else
                        {
                            Console.WriteLine("Device with name \'{0}\' already exists. " +
                                              "Press <Enter> to continue", commands[2]);
                            Console.ReadLine();

                        }
                        break;
                    case "del":
                        if (commands.Length != 2)
                        {
                            Help();
                            continue;
                        }
                        if (commands[1] == "all")
                        {
                            devices.Clear();
                            Console.WriteLine("All devices was removed");
                        }
                        if (isValid(commands[1]) && commands[1] != "all")
                        {
                            devices.Remove(commands[1]);
                        }
                        break;
                    case "on":
                        if (commands.Length != 2)
                        {
                            Help();
                            continue;
                        }
                        if (commands[1] == "all")
                        {
                            foreach (var device in devices)
                            {
                                device.Value.On();
                            }
                        }
                        if (isValid(commands[1]) && commands[1] != "all")
                        {
                            devices[commands[1]].On();
                        }
                        break;
                    case "off":
                        if (commands.Length != 2)
                        {
                            Help();
                            continue;
                        }
                        if (commands[1] == "all")
                        {
                            foreach (var device in devices)
                            {
                                device.Value.Off();
                            }
                        }
                        if (isValid(commands[1]) && commands[1] != "all")
                        {
                            devices[commands[1]].On();
                        }
                        break;
                    case "open":
                        if (commands.Length != 2)
                        {
                            Help();
                            continue;
                        }
                        if (commands[1] == "all")
                        {
                            foreach (var device in devices)
                            {
                                if (device.Value is IOpenable)
                                {
                                    (device.Value as IOpenable).Open();
                                }
                            }
                        }
                        if (isValid(commands[1]) && commands[1] != "all")
                        {
                            if (devices[commands[1]] is IOpenable)
                            {
                                (devices[commands[1]] as IOpenable).Open();
                            }
                            else
                            {
                                Console.WriteLine("You can't apply command \'open\' to {0} {1}. " +
                                                  "Press <Enter> to continue", devices[commands[1]].GetType().Name, commands[1]);
                                Console.ReadLine();

                            }
                        }
                        break;
                    case "close":
                        if (commands.Length != 2)
                        {
                            Help();
                            continue;
                        }
                        if (commands[1] == "all")
                        {
                            foreach (var device in devices)
                            {
                                if (device.Value is IOpenable)
                                {
                                    (device.Value as IOpenable).Close();
                                }
                            }
                        }
                        if (isValid(commands[1]) && commands[1] != "all")
                        {
                            if (devices[commands[1]] is IOpenable)
                            {
                                (devices[commands[1]] as IOpenable).Close();
                            }
                            else
                            {
                                Console.WriteLine("You can't apply command \'close\' to {0} {1}. " +
                                                  "Press <Enter> to continue", devices[commands[1]].GetType().Name, commands[1]);
                                Console.ReadLine();

                            }
                        }
                        break;
                    case "addvolume":
                        if (commands.Length != 2)
                        {
                            Help();
                            continue;
                        }
                        if (commands[1] == "all")
                        {
                            foreach (var device in devices)
                            {
                                if (device.Value is IVolumeable)
                                {
                                    (device.Value as IVolumeable).AddVolume();
                                }
                            }
                        }
                        if (isValid(commands[1]) && commands[1] != "all")
                        {
                            if (devices[commands[1]] is IVolumeable)
                            {
                                (devices[commands[1]] as IVolumeable).AddVolume();
                            }
                            else
                            {
                                Console.WriteLine("You can't apply command \'addvolume\' to {0} {1}. " +
                                                  "Press <Enter> to continue", devices[commands[1]].GetType().Name, commands[1]);
                                Console.ReadLine();

                            }
                        }
                        break;
                    case "decvolume":
                        if (commands.Length != 2)
                        {
                            Help();
                            continue;
                        }
                        if (commands[1] == "all")
                        {
                            foreach (var device in devices)
                            {
                                if (device.Value is IVolumeable)
                                {
                                    (device.Value as IVolumeable).DecreaseVolume();
                                }
                            }
                        }
                        if (isValid(commands[1]) && commands[1] != "all")
                        {
                            if (devices[commands[1]] is IVolumeable)
                            {
                                (devices[commands[1]] as IVolumeable).DecreaseVolume();
                            }
                            else
                            {
                                Console.WriteLine("You can't apply command \'decvolume\' to {0} {1}. " +
                                                  "Press <Enter> to continue", devices[commands[1]].GetType().Name, commands[1]);
                                Console.ReadLine();

                            }
                        }
                        break;
                    case "mute":
                        if (commands.Length != 2)
                        {
                            Help();
                            continue;
                        }
                        if (commands[1] == "all")
                        {
                            foreach (var device in devices)
                            {
                                if (device.Value is IVolumeable)
                                {
                                    (device.Value as IVolumeable).Mute();
                                }
                            }
                        }
                        if (isValid(commands[1]) && commands[1] != "all")
                        {
                            if (devices[commands[1]] is IVolumeable)
                            {
                                (devices[commands[1]] as IVolumeable).Mute();
                            }
                            else
                            {
                                Console.WriteLine("You can't apply command \'mute\' to {0} {1}. " +
                                                  "Press <Enter> to continue", devices[commands[1]].GetType().Name, commands[1]);
                                Console.ReadLine();

                            }
                        }
                        break;
                    case "startrec":
                        if (commands.Length != 2)
                        {
                            Help();
                            continue;
                        }
                        if (commands[1] == "all")
                        {
                            foreach (var device in devices)
                            {
                                if (device.Value is IRecording)
                                {
                                    (device.Value as IRecording).StartRecording();
                                }
                            }
                        }
                        if (isValid(commands[1]) && commands[1] != "all")
                        {
                            if (devices[commands[1]] is IRecording)
                            {
                                (devices[commands[1]] as IRecording).StartRecording();
                            }
                            else
                            {
                                Console.WriteLine("You can't apply command \'startrec\' to {0} {1}. " +
                                                  "Press <Enter> to continue", devices[commands[1]].GetType().Name, commands[1]);
                                Console.ReadLine();

                            }
                        }
                        break;
                    case "stoprec":
                        if (commands.Length != 2)
                        {
                            Help();
                            continue;
                        }
                        if (commands[1] == "all")
                        {
                            foreach (var device in devices)
                            {
                                if (device.Value is IRecording)
                                {
                                    (device.Value as IRecording).StopRecording();
                                }
                            }
                        }
                        if (isValid(commands[1]) && commands[1] != "all")
                        {
                            if (devices[commands[1]] is IRecording)
                            {
                                (devices[commands[1]] as IRecording).StopRecording();
                            }
                            else
                            {
                                Console.WriteLine("You can't apply command \'stoprec\' to {0} {1}. " +
                                                  "Press <Enter> to continue", devices[commands[1]].GetType().Name, commands[1]);
                                Console.ReadLine();

                            }
                        }
                        break;
                    case "addtemp":
                        if (commands.Length != 2)
                        {
                            Help();
                            continue;
                        }
                        if (commands[1] == "all")
                        {
                            foreach (var device in devices)
                            {
                                if (device.Value is ITemperature)
                                {
                                    (device.Value as ITemperature).AddTemperture();
                                }
                            }
                        }
                        if (isValid(commands[1]) && commands[1] != "all")
                        {
                            if (devices[commands[1]] is ITemperature)
                            {
                                (devices[commands[1]] as ITemperature).AddTemperture();
                            }
                            else
                            {
                                Console.WriteLine("You can't apply command \'addtemp\' to {0} {1}. " +
                                                  "Press <Enter> to continue", devices[commands[1]].GetType().Name, commands[1]);
                                Console.ReadLine();

                            }
                        }
                        break;
                    case "dectemp":
                        if (commands.Length != 2)
                        {
                            Help();
                            continue;
                        }
                        if (commands[1] == "all")
                        {
                            foreach (var device in devices)
                            {
                                if (device.Value is ITemperature)
                                {
                                    (device.Value as ITemperature).DecreaseTemperature();
                                }
                            }
                        }
                        if (isValid(commands[1]) && commands[1] != "all")
                        {
                            if (devices[commands[1]] is ITemperature)
                            {
                                (devices[commands[1]] as ITemperature).DecreaseTemperature();
                            }
                            else
                            {
                                Console.WriteLine("You can't apply command \'dectemp\' to {0} {1}. " +
                                                  "Press <Enter> to continue", devices[commands[1]].GetType().Name, commands[1]);
                                Console.ReadLine();

                            }
                        }
                        break;
                    case "addbass":
                        if (commands.Length != 2)
                        {
                            Help();
                            continue;
                        }
                        if (commands[1] == "all")
                        {
                            foreach (var device in devices)
                            {
                                if (device.Value is IBass)
                                {
                                    (device.Value as IBass).BassOn();
                                }
                            }
                        }
                        if (isValid(commands[1]) && commands[1] != "all")
                        {
                            if (devices[commands[1]] is IBass)
                            {
                                (devices[commands[1]] as IBass).BassOn();
                            }
                            else
                            {
                                Console.WriteLine("You can't apply command \'addbass\' to {0} {1}. " +
                                                  "Press <Enter> to continue", devices[commands[1]].GetType().Name, commands[1]);
                                Console.ReadLine();

                            }
                        }
                        break;
                    case "delbass":
                        if (commands.Length != 2)
                        {
                            Help();
                            continue;
                        }
                        if (commands[1] == "all")
                        {
                            foreach (var device in devices)
                            {
                                if (device.Value is IBass)
                                {
                                    (device.Value as IBass).BassOff();
                                }
                            }
                        }
                        if (isValid(commands[1]) && commands[1] != "all")
                        {
                            if (devices[commands[1]] is IBass)
                            {
                                (devices[commands[1]] as IBass).BassOff();
                            }
                            else
                            {
                                Console.WriteLine("You can't apply command \'delbass\' to {0} {1}. " +
                                                  "Press <Enter> to continue", devices[commands[1]].GetType().Name, commands[1]);
                                Console.ReadLine();

                            }
                        }
                        break;
                    case "exit":
                        if (commands.Length != 1)
                        {
                            Help();
                        }
                        return;
                    default:
                        Help();
                        continue;
                        break;
                }
            }
        }
 public PanasonicStereoSystem(PanasonicLoudspeakers p)
     : base(p)
 {
 }