Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. public class Program
  5. {
  6. public static void Main()
  7. {
  8. string[] napis = null;
  9. Czas24h t = null;
  10.  
  11. // wczytanie i parsowanie napisu oznaczającego godzinę, np. 2:15:27
  12. napis = Console.ReadLine().Split(':');
  13. int[] czas = Array.ConvertAll(napis, int.Parse);
  14. try
  15. {
  16. t = new Czas24h(czas[0], czas[1], czas[2]);
  17. }
  18. catch (ArgumentException)
  19. {
  20. Console.WriteLine("error");
  21. return;
  22. }
  23.  
  24. // wczytanie liczby poleceń
  25. int liczbaPolecen = int.Parse(Console.ReadLine());
  26.  
  27. for (int i = 1; i <= liczbaPolecen; i++)
  28. {
  29. // wczytanie polecenia
  30. napis = Console.ReadLine().Split(' ');
  31. string typPolecenia = napis[0];
  32. int liczba = int.Parse(napis[1]);
  33.  
  34. // wykonanie polecenia na obiekcie t typu Czas24h
  35. try
  36. {
  37. switch (typPolecenia)
  38. {
  39. case "g":
  40. if (liczba < 0 || liczba > 23)
  41. throw new ArgumentException(String.Format("{0} Nieprawidlowy format godzin: ", liczba), "! Wymagany przedzial: <0, 23>");
  42. t.Godzina = liczba;
  43. break;
  44. case "m":
  45. if (liczba < 0 || liczba > 59)
  46. throw new ArgumentException(String.Format("{0} Nieprawidlowy format godzin: ", liczba), "! Wymagany przedzial: <0, 23>");
  47. t.Minuta = liczba;
  48. break;
  49. case "s":
  50. if (liczba < 0 || liczba > 59)
  51. throw new ArgumentException(String.Format("{0} Nieprawidlowy format godzin: ", liczba), "! Wymagany przedzial: <0, 23>");
  52. t.Sekunda = liczba;
  53. break;
  54. }
  55. }
  56. catch (ArgumentException)
  57. {
  58. Console.WriteLine("error");
  59. return;
  60. }
  61. }
  62. Console.WriteLine(t);
  63. }
  64. }
  65.  
  66. public class Czas24h
  67. {
  68. private int liczbaSekund;
  69.  
  70. public int Sekunda
  71. {
  72. get => liczbaSekund - Godzina * 60 * 60 - Minuta * 60;
  73. // uzupełnij kod - zdefiniuj setters'a
  74. set => liczbaSekund = (Godzina*60*60) + (Minuta*60) + value;
  75. }
  76.  
  77. public int Minuta
  78. {
  79. get => (liczbaSekund / 60) % 60;
  80. // uzupełnij kod - zdefiniuj setters'a
  81. set => liczbaSekund = (Godzina * 60 * 60) + (value * 60) + Sekunda;
  82. }
  83.  
  84. public int Godzina
  85. {
  86. get => liczbaSekund / 3600;
  87. // uzupełnij kod - zdefiniuj setters'a
  88. set => liczbaSekund = (value * 60 * 60) + (Minuta * 60) + Sekunda;
  89. }
  90.  
  91. public Czas24h(int godzina, int minuta, int sekunda)
  92. {
  93. // uzupełnij kod zgłaszając wyjątek ArgumentException
  94. // w sytuacji niepoprawnych danych
  95. if (godzina < 0 || godzina > 23)
  96. throw new ArgumentException(String.Format("{0} Nieprawidlowy format godzin: ", godzina), "! Wymagany przedzial: <0, 23>");
  97.  
  98. if (minuta < 0 || minuta > 59)
  99. throw new ArgumentException(String.Format("{0} Nieprawidlowy format minut: ", minuta), "! Wymagany przedzial: <0, 59>");
  100.  
  101. if (sekunda < 0 || sekunda > 59)
  102. throw new ArgumentException(String.Format("{0} Nieprawidlowy format sekund: ", sekunda), "! Wymagany przedzial: <0, 59>");
  103.  
  104. liczbaSekund = sekunda + 60 * minuta + 3600 * godzina;
  105. }
  106.  
  107. public override string ToString() => $"{Godzina}:{Minuta:D2}:{Sekunda:D2}";
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement