Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2016
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. using System;
  2.  
  3. class PriceChangeAlert
  4. {
  5. static void Main()
  6. {
  7. int n = int.Parse(Console.ReadLine());
  8. decimal threshold = decimal.Parse(Console.ReadLine());
  9. decimal lastPrice = decimal.Parse(Console.ReadLine());
  10.  
  11. for (int i = 0; i < n - 1; i++)
  12. {
  13. decimal currentPrice = decimal.Parse(Console.ReadLine());
  14. decimal diff = ProcentDifference(lastPrice, currentPrice);
  15. bool isSignificantDifference = isDif(diff, threshold);
  16. string message = GetMessage(currentPrice, lastPrice, diff, isSignificantDifference);
  17. Console.WriteLine(message);
  18. lastPrice = currentPrice;
  19. }
  20. }
  21.  
  22. private static string GetMessage(decimal currentPrice, decimal lastPrice, decimal procentDiff, bool etherTrueOrFalse)
  23. {
  24. string output = "";
  25. if (procentDiff == 0)
  26. {
  27. output = string.Format("NO CHANGE: {0}", currentPrice);
  28. }
  29. else if (!etherTrueOrFalse)
  30. {
  31. output = string.Format("MINOR CHANGE: {0} to {1} ({2:F2}%)", lastPrice, currentPrice, procentDiff*100);
  32. }
  33. else if (etherTrueOrFalse && (procentDiff > 0))
  34. {
  35. output = string.Format("PRICE UP: {0} to {1} ({2:F2}%)", lastPrice, currentPrice, procentDiff*100);
  36. }
  37. else if (etherTrueOrFalse && (procentDiff < 0))
  38. output = string.Format("PRICE DOWN: {0} to {1} ({2:F2}%)", lastPrice, currentPrice, procentDiff*100);
  39.  
  40. return output;
  41. }
  42.  
  43. private static bool isDif(decimal Diff,decimal threshold)
  44. {
  45. if (Math.Abs(Diff) >= threshold)
  46. {
  47. return true;
  48. }
  49. return false;
  50. }
  51.  
  52. private static decimal ProcentDifference(decimal lastPrice, decimal currentPrice)
  53. {
  54. decimal diff = (currentPrice - lastPrice) / lastPrice;
  55. return diff;
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement