Advertisement
Guest User

Untitled

a guest
Jun 29th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. public class CarProblem
  5. {
  6. public static void Main()
  7. {
  8. int[] carInfo = Console.ReadLine().Split().Select(int.Parse).ToArray();
  9. int speed = carInfo[0];
  10. int fuel = carInfo[1];
  11. int fuelEconomy = carInfo[2];
  12. var car = new Car(speed, fuel, fuelEconomy);
  13. string input = Console.ReadLine();
  14.  
  15. while (input != "END")
  16. {
  17. string[] command = input.Split();
  18. if (command[0] == "Travel")
  19. {
  20. car.Travel(int.Parse(command[1]));
  21. }
  22. else if (command[0] == "Refuel")
  23. {
  24. car.Refuel(int.Parse(command[1]));
  25. }
  26. else if (command[0] == "Distance")
  27. {
  28. Console.WriteLine($"Total distance: {car.TotalDistance():f1} kilometers");
  29. }
  30. else if (command[0] == "Time")
  31. {
  32. Console.WriteLine($"Total time: {car.Hours()} hours and {car.Minutes()} minutes");
  33. }
  34. else if (command[0] == "Fuel")
  35. {
  36. Console.WriteLine($"Fuel left: {car.RemFuel():f1} liters");
  37. }
  38. input = Console.ReadLine();
  39. }
  40. }
  41. public class Car
  42. {
  43. public double speed;
  44. public double fuel;
  45. public double fuelEconomy;
  46. public double traveledDistance = 0;
  47. public int hours = 0;
  48. public int minutes = 0;
  49.  
  50. public Car(double speed, double fuel, double fuelEconomy)
  51. {
  52. this.speed = speed;
  53. this.fuel = fuel;
  54. this.fuelEconomy = fuelEconomy;
  55. }
  56. public void Travel(double distanceToTravel)
  57. {
  58. double realDistance = Math.Min((fuel / fuelEconomy) * speed, distanceToTravel);
  59. traveledDistance += realDistance;
  60. }
  61. public void Refuel(double refuelAmount)
  62. {
  63. this.fuel += refuelAmount;
  64. }
  65. public double TotalDistance()
  66. {
  67. return traveledDistance;
  68. }
  69. public int Hours()
  70. {
  71. hours = (int)(traveledDistance / speed);
  72. return hours;
  73. }
  74. public int Minutes()
  75. {
  76. minutes = (int)(this.traveledDistance % speed);
  77. return minutes;
  78. }
  79. public double RemFuel()
  80. {
  81. return fuel - fuelEconomy * (traveledDistance / speed);
  82. }
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement