public async Task <ActionResult <ComputerModel> > PutComputer(Guid id, ComputerModel computer)
        {
            if (id != computer.Id)
            {
                return(BadRequest());
            }

            var existingComputer = await _computerService.Get(id);

            if (existingComputer == null)
            {
                return(NotFound());
            }

            ComputerModel updatedComputer;

            switch (existingComputer.TypeName)
            {
            case "Desktop":
                PCComputer pcComputerToUpdate = computer as PCComputer;
                pcComputerToUpdate = new PCComputer {
                    Id            = pcComputerToUpdate.Id,
                    Brand         = pcComputerToUpdate.Brand,
                    ProcessorName = pcComputerToUpdate.ProcessorName,
                    Quantity      = pcComputerToUpdate.Quantity,
                    RamSlots      = pcComputerToUpdate.RamSlots,
                    UsbPorts      = pcComputerToUpdate.UsbPorts
                };
                updatedComputer = pcComputerToUpdate;
                break;

            case "Laptop":
                LaptopComputer lpComputerToUpdate = computer as LaptopComputer;
                lpComputerToUpdate = new LaptopComputer {
                    Id            = lpComputerToUpdate.Id,
                    Brand         = lpComputerToUpdate.Brand,
                    ProcessorName = lpComputerToUpdate.ProcessorName,
                    ScreenSize    = lpComputerToUpdate.ScreenSize
                };
                updatedComputer = lpComputerToUpdate;
                break;

            default:
                return(BadRequest());
            }

            await _computerService.Update(updatedComputer);

            return(existingComputer);
        }
Exemplo n.º 2
0
 public override ILaptopComputer MakeLaptopComputer()
 {
     IVideoCard videoCard = new ColorVideoCard();
     Ram ram = new Ram(4);
     IMotherboard motherboard = new Motherboard(ram, videoCard);
     Cpu cpu = new Cpu64Bit(2, motherboard, this.Random);
     var hardDrive = new[]
     {
         new HardDrive(500, false, 0)
     };
     Battery.LaptopBattery battery = new Battery.LaptopBattery();
     ILaptopComputer laptop = new LaptopComputer(motherboard, cpu, ram, hardDrive, videoCard, battery);
     return laptop;
 }
        public async Task <ActionResult <ComputerModel> > PostComputer(GenericComputer computer)
        {
            if (computer == null)
            {
                return(BadRequest());
            }

            ComputerModel cp;

            switch (computer.TypeName)
            {
            case "Desktop":
                PCComputer newPC = new PCComputer {
                    Id            = Guid.NewGuid(),
                    TypeName      = computer.TypeName,
                    Brand         = computer.Brand,
                    ProcessorName = computer.ProcessorName,
                    Quantity      = computer.Quantity,
                    UsbPorts      = computer.UsbPorts,
                    RamSlots      = computer.RamSlots,
                    ImageUrl      = computer.ImageUrl,
                };
                cp = newPC;
                break;

            case "Laptop":
                LaptopComputer newLP = new LaptopComputer {
                    Id            = Guid.NewGuid(),
                    TypeName      = computer.TypeName,
                    Brand         = computer.Brand,
                    ProcessorName = computer.ProcessorName,
                    Quantity      = computer.Quantity,
                    ImageUrl      = computer.ImageUrl,
                    ScreenSize    = computer.ScreenSize
                };
                cp = newLP;
                break;

            default:
                return(BadRequest());
            }

            await _computerService.Add(cp);

            return(cp);
        }
Exemplo n.º 4
0
        public async void Test_Laptop_Storing_And_Unboxing()
        {
            //Test that when we store a Laptop object via its parent class, we can unbox the return value to an
            //instance of the Laptop class and we don't just get a truncated parent class.

            LaptopComputer lt = new LaptopComputer {
                Brand = "Dell", ProcessorName = "Pentium", ScreenSize = 15
            };

            await cp.PostComputer(lt);

            var l = await cp.GetComputer(lt.Id);

            var res = l.Value as LaptopComputer;

            Assert.Equal(lt, res);
            Assert.Equal(15, res.ScreenSize); //Result has ScreenSize property.
        }
Exemplo n.º 5
0
        public async void Test_Laptop_Storing_And_Update()
        {
            LaptopComputer lt = new LaptopComputer {
                Brand = "Dell", ProcessorName = "Pentium", Quantity = 1, ScreenSize = 13
            };

            ActionResult <ComputerModel> result = await cp.PostComputer(lt);

            var res = result.Value as LaptopComputer;

            res.ScreenSize = 15;

            ActionResult <ComputerModel> result2 = await cp.PutComputer(res.Id, res);

            var res2 = result2.Value as LaptopComputer;

            Assert.Equal(lt, res);                         //New Laptop and inserted Laptop are identical.
            Assert.Equal(res.ScreenSize, res2.ScreenSize); //Updated laptop has correct screen size.
        }
Exemplo n.º 6
0
 /// <summary>
 /// Create a new LaptopComputer object.
 /// </summary>
 /// <param name="brand">Initial value of Brand.</param>
 /// <param name="id">Initial value of Id.</param>
 public static LaptopComputer CreateLaptopComputer(string brand, int id)
 {
     LaptopComputer laptopComputer = new LaptopComputer();
     laptopComputer.Brand = brand;
     laptopComputer.Id = id;
     return laptopComputer;
 }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            API_Obj             Test;
            double              curren     = 0;
            double              priceValue = 0;
            string              splitCurr;
            List <CompanyAsset> companyAssets = new List <CompanyAsset>();
            String              URLString     = "https://v6.exchangerate-api.com/v6/aeccd74cdf37986987dc6b47/latest/USD";

            using (var webClient = new System.Net.WebClient())
            {
                var json = webClient.DownloadString(URLString);
                Test = JsonConvert.DeserializeObject <API_Obj>(json);
            }

            var    nameOfProperty = "conversion_rates";
            var    propertyInfo   = Test.GetType().GetProperty(nameOfProperty);
            object value          = propertyInfo.GetValue(Test, null);

            //Write to file
            using (StreamWriter sw = File.CreateText("CurrencyCuntry.txt"))
            {
                sw.WriteLine("sweden SEK");
                sw.WriteLine("china CNY");
                sw.WriteLine("poland PLN");
                sw.WriteLine("denemark DKK");
            }

            while (true)
            {
                Console.WriteLine("Press 1 to insert Computers information, press 2 to insert Phone information or q to quite ");
                string input = Console.ReadLine();
                if (input.ToLower() == "q")
                {
                    break;;
                }
                if (input == "1")
                {
                    Console.Write("Model name: ");
                    string   modelName = Console.ReadLine();
                    DateTime dt;
                    while (true)
                    {
                        Console.Write("Purschase date in MM-DD-YYYY format: ");
                        // DateTime purchaseDate = DateTime.Parse(Console.ReadLine());
                        string purchaseDate = Console.ReadLine();
                        var    isValidDate  = DateTime.TryParse(purchaseDate, out dt);
                        if (isValidDate)
                        {
                            break;
                        }
                        else
                        {
                            Console.WriteLine($"{dt} is not a valid date string");
                        }
                    }

                    while (true)
                    {
                        Console.Write("Price in dollar: ");
                        Console.Write("$");
                        string price = Console.ReadLine();
                        if (double.TryParse(price, out priceValue))
                        {
                            break;
                        }
                        else
                        {
                            Console.WriteLine("Flektigt formatt på price!");
                        }
                    }
                    Console.Write("Office Location: ");
                    string       office = Console.ReadLine();
                    CompanyAsset ca     = new LaptopComputer(priceValue, dt, modelName, office);
                    companyAssets.Add(ca);
                }

                if (input == "2")
                {
                    Console.Write("Model name: ");
                    string   modelName = Console.ReadLine();
                    DateTime dt;
                    while (true)
                    {
                        Console.Write("Purschase date in MM/DD/YYYY format: ");
                        // DateTime purchaseDate = DateTime.Parse(Console.ReadLine());
                        string purchaseDate = Console.ReadLine();
                        var    isValidDate  = DateTime.TryParse(purchaseDate, out dt);
                        if (isValidDate)
                        {
                            break;
                        }
                        else
                        {
                            Console.WriteLine($"{dt} is not a valid date string");
                        }
                    }
                    // string cutencyName = "";
                    while (true)
                    {
                        Console.Write("Price in dollar: ");
                        Console.Write("$");
                        string price = Console.ReadLine();
                        if (double.TryParse(price, out priceValue))
                        {
                            break;
                        }
                        else
                        {
                            Console.WriteLine("Flektigt formatt på price!");
                        }
                    }
                    Console.Write("Office Location: ");
                    string office = Console.ReadLine();

                    CompanyAsset companyAsset = new MobilPhones(priceValue, dt, modelName, office);
                    companyAssets.Add(companyAsset);
                }
            }

            CompanyAsset mobilPhones = new MobilPhones(400, new DateTime(2018, 8, 30), "Iphone", "China");

            companyAssets.Add(mobilPhones);
            CompanyAsset mobilPhones1 = new MobilPhones(100, new DateTime(2018, 2, 28), "Samsung", "Poland");

            companyAssets.Add(mobilPhones1);
            CompanyAsset mobilPhones2 = new MobilPhones(200, new DateTime(2021, 5, 4), "Nokia", "Denemark");

            companyAssets.Add(mobilPhones2);
            CompanyAsset mobilPhones3 = new LaptopComputer(100, new DateTime(2018, 8, 30), "ASUS", "China");

            companyAssets.Add(mobilPhones3);
            CompanyAsset mobilPhones4 = new LaptopComputer(100, new DateTime(2018, 2, 28), "MacBook", "Poland");

            companyAssets.Add(mobilPhones4);
            CompanyAsset mobilPhones5 = new LaptopComputer(100, new DateTime(2021, 5, 4), "Lenovo", "Denemark");

            companyAssets.Add(mobilPhones5);

            //   LEVEL2
            // List<CompanyAsset> sortedCompanyAsset = companyAssets.
            //   OrderBy(companyAsset => companyAsset.GetType().Name).
            //  ThenBy(companyAsset => companyAsset.PurchaseDate).
            //  ToList();

            //LEVEL3
            List <CompanyAsset> sortedCompanyAsset = companyAssets.
                                                     OrderBy(companyAsset => companyAsset.Office).
                                                     ThenBy(companyAsset => companyAsset.PurchaseDate).
                                                     ToList();

            Console.WriteLine("Office".PadRight(15) + "Model Name".PadRight(15) + "Price".PadRight(20) + "Purchase Date".PadRight(10));
            Console.WriteLine("======================================================================");

            foreach (CompanyAsset company in sortedCompanyAsset)
            {
                var s = File.ReadLines("CurrencyCuntry.txt")
                        .SkipWhile(line => !line.Contains(company.Office.ToLower()));
                string[] split = s.First().Split(" ");
                splitCurr = split[1];

                foreach (var cur in value.GetType().GetProperties().Where(p => !p.GetGetMethod().GetParameters().Any()))
                {
                    if (cur.Name == splitCurr)
                    {
                        curren = (double)cur.GetValue(value, null);
                    }
                }
                DateTime dt           = DateTime.Now;
                int      incresedYear = company.PurchaseDate.Year + 3;
                DateTime newdt        = company.PurchaseDate.AddYears(incresedYear - company.PurchaseDate.Year);
                TimeSpan diff         = newdt.Subtract(dt);


                //LEVEL2
                // if (diff.TotalDays < 90)
                //{
                //    Console.Write(company.GetType().Name.PadRight(20));
                //   Console.BackgroundColor = ConsoleColor.Red;
                //    Console.WriteLine(company.ModelName.PadRight(10) + (company.Price * curren).ToString("0.##") + "  " +
                //      company.Currency.PadRight(10) + company.PurchaseDate);
                //   Console.ResetColor();
                // }
                // else if (diff.TotalDays < 180)
                //{
                //   Console.Write(company.GetType().Name.PadRight(20));
                //  Console.BackgroundColor = ConsoleColor.Yellow;
                //   Console.WriteLine(company.ModelName.PadRight(10) + (company.Price * curren).ToString("0.##")
                //      + "  " + company.Currency.PadRight(10) + company.PurchaseDate);
                //  Console.ResetColor();
                //}
                //else
                //{
                //   Console.ResetColor();
                //   Console.WriteLine(company.GetType().Name.PadRight(20) + company.ModelName.PadRight(10) + (company.Price * curren).ToString("0.##") + "  " + company.Currency.PadRight(10) + company.PurchaseDate);
                // }

                //LEVEL3
                if (diff.TotalDays < 90)
                {
                    Console.Write(company.Office.PadRight(15));
                    Console.BackgroundColor = ConsoleColor.Red;
                    Console.WriteLine(company.ModelName.PadRight(15) + (company.Price * curren).ToString("0.##").PadRight(10) +
                                      splitCurr.PadRight(10).PadRight(10) + company.PurchaseDate);
                    Console.ResetColor();
                }
                else if (diff.TotalDays < 180)
                {
                    Console.Write(company.Office.PadRight(15));
                    Console.BackgroundColor = ConsoleColor.Yellow;
                    Console.WriteLine(company.ModelName.PadRight(15) + (company.Price * curren).ToString("0.##").PadRight(10)
                                      + splitCurr.PadRight(10) + company.PurchaseDate);
                    Console.ResetColor();
                }
                else
                {
                    Console.ResetColor();
                    Console.WriteLine(company.Office.PadRight(15) + company.ModelName.PadRight(15)
                                      + (company.Price * curren).ToString("0.##").PadRight(10)
                                      + splitCurr.PadRight(10) + company.PurchaseDate);
                }
            }
            Console.ReadLine();
        }