Advertisement
JulianJulianov

Break or Continue!

May 30th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.20 KB | None | 0 0
  1. Hello, France
  2. The budget was enough to get them to Frankfurt and they have some money left, but their final aim is to go to France, which means that they will need more finances. They’ve decided to make profit by buying items on discount from the Thrift Shop and selling them for a higher price. You must help them.
  3. Create a program that calculates the profit after buying some items and selling them on a higher price. In order to fulfil that, you are going to need certain data - you will receive a collection of items and a budget in the following format:
  4. {type->price|type->price|type->price……|type->price}
  5. {budget}
  6. The prices for each of the types cannot exceed a certain price, which is given bellow:
  7. Type    Maximum Price
  8. Clothes 50.00
  9. Shoes   35.00
  10. Accessories 20.50
  11. If a price for a certain item is higher than the maximum price, don’t buy it. Every time you buy an item, you have to reduce the budget with the value of its price. If you don’t have enough money for it, you can’t buy it. Buy as much items as you can.
  12. You have to increase the price of each of the items you have successfully bought with 40%. Print the list with the new prices and the profit you will gain from selling the items. They need exactly 150$ for tickets for the train, so if their budget after selling the products is enough – print – "Hello, France!" and if not – "Time to go."
  13. Input / Constraints
  14. • On the 1st line you are going to receive the items with their prices in the format described above – real numbers in the range [0.00……1000.00]
  15. • On the 2nd line, you are going to be given the budget – a real number in the range [0.0.1000.0]
  16. Output
  17. • Print the list with the bought item’s new prices, rounded 2 digits after the decimal separator in the following format:
  18. "{price1} {price2} {price3} {price5}………{priceN}"
  19. • Print the profit, rounded 2 digits after the decimal separator in the following format:
  20. "Profit: {profit}"
  21. If the money for tickets are enough, print: "Hello, France!" and if not – "Time to go."
  22. Examples
  23. Input                                                                                   Output           
  24. Clothes->43.30|Shoes->25.25|Clothes->36.52|Clothes->20.90|Accessories->15.60            60.62 35.35 51.13
  25. 120                                                                                     Profit: 42.03
  26.                                                                                         Hello, France!
  27. Comments                                                                                    
  28. We start subtracting the valid prices from the budget:
  29. 12043.40 = 76.7.
  30. 76.725.25 = 51.45
  31. 51.4536.52 = 14.93
  32. 14.93 is less than 20.90 and 15.60, so we can’t buy either of the last two. We must increase each price with 40% and the new prices are: 60.62 35.35 51.13. The profit is 42.03 and their new budget will be – what is left of the budget - 14.93 + {sum of all newPrices}. It is enough, so we print: Hello, France!
  33. Input                                                                                   Output
  34. Shoes->41.20|Clothes->20.30|Accessories->40|Shoes->15.60|Shoes->33.30|Clothes->48.60    28.42 21.84 46.62
  35. 90                                                                                      Profit: 27.68
  36.                                                                                         Time to go.
  37. using System;
  38. using System.Collections.Generic;
  39.  
  40. public class Program
  41. {
  42.     public static void Main()
  43.     {
  44.            var collectionTypeAndPrice = Console.ReadLine().Split('|');
  45.             var budget = decimal.Parse(Console.ReadLine());
  46.  
  47.             var maxPriceClothes = 50m;
  48.             var maxPriceShoes = 35m;
  49.             var maxpriceAccessories = 20.50m;
  50.             var percent = 0.40m;
  51.             var neededMoney = 150m;
  52.             var newPrice = 0m;
  53.             var sumOldPrice = 0m;
  54.             var sumNewPrice = 0m;
  55.            
  56.             var listPrice = new List<string>();
  57.  
  58.             foreach (var item in collectionTypeAndPrice)
  59.             {
  60.                 var typeAndPrice = item.Split("->");
  61.                 var type = typeAndPrice[0];
  62.                 var price = decimal.Parse(typeAndPrice[1]);
  63.                 if (budget < price)
  64.                 {
  65.                     continue;  //Така продължава с проверката, защото може следващата цена да е в границите на бюджета!
  66.                 }
  67.                 var countValidPrice = 0;
  68.                 switch (type)
  69.                 {
  70.                     case "Clothes":
  71.                         if (price <= maxPriceClothes)
  72.                         {
  73.                             budget -= price;
  74.                             listPrice.Add($"{newPrice = (percent * price) + price:F2}");
  75.                             countValidPrice++;
  76.                         }
  77.                         break;
  78.                     case "Shoes":
  79.                         if (price <= maxPriceShoes)
  80.                         {
  81.                             budget -= price;
  82.                             listPrice.Add($"{newPrice = (percent * price) + price:F2}");
  83.                             countValidPrice++;
  84.                         }
  85.                         break;
  86.                     case "Accessories":
  87.                         if (price <= maxpriceAccessories)
  88.                         {
  89.                             budget -= price;
  90.                             listPrice.Add($"{newPrice = (percent * price) + price:F2}");
  91.                             countValidPrice++;
  92.                         }
  93.                         break;
  94.                 }
  95.                 if (countValidPrice > 0)
  96.                 {
  97.                     sumOldPrice += price;
  98.                     sumNewPrice += newPrice;
  99.                 }  
  100.             }
  101.             var profit = sumNewPrice - sumOldPrice;
  102.             Console.WriteLine(string.Join(" ", listPrice));
  103.             Console.WriteLine($"Profit: {profit:F2}");
  104.             if (budget + sumNewPrice >= neededMoney)
  105.             {
  106.                 Console.WriteLine("Hello, France!");
  107.             }
  108.             else
  109.             {
  110.                 Console.WriteLine("Time to go.");
  111.             }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement