public static void Main() { ComputerAbstractFactory factory; PcMotherBoard pc; LaptopMotherBoard laptop; ServerMotherBoard server; var manufacturerName = Console.ReadLine(); if (manufacturerName.Equals("HP")) { factory = new Hp(); } else if (manufacturerName == "Dell") { factory = new Dell(); } else { throw new ArgumentException("Invalid manufacturer!"); } pc = factory.MakePc(); laptop = factory.MakeLaptop(); server = factory.MakeServer(); while (true) { var commandLine = Console.ReadLine(); if (commandLine.StartsWith("Exit")) { ICommand command = CommandSimpleFactory.CreateCommand("Exit", pc); command.Execute(0); command = CommandSimpleFactory.CreateCommand("Exit", laptop); command.Execute(0); command = CommandSimpleFactory.CreateCommand("Exit", server); command.Execute(0); break; } string[] parsedCommand = CommandParser.Parse(commandLine); string commandName = parsedCommand[0]; int commandArgument = int.Parse(parsedCommand[1]); if (commandName == "Charge") { ICommand command = CommandSimpleFactory.CreateCommand(commandName, laptop); } else if (commandName == "Process") { ICommand command = CommandSimpleFactory.CreateCommand(commandName, server); } else if (commandName == "Play") { ICommand command = CommandSimpleFactory.CreateCommand(commandName, pc); } } }
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; }
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); ; } } }
// 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 }