Advertisement
dobroslav-atanasov

Untitled

Mar 7th, 2018
1,125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.27 KB | None | 0 0
  1. //////////////////////////////// MAIN
  2. using System;
  3.  
  4.  
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. string[] carInfo = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  10. string[] truckInfo = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  11. string[] busInfo = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  12.  
  13. Car car = (Car)CreateVehicle(carInfo);
  14. Truck truck = (Truck)CreateVehicle(truckInfo);
  15. Bus bus = (Bus)CreateVehicle(busInfo);
  16.  
  17. int n = int.Parse(Console.ReadLine());
  18.  
  19. for (int i = 0; i < n; i++)
  20. {
  21. string[] command = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  22. string toDo = command[0];
  23. string type = command[1];
  24.  
  25. try
  26. {
  27. switch (toDo)
  28. {
  29. case "Drive":
  30. double distance = double.Parse(command[2]);
  31. if (type == "Car")
  32. {
  33. car.Drive(distance);
  34. }
  35. else if (type == "Truck")
  36. {
  37. truck.Drive(distance);
  38. }
  39. else if (type == "Bus")
  40. {
  41. bus.Drive(distance);
  42. }
  43. break;
  44. case "Refuel":
  45. double liters = double.Parse(command[2]);
  46. if (type == "Car")
  47. {
  48. car.Refuel(liters);
  49. }
  50. else if (type == "Truck")
  51. {
  52. truck.Refuel(liters);
  53. }
  54. else if (type == "Bus")
  55. {
  56. bus.Refuel(liters);
  57. }
  58. break;
  59. case "DriveEmpty":
  60. double busDistance = double.Parse(command[2]);
  61. bus.DriveEmpty(busDistance);
  62. break;
  63. }
  64. }
  65. catch (ArgumentException ex)
  66. {
  67. Console.WriteLine(ex.Message);
  68. }
  69. }
  70.  
  71. Console.WriteLine($"Car: {car.FuelQuantity:F2}");
  72. Console.WriteLine($"Truck: {truck.FuelQuantity:F2}");
  73. Console.WriteLine($"Bus: {bus.FuelQuantity:F2}");
  74. }
  75.  
  76. public static Vehicle CreateVehicle(string[] vehicleInfo)
  77. {
  78. double initialFuelQuantity = double.Parse(vehicleInfo[1]);
  79. double litersPerKilometer = double.Parse(vehicleInfo[2]);
  80. double tankCapacity = double.Parse(vehicleInfo[3]);
  81.  
  82. string type = vehicleInfo[0];
  83.  
  84. switch (type)
  85. {
  86. case "Car":
  87. Car car = new Car(initialFuelQuantity, litersPerKilometer, tankCapacity);
  88. return car;
  89. case "Truck":
  90. Truck truck = new Truck(initialFuelQuantity, litersPerKilometer, tankCapacity);
  91. return truck;
  92. case "Bus":
  93. Bus bus = new Bus(initialFuelQuantity, litersPerKilometer, tankCapacity);
  94. return bus;
  95. default:
  96. throw new Exception();
  97. }
  98. }
  99. }
  100.  
  101.  
  102. //////////////////////////////// BUS
  103.  
  104. using System;
  105. using System.Collections.Generic;
  106. using System.Text;
  107.  
  108.  
  109. public class Bus : Vehicle
  110. {
  111. public Bus(double fuel, double consumption, double tankCapacity)
  112. {
  113. this.FuelQuantity = fuel > tankCapacity ? 0 : fuel;
  114. this.ConsumprionPerKilometer = consumption;
  115. this.TankCapacity = tankCapacity;
  116. }
  117.  
  118. public override void Drive(double distance)
  119. {
  120. double consumedFuel = distance * (this.ConsumprionPerKilometer + 1.4);
  121. if (this.FuelQuantity - consumedFuel >= 0)
  122. {
  123. Console.WriteLine($"Bus travelled {distance} km");
  124. this.FuelQuantity -= consumedFuel;
  125. }
  126. else
  127. {
  128. Console.WriteLine("Bus needs refueling");
  129. }
  130. }
  131.  
  132. public void DriveEmpty(double distance)
  133. {
  134. double consumedFuel = distance * this.ConsumprionPerKilometer;
  135. if (this.FuelQuantity - consumedFuel >= 0)
  136. {
  137. Console.WriteLine($"Bus travelled {distance} km");
  138. this.FuelQuantity -= consumedFuel;
  139. }
  140. else
  141. {
  142. Console.WriteLine("Bus needs refueling");
  143. }
  144. }
  145. public override void Refuel(double liters)
  146. {
  147. if (liters <= 0)
  148. {
  149. throw new ArgumentException("Fuel must be a positive number");
  150. }
  151. else if (this.FuelQuantity + liters > this.TankCapacity)
  152. {
  153. throw new ArgumentException($"Cannot fit {liters} fuel in the tank");
  154. }
  155. this.FuelQuantity += liters;
  156. }
  157. }
  158.  
  159.  
  160. using System;
  161.  
  162. public class Car : Vehicle
  163. {
  164. private const double CAR_CONSUMPTION_DURING_SUMMER = 0.9;
  165.  
  166. public Car(double fuel, double consumption, double tankCapacity)
  167. {
  168. this.FuelQuantity = fuel > tankCapacity ? 0 : fuel;
  169. this.ConsumprionPerKilometer = consumption + CAR_CONSUMPTION_DURING_SUMMER;
  170. this.TankCapacity = tankCapacity;
  171. }
  172.  
  173. public override void Drive(double distance)
  174. {
  175. double consumedFuel = distance * this.ConsumprionPerKilometer;
  176. if (this.FuelQuantity - consumedFuel >= 0)
  177. {
  178. Console.WriteLine($"Car travelled {distance} km");
  179. this.FuelQuantity -= consumedFuel;
  180. }
  181. else
  182. {
  183. Console.WriteLine("Car needs refueling");
  184. }
  185. }
  186.  
  187. public override void Refuel(double liters)
  188. {
  189. if (liters <= 0)
  190. {
  191. throw new ArgumentException("Fuel must be a positive number");
  192. }
  193. else if (this.FuelQuantity + liters > this.TankCapacity)
  194. {
  195. throw new ArgumentException($"Cannot fit {liters} fuel in the tank");
  196. }
  197. this.FuelQuantity += liters;
  198. }
  199. }
  200.  
  201. ///////////////////////////////// TRUCK
  202.  
  203. using System;
  204. using System.Collections.Generic;
  205. using System.Text;
  206.  
  207.  
  208. public class Truck : Vehicle
  209. {
  210. private const double TRUCK_CONSUMPTION_DURING_SUMMER = 1.6;
  211.  
  212. public Truck(double fuel, double consumption, double tankCapacity)
  213. {
  214. this.FuelQuantity = fuel > tankCapacity ? 0 : fuel;
  215. this.ConsumprionPerKilometer = consumption + TRUCK_CONSUMPTION_DURING_SUMMER;
  216. this.TankCapacity = tankCapacity;
  217. }
  218.  
  219. public override void Drive(double distance)
  220. {
  221. double consumedFuel = distance * this.ConsumprionPerKilometer;
  222. if (this.FuelQuantity - consumedFuel >= 0)
  223. {
  224. Console.WriteLine($"Truck travelled {distance} km");
  225. this.FuelQuantity -= consumedFuel;
  226. }
  227. else
  228. {
  229. Console.WriteLine("Truck needs refueling");
  230. }
  231. }
  232.  
  233. public override void Refuel(double liters)
  234. {
  235. if (liters <= 0)
  236. {
  237. throw new ArgumentException("Fuel must be a positive number");
  238. }
  239. else if (this.FuelQuantity + liters > this.TankCapacity)
  240. {
  241. throw new ArgumentException($"Cannot fit {liters} fuel in the tank");
  242. }
  243. double fuelToAdd = (liters * 95) / 100.0;
  244. this.FuelQuantity += fuelToAdd;
  245. }
  246. }
  247.  
  248.  
  249.  
  250. public abstract class Vehicle
  251. {
  252. private double fuelQuantity;
  253. private double consumprionPerKilometer;
  254. private double tankCapacity;
  255.  
  256. public double FuelQuantity
  257. {
  258. get { return fuelQuantity; }
  259. set { fuelQuantity = value; }
  260. }
  261.  
  262. public double ConsumprionPerKilometer
  263. {
  264. get { return consumprionPerKilometer; }
  265. set { consumprionPerKilometer = value; }
  266. }
  267.  
  268. public double TankCapacity
  269. {
  270. get { return tankCapacity; }
  271. set
  272. { tankCapacity = value; }
  273. }
  274.  
  275.  
  276. public abstract void Drive(double distance);
  277. public abstract void Refuel(double liters);
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement