Advertisement
nikolayneykov

Untitled

Mar 11th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _02Hello_France
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string[] items = Console.ReadLine().Split("|");
  12.             double budget = double.Parse(Console.ReadLine());
  13.  
  14.             // set the initial max prices of all products (consts)
  15.             double clothesMaxPrice = 50;
  16.             double shoesMaxPrice = 35;
  17.             double accessoriesMaxPrice = 20.50;
  18.  
  19.             List<double> totalPrices = new List<double>();
  20.             double oldPrices = default(double);   // create this variable to store the old prices of products
  21.  
  22.             for (int i = 0; i < items.Length; i++)  // loop all the items
  23.             {
  24.                 string[] tokens = items[i].Split("->");  // make an inner array to split the items by ->
  25.                 string product = tokens[0];              // set the product                      
  26.                 double price = double.Parse(tokens[1]);  // set the price ot this product
  27.  
  28.                 // check what command is recieved from the tokens array
  29.                 if (product == "Clothes")
  30.                 {
  31.                     if (price > clothesMaxPrice)         // check the price of product with the max price
  32.                     {
  33.                         continue; // do nothing
  34.                     }
  35.                     else
  36.                     {
  37.                         if (price > budget)  // check whether the price is bigger than the given budget
  38.                         {
  39.                             continue; // do nothing
  40.                         }
  41.                         budget -= price;  // remove that price from the budget
  42.                         oldPrices += price; // add the price to the old prices variable
  43.                         price += price * 0.4; // add the additional 40% to the price, because the product is already bought
  44.                         totalPrices.Add(price); // add the price to the list total prices
  45.                     }
  46.                 }
  47.                 else if (product == "Shoes") // the same steps as above
  48.                 {
  49.                     if (price > shoesMaxPrice) // check whether the price is bigger than the given budget
  50.                     {
  51.                         continue;  // do nothing
  52.                     }
  53.                     else
  54.                     {
  55.                         if (price > budget)
  56.                         {
  57.                             continue; // do nothing
  58.                         }
  59.                         budget -= price;
  60.                         oldPrices += price;
  61.                         price += price * 0.4;
  62.                         totalPrices.Add(price);
  63.                     }
  64.                 }
  65.                 else if (product == "Accessories") // the same steps as above
  66.                 {
  67.                     if (price > accessoriesMaxPrice)
  68.                     {
  69.                         continue;
  70.                     }
  71.                     else
  72.                     {
  73.                         if (price > budget)
  74.                         {
  75.                             continue;
  76.                         }
  77.                         budget -= price;
  78.                         oldPrices += price;
  79.                         price += price * 0.4;
  80.                         totalPrices.Add(price);
  81.                     }
  82.                 }
  83.             }
  84.  
  85.             double sumOfNewPrices = totalPrices.Sum();     // make a new variable to store the sum of total prices list (this step can be skipped)
  86.             double profit = sumOfNewPrices - oldPrices;    // calculate the profit
  87.  
  88.             double finalBudget = budget + sumOfNewPrices;  // calculate the final budget, which is the current budget + the sum of new prices
  89.  
  90.             //foreach (var item in totalPrices)              // make all the elements formatted to second decimal point
  91.             //{
  92.             //    Console.Write($"{item:F2} ");
  93.             //}
  94.             string[] formatedPrices = totalPrices.Select(x => x.ToString("F2")).ToArray();
  95.             Console.WriteLine(string.Join(" ", formatedPrices));                           // print new line in order to print the next line on new line
  96.  
  97.             Console.WriteLine($"Profit: {profit:F2}");
  98.             Console.WriteLine(finalBudget >= 150 ? "Hello, France!" : "Time to go.");
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement