コード例 #1
0
        public Manufacturer CreateManufacturer(string manufacturerName)
        {
            Manufacturer manufacturer;
            if (manufacturerName == "HP")
            {
                manufacturer = new HP();
            }
            else if (manufacturerName == "Dell")
            {
                manufacturer = new Dell();
            }
            else if (manufacturerName == "Lenovo")
            {
                manufacturer = new Lenovo();
            }
            else
            {
                throw new InvalidArgumentException("Invalid manufacturer!");
            }

            return manufacturer;
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: damy90/Telerik-all
        public static void Main()
        {
            IPersonalComputer pc;
            ILaptop laptop;
            IServer server;

            var manufacturerName = Console.ReadLine();
            if (manufacturerName == "HP")
            {
                IManufacturer manufacturer = new HP();

                pc = (IPersonalComputer)manufacturer.Manufacture(ComputerType.PC);

                server = (IServer)manufacturer.Manufacture(ComputerType.Server);

                laptop = (ILaptop)manufacturer.Manufacture(ComputerType.Laptop);
            }
            else if (manufacturerName == "Dell")
            {
                IManufacturer manufacturer = new Dell();

                pc = (IPersonalComputer)manufacturer.Manufacture(ComputerType.PC);
                server = (IServer)manufacturer.Manufacture(ComputerType.Server);
                laptop = (ILaptop)manufacturer.Manufacture(ComputerType.Laptop);
            }
            else if (manufacturerName == "Lenovo")
            {
                IManufacturer manufacturer = new Lenovo();

                pc = (IPersonalComputer)manufacturer.Manufacture(ComputerType.PC);
                server = (IServer)manufacturer.Manufacture(ComputerType.Server);
                laptop = (ILaptop)manufacturer.Manufacture(ComputerType.Laptop);
            }
            else
            {
                throw new InvalidArgumentException("Invalid manufacturer!");
            }

            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 cn = cp[0];
                var ca = int.Parse(cp[1]);

                if (cn == "Charge")
                {
                    laptop.ChargeBattery(ca);
                }
                else if (cn == "Process")
                {
                    server.Process(ca);
                }
                else if (cn == "Play")
                {
                    pc.Play(ca); ;
                }
            }
        }        
コード例 #3
0
        // static Computer pc, laptop, server;
        public static void Main()
        {
            var manufacturerName = Console.ReadLine();
            Manifacturer computerProducer;
            switch (manufacturerName)
            {
                case "HP":
                    computerProducer = new HP();
                    break;
                case "Dell":
                    computerProducer = new Dell();
                    break;
                case "Lenovo":
                    computerProducer = new Lenovo();
                    break;
                default:
                    throw new InvalidArgumentException("Invalid manufacturer!");
            }

            var laptop = computerProducer.ProduceLaptop();
            var personalComputer = computerProducer.ProducePC();
            var server = computerProducer.ProduceServer();

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

                if (command == "Exit")
                {
                    break;
                }

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

                var commandName = cp[0];
                var commandValue = int.Parse(cp[1]);
                if (commandName == "Charge")
                {
                    laptop.Battery.Charge(commandValue);
                    string message = string.Format("Battery status: {0}%", laptop.Battery.PowerLeft);
                    laptop.MotherBoard.DrawOnVideoCard(message);
                }
                else if (commandName == "Process")
                {
                    server.Process(commandValue);
                }
                else if (commandName == "Play")
                {
                    personalComputer.Play(commandValue);
                }
            }

            Console.Read(); // Whait Enter to finish the run
        }