public static void Main() { try { List<Computer> computers = new List<Computer>(); Computer acer = new Computer( "Acer Aspire", new Component("Processor", 350.36m, "Intel i7"), new Component("Ram", 105.25m, "8GB DDR3")); acer.AddComponents(new Component("Hdd", 200.00M), new Component("Monitor", 300.0m, "LG 13 Wide")); Computer toshiba = new Computer( "Toshiba Satellite", new Component("Graphic card", 420.16m, "Ati Radeon R9 1GB"), new Component("Motherboard", 160.00m)); computers.Add(acer); computers.Add(toshiba); computers = computers.OrderBy(computer => computer.TotalPrice).ToList(); foreach (Computer computer in computers) { Console.WriteLine("*************************\n" + computer); } } catch (Exception ex) { Console.WriteLine(ex); } }
static void Main() { var pc = new Computer(); pc.Name = "My PC"; var ram = new Component("RAM", 150.5m); var cpu = new Component("Processor", 259.99m); pc.Components = new List<Component>(); pc.Components.Add(ram); pc.Components.Add(cpu); pc.Components.Add(new Component("Graphics", 95.66m)); ram.Details = "8 GB DDR3, KINGSTON"; cpu.Details = "Intel CoreĀ® i5"; var pc1 = new Computer("Gosho's PC"); var pc2 = new Computer("Pesho's PC"); var pc3 = new Computer("Vlado's PC"); pc1.Components = new List<Component>(); pc2.Components = new List<Component>(); pc3.Components = new List<Component>(); pc1.Components.Add(new Component("RAM", 167.65m, "6 GB DDR3, KINGSTON")); pc1.Components.Add(new Component("Graphics", 252m, "512 MB")); pc1.Components.Add(new Component("CPU", 199.99m, "AMD Athlon 64 x2, 2.11 GHz")); pc1.Components.Add(new Component("HDD", 68m, "Hitachi")); pc2.Components.Add(new Component("RAM", 100m, "4 GB DDR3, KINGSTON")); pc2.Components.Add(new Component("Graphics", 212.35m, "1GB")); pc2.Components.Add(new Component("HDD", 188m, "500 RPM")); pc3.Components.Add(new Component("RAM", 129.65m, "4 GB DDR3")); pc3.Components.Add(new Component("HDD", 212.35m, "1GB")); pc3.Components.Add(new Component("Processor", 250m, "Intel Core 2 Duo, 2.8 GHz")); var computers = new List<Computer>(); computers.Add(pc); computers.Add(pc1); computers.Add(pc2); computers.Add(pc3); var sortedComputers = computers .OrderBy(x => x.TotalComponentsPrice(x.Components)); foreach (var computer in sortedComputers) { computer.PrintInfo(); Console.WriteLine(); } }
static void Main() { // valid data Computer pc1 = new Computer( "Dell Precision T3400 Workstation", "Intel Core2Quad E6600", 100m, "Dell Intel Motherboard", 50m, "nVidia GeForce 8600 GT", 50m); Computer pc2 = new Computer( "Assembled", "AMD Athlon X2 64 4400+", 30m, "2 MB cache, not overclocked", "ASUS M2N-E", 30m, "nVidia nForce 570 Chipset, 10xUSB 2.0, 1xPCIe x16, 4xPCI", "nVidia Quadro FX 1700", 30m, "Professional Graphics Card"); Computer pc6 = new Computer( "ASUS G20AJ-BG002S", "INTEL CORE i7-4790", 1000m, "3.6 GHz 8MB", "Unknown Motherboard", 1000m, "INTEL H97 CHIPSET", "NVIDIA GeForce GTX970", 1000m, "4GB DDR5" ); List<Computer> compList = new List<Computer> { pc1, pc2, pc6 }; var sortCompsByPrice = compList.OrderByDescending(x => x.Price); foreach (var comp in sortCompsByPrice) { comp.PrintConfigDetails(); } // invalid data // try editing the line below - until you've defined a name for the computer and all of its components + the prices of the components are > 0 the line below will throw the respective exceptions //Computer pc3 = new Computer("", "", -5, "", -5, "", -5); //pc3.PrintConfigDetails(); }
static void Main(string[] args) { List<Computer> comps = new List<Computer>(); Computer comp = new Computer("HP 450 G2 ProBook", new Component("1 TB", 200.9M), new Component("Intel Core i7-4510U", "2.00 - 3.10 GHz, 4MB cache", 350.12M), 1100); Computer comp2 = new Computer("HP 450 G2 ProBook", 1254.54M); comps.Add(comp); comps.Add(comp2); comps.OrderBy(p => p.Price); foreach (Computer c in comps) { Console.WriteLine(c); } }
static void Main(string[] args) { List<Computer> catalog = new List<Computer>(); List <Component> componentsList1 = new List<Component>(); componentsList1.Add(new Component("Motherboard", 90)); componentsList1.Add(new Component("CPU", 120.45m)); componentsList1.Add(new Component("RAM", 45.50m, "8 GB")); Computer pc = new Computer("HP", componentsList1); Console.WriteLine(pc); pc.AddComponent(new Component("added later", 126)); Console.WriteLine(pc); catalog.Add(pc); var componentsList2 = new List<Component>(); componentsList2.Add(new Component("DVD", 15.99m)); componentsList2.Add(new Component("GPU", 255.1m)); catalog.Add(new Computer("Cheap", componentsList2)); var componentsList3 = new List<Component>(); componentsList3.Add(new Component("RAM", 52.19m)); componentsList3.Add(new Component("SSD", 550)); catalog.Add(new Computer("Average", componentsList3)); var componentsList4 = new List<Component>(); componentsList4.Add(new Component("GPU", 1125.5m)); componentsList4.Add(new Component("CPU", 900)); catalog.Add(new Computer("Expensive", componentsList4)); catalog.Sort(); // IComparable interface implemented on Computer class Console.WriteLine("\r\n" + new string('-', 50) + "\r\n" + "COMPUTER CATALOG"); foreach (var computer in catalog) { Console.WriteLine(computer); } }
public static decimal CalculatePrice(Computer computer) { var components = computer.Components; return components.Sum(component => component.Price); }