Example #1
0
        private static void Charge(string argument)
        {
            try
            {
                var percentage = int.Parse(argument);

                laptop.ChargeBattery(percentage);
            }
            catch (FormatException)
            {
                Console.WriteLine("Invalid number");
            }
        }
        private static void PreocessCommands()
        {
            // TODO: Command design pattern
            while (true)
            {
                var userInput = Console.ReadLine();
                if (userInput == null)
                {
                    break;
                }

                if (userInput.StartsWith(ExitCommandName))
                {
                    break;
                }

                var commandParts = userInput.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (commandParts.Length != 2)
                {
                    throw new ArgumentException(InvalidCommandMessage);
                }

                // Preocess commands
                var commandName     = commandParts[0];
                var commandArgument = int.Parse(commandParts[1]);

                if (commandName == ChargeCommandName)
                {
                    laptop.ChargeBattery(commandArgument);
                }
                else if (commandName == ProcessCommandName)
                {
                    server.Process(commandArgument);
                }
                else if (commandName == PlayCommandName)
                {
                    pc.Play(commandArgument);
                }
                else
                {
                    Console.WriteLine(InvalidCommandMessage);
                }
            }
        }
        public static void ProcessCommands()
        {
            while (true)
            {
                var c = Console.ReadLine();
                if (c == null)
                {
                    break;
                }

                if (c.StartsWith("Exit"))
                {
                    break;
                }

                var cp = c.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (cp.Length != 2)
                {
                    throw new ArgumentException("Invalid command!");
                }

                var commandName     = cp[0];
                var commandArgument = int.Parse(cp[1]);

                if (commandName == "Charge")
                {
                    laptop.ChargeBattery(commandArgument);
                }
                else if (commandName == "Process")
                {
                    server.Process(commandArgument);
                }
                else if (commandName == "Play")
                {
                    pc.Play(commandArgument);
                }
                else
                {
                    Console.WriteLine("Invalid command!");
                }
            }
        }
Example #4
0
        public static void Main()
        {
            var manufacturer = Console.ReadLine();

            if (manufacturer == "HP")
            {
                pc = new PersonalComputor(new Cpu32(4), new Ram(2), new IHardDriver[] { new HardDriver(500) }, new ColorVideoCard());
                var raid = new List <IHardDriver>
                {
                    new RaidArrays(new List <HardDriver> {
                        new HardDriver(1000), new HardDriver(1000)
                    })
                };
                server = new Server(new Cpu32(2), new Ram(2), raid, new MonochromeVideoCard());
                laptop = new Laptop(new Cpu64(4), new Ram(4), new HardDriver[] { new HardDriver(500) }, new ColorVideoCard(), new LaptopBattery());
            }
            else if (manufacturer == "Dell")
            {
                pc = new PersonalComputor(new Cpu64(4), new Ram(8), new HardDriver[] { new HardDriver(500) }, new ColorVideoCard());
                var raid = new List <IHardDriver>
                {
                    new RaidArrays(new List <HardDriver> {
                        new HardDriver(2000), new HardDriver(2000)
                    })
                };
                server = new Server(new Cpu64(4), new Ram(64), new HardDriver[] { new HardDriver(1000) }, new MonochromeVideoCard());
                laptop = new Laptop(new Cpu32(4), new Ram(8), new HardDriver[] { new HardDriver(1000) }, new ColorVideoCard(), new LaptopBattery());
            }
            else if (manufacturer == "Lenovo")
            {
                pc = new PersonalComputor(new Cpu64(2), new Ram(4), new HardDriver[] { new HardDriver(2000) }, new MonochromeVideoCard());
                var raid = new List <IHardDriver>
                {
                    new RaidArrays(new List <HardDriver> {
                        new HardDriver(500), new HardDriver(500)
                    })
                };
                server = new Server(new Cpu128(2), new Ram(8), new HardDriver[] { new HardDriver(1000) }, new MonochromeVideoCard());
                laptop = new Laptop(new Cpu64(2), new Ram(16), new HardDriver[] { new HardDriver(1000) }, new ColorVideoCard(), new LaptopBattery());
            }
            else
            {
                throw new ArgumentException("Invalid manufacturer!");
            }

            var commandLine = Console.ReadLine();
            var isExit      = false;

            while (!isExit)
            {
                var commands = commandLine.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (commands.Length != 2)
                {
                    throw new ArgumentException("Invalid command!");
                }

                var command = commands[0];
                var value   = int.Parse(commands[1]);

                if (command == "Charge")
                {
                    laptop.ChargeBattery(value);
                }
                else if (command == "Process")
                {
                    server.Process(value);
                }
                else if (command == "Play")
                {
                    pc.Play(value);
                }
                else
                {
                    Console.WriteLine("Invalid command!");
                }

                commandLine = Console.ReadLine();
                isExit      = commandLine == "Exit";
            }
        }
Example #5
0
        public static void Main()
        {
            string manufacturer = Console.ReadLine();

            if (manufacturer == "HP")
            {
                var manufacturerFactory = new HpFactory();
                pc     = manufacturerFactory.CreatePc();
                server = manufacturerFactory.CreateServer();
                laptop = manufacturerFactory.CreateLaptop();
            }
            else if (manufacturer == "Dell")
            {
                var manufacturerFactory = new DellFactory();
                pc     = manufacturerFactory.CreatePc();
                server = manufacturerFactory.CreateServer();
                laptop = manufacturerFactory.CreateLaptop();
            }
            else if (manufacturer == "Lenovo")
            {
                var manufacturerFactory = new LenovoFactory();
                pc     = manufacturerFactory.CreatePc();
                server = manufacturerFactory.CreateServer();
                laptop = manufacturerFactory.CreateLaptop();
            }
            else
            {
                throw new InvalidArgumentException("Invalid manufacturer!");
            }

            while (true)
            {
                var command = Console.ReadLine();
                if (command == null)
                {
                    return;
                }

                if (command.StartsWith("Exit"))
                {
                    return;
                }

                var commandProduce = command.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (commandProduce.Length != 2)
                {
                    {
                        throw new ArgumentException("Invalid command!");
                    }
                }

                var comandName = commandProduce[0];
                var ca         = int.Parse(commandProduce[1]);

                if (comandName == "Charge")
                {
                    laptop.ChargeBattery(ca);
                }
                else if (comandName == "Process")
                {
                    server.Process(ca);
                }
                else if (comandName == "Play")
                {
                    pc.Play(ca);
                }
                else
                {
                    Console.WriteLine("Invalid command!");
                }
            }
        }