elena1234

HelloFrance-roundedWith:F2

Oct 15th, 2020 (edited)
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace HelloFrance
  6. {
  7.     class MainClass
  8.     {
  9.         public static void Main(string[] args)
  10.         {
  11.             List<string> listOfItems = Console.ReadLine().Split('|').ToList();
  12.             double leftMoney = double.Parse(Console.ReadLine());
  13.             List<double> listOfBoughtItemsOnlyPrices = new List<double>();
  14.             List<double> listOfNewPricesAfterSold = new List<double>().ToList();
  15.  
  16.             for (int i = 0; i < listOfItems.Count; i++)
  17.             {
  18.                 List<string> itemWithPrice = listOfItems[i].Split("->").ToList();
  19.                 string currentItem = itemWithPrice[0];
  20.                 double currentPrice = double.Parse(itemWithPrice[1]);
  21.  
  22.                 if (currentItem == "Clothes" && currentPrice <= 50.00 && leftMoney >= currentPrice)
  23.                 {
  24.                     leftMoney -= currentPrice;
  25.                     listOfBoughtItemsOnlyPrices.Add(currentPrice);
  26.                     listOfNewPricesAfterSold.Add(currentPrice + (0.40 * currentPrice));
  27.                 }
  28.  
  29.                 else if (currentItem == "Shoes" && currentPrice <= 35.00 && leftMoney >= currentPrice)
  30.                 {
  31.                     leftMoney -= currentPrice;
  32.                     listOfBoughtItemsOnlyPrices.Add(currentPrice);
  33.                     listOfNewPricesAfterSold.Add(currentPrice + (0.40 * currentPrice));
  34.                 }
  35.  
  36.                 else if (currentItem == "Accessories" && currentPrice <= 20.50 && leftMoney >= currentPrice)
  37.                 {
  38.                     leftMoney -= currentPrice;
  39.                     listOfBoughtItemsOnlyPrices.Add(currentPrice);
  40.                     listOfNewPricesAfterSold.Add(currentPrice + (0.40 * currentPrice));
  41.                 }
  42.             }
  43.  
  44.             foreach (var item in listOfNewPricesAfterSold)
  45.             {
  46.                 Console.Write($"{item:F2} "); // never use Math.Round in this case, because in VS Console.WriteLine(Math.Round(0.005, 2)) gives 0, but Console.WriteLine($"{0.005:F2}") gives 0.01;
  47.             }
  48.  
  49.             Console.WriteLine();
  50.             double profit = listOfNewPricesAfterSold.Sum() - listOfBoughtItemsOnlyPrices.Sum();
  51.             Console.WriteLine($"Profit: {profit:F2}");
  52.  
  53.             double allMoney = leftMoney + listOfNewPricesAfterSold.Sum();
  54.             if (allMoney >= 150)
  55.             {
  56.                 Console.WriteLine("Hello, France!");
  57.             }
  58.  
  59.             else
  60.             {
  61.                 Console.WriteLine("Time to go.");
  62.             }
  63.  
  64.         }
  65.     }
  66. }
  67.  
Add Comment
Please, Sign In to add comment