Exemple #1
0
        private void RegisterComponent(string component)
        {
            int    startIndexOfComponentArguments = component.IndexOf('(');
            string typeComponent = component.Substring(0, startIndexOfComponentArguments);

            string[] componentArguments = SplitByMultipleChars(
                component.Substring(startIndexOfComponentArguments));

            if (typeComponent.Contains("Hardware"))
            {
                IComponentHardware newHardware = hardwareFactory.CreateHardware(
                    typeComponent, componentArguments[0],
                    int.Parse(componentArguments[1]),
                    int.Parse(componentArguments[2]));

                this.newMachine.AddHardwareItem(newHardware);
            }
            else
            {
                IComponentSoftware newSoftware = softwareFactory.CreateSoftware(typeComponent,
                                                                                componentArguments[1],
                                                                                int.Parse(componentArguments[2]),
                                                                                int.Parse(componentArguments[3]));

                this.newMachine.AddSoftwareItem(componentArguments[0], newSoftware);
            }
        }
Exemple #2
0
    private static void AddSoftware(List <HardwareComponent> hardware, List <string> input)
    {
        SoftwareComponent currentSoftwareComponent = SoftwareFactory.CreateSoftware(input);

        if (hardware.Any(h => h.Name == currentSoftwareComponent.HardwareComponent))
        {
            var currenthardComponent = hardware.First(h => h.Name == currentSoftwareComponent.HardwareComponent);
            if (currenthardComponent.CanTakeSoftware(currentSoftwareComponent))
            {
                currenthardComponent.AddSoftwareComponent(currentSoftwareComponent);
            }
        }
    }
 public void RegisterExpressSoftware(string hardwareComponentName, string name, int capacity, int memory)
 {
     system.AddSoftware(hardwareComponentName, createSoftware.CreateSoftware("Express", name, capacity, memory));
 }
    public void Execute()
    {
        while (true)
        {
            string[] commandArgs = GetCommand();

            string command = commandArgs[0];
            switch (command)
            {
            case "RegisterPowerHardware":
                Hardware hardware = HardwareFactory.CreateHardware("Power", commandArgs.Skip(1).ToArray());

                this.hardwares.Add(hardware);
                break;

            case "RegisterHeavyHardware":
                hardware = HardwareFactory.CreateHardware("Heavy", commandArgs.Skip(1).ToArray());

                this.hardwares.Add(hardware);
                break;

            case "RegisterExpressSoftware":
                string hardwareComponentName = commandArgs[1];

                try
                {
                    hardware = hardwares.FirstOrDefault(h => h.Name == hardwareComponentName);

                    Software software = SoftwareFactory.CreateSoftware("Express", commandArgs.Skip(2).ToArray());

                    hardware.AddSoftwareComponent(software);
                    softwares.Add(software);
                }
                catch (Exception)
                {
                    continue;
                }
                break;

            case "RegisterLightSoftware":
                hardwareComponentName = commandArgs[1];

                try
                {
                    hardware = hardwares.FirstOrDefault(h => h.Name == hardwareComponentName);

                    Software software = SoftwareFactory.CreateSoftware("Light", commandArgs.Skip(2).ToArray());

                    hardware.AddSoftwareComponent(software);
                    softwares.Add(software);
                }
                catch (Exception)
                {
                    continue;
                }
                break;

            case "ReleaseSoftwareComponent":
                hardwareComponentName = commandArgs[1];
                string softwareComponentName = commandArgs[2];

                try
                {
                    hardware = hardwares.FirstOrDefault(h => h.Name == hardwareComponentName);
                    Software software = hardware.ReleaseSoftwareComponent(softwareComponentName);
                    softwares.Remove(software);
                }
                catch (Exception)
                {
                    continue;
                }
                break;

            case "Dump":
                hardwareComponentName = commandArgs[1];

                if (!hardwares.Exists(h => h.Name == hardwareComponentName))
                {
                    continue;
                }
                try
                {
                    hardware = hardwares.FirstOrDefault(h => h.Name == hardwareComponentName);

                    hardwares.Remove(hardware);

                    dump.Add(hardware);
                }
                catch (Exception)
                {
                    continue;
                }
                break;

            case "Restore":
                hardwareComponentName = commandArgs[1];

                if (!dump.Exists(h => h.Name == hardwareComponentName))
                {
                    continue;
                }

                try
                {
                    hardware = dump.FirstOrDefault(h => h.Name == hardwareComponentName);

                    dump.Remove(hardware);

                    hardwares.Add(hardware);
                }
                catch (Exception)
                {
                    continue;
                }
                break;

            case "Destroy":
                hardwareComponentName = commandArgs[1];

                if (!hardwares.Exists(h => h.Name == hardwareComponentName))
                {
                    continue;
                }

                try
                {
                    hardware = dump.FirstOrDefault(h => h.Name == hardwareComponentName);

                    dump.Remove(hardware);
                }
                catch (Exception)
                {
                    continue;
                }
                break;

            case "DumpAnalyze":
                StringBuilder sb = new StringBuilder();

                sb.Append($"Dump Analysis{Environment.NewLine}");

                int countOfPowerHardwareComponents   = dump.Where(c => c.GetType().Name == "Power").Count();
                int countOfHeavyHardwareComponents   = dump.Where(c => c.GetType().Name == "Heavy").Count();
                int countOfExpressSoftwareComponents = dump.Sum(c => c.Softwares.Where(s => s.GetType().Name == "Express").Count());
                int countOfLightSoftwareComponents   = dump.Sum(c => c.Softwares.Where(s => s.GetType().Name == "Light").Count());;
                int totalDumpedMemory   = dump.Sum(c => c.TotalOperationalMemoryInUse);
                int totalDumpedCapacity = dump.Sum(c => c.TotalCapacityTaken);

                sb.Append($"Power Hardware Components: {countOfPowerHardwareComponents}{Environment.NewLine}");
                sb.Append($"Heavy Hardware Components: {countOfHeavyHardwareComponents}{Environment.NewLine}");
                sb.Append($"Express Software Components: {countOfExpressSoftwareComponents}{Environment.NewLine}");
                sb.Append($"Light Software Components: {countOfLightSoftwareComponents}{Environment.NewLine}");
                sb.Append($"Total Dumped Memory: {totalDumpedMemory}{Environment.NewLine}");
                sb.Append($"Total Dumped Capacity: {totalDumpedCapacity}{Environment.NewLine}");

                Console.Write(sb.ToString());
                break;

            case "Analyze":
                sb = new StringBuilder();

                sb.Append($"System Analysis{Environment.NewLine}");

                sb.Append($"Hardware Components: {hardwares.Count}{Environment.NewLine}");
                sb.Append($"Software Components: {hardwares.Sum(s => s.Softwares.Count)}{Environment.NewLine}");

                int TotalOperationalMemoryInUse = hardwares.Sum(h => h.TotalOperationalMemoryInUse);
                int TotalMemory = hardwares.Sum(h => h.MaximumMemory);
                sb.Append($"Total Operational Memory: {TotalOperationalMemoryInUse} / {TotalMemory}{Environment.NewLine}");

                int TotalCapacityTaken = hardwares.Sum(h => h.TotalCapacityTaken);
                int TotalCapacity      = hardwares.Sum(h => h.MaximumCapacity);
                sb.Append($"Total Capacity Taken: {TotalCapacityTaken} / {TotalCapacity}");
                Console.WriteLine(sb.ToString());
                break;

            case "System":
                Console.Write(SystemInformation());
                return;
            }
        }
    }