Advertisement
butoff

Problem 1. Vehicles

Mar 2nd, 2018
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.16 KB | None | 0 0
  1. using System;
  2.  
  3. public class Program
  4. {
  5.     static void Main()
  6.     {
  7.         Car car = (Car)Factory();
  8.         Truck truck = (Truck)Factory();
  9.         int cnt = int.Parse(Console.ReadLine());
  10.  
  11.         while (cnt > 0)
  12.         {
  13.             string command = Console.ReadLine();
  14.             string[] data = command.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
  15.             string action = data[0] + data[1];
  16.             decimal argument = decimal.Parse(data[2]);
  17.  
  18.             switch (action)
  19.             {
  20.                 case "DriveCar":
  21.                     Console.WriteLine(car.Drive(argument));                  
  22.                     break;
  23.                 case "DriveTruck":
  24.                     Console.WriteLine(truck.Drive(argument));
  25.                     break;
  26.                 case "RefuelCar":
  27.                     car.Refuel(argument);
  28.                     break;
  29.                 case "RefuelTruck":
  30.                     truck.Refuel(argument);
  31.                     break;                
  32.             }
  33.             cnt--;
  34.         }
  35.  
  36.         Console.WriteLine(car);
  37.         Console.WriteLine(truck);
  38.     }  
  39.  
  40.     private static Vehicle Factory()
  41.     {
  42.         string[] part = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  43.         decimal fuelQty = decimal.Parse(part[1]);
  44.         decimal fuelConsumption = decimal.Parse(part[2]);
  45.         switch (part[0])
  46.         {
  47.             case "Car":
  48.                 return new Car(fuelQty, fuelConsumption);
  49.             case "Truck":
  50.                 return new Truck(fuelQty, fuelConsumption);
  51.             default:
  52.                 return null;
  53.         }
  54.     }
  55. }
  56.  
  57. public abstract class Vehicle
  58. {
  59.     public Vehicle(decimal fuelQuantity, decimal fuelConsumption)
  60.     {
  61.         FuelQuantity = fuelQuantity;
  62.         FuelConsumption = fuelConsumption;
  63.     }
  64.  
  65.     public decimal FuelQuantity { get; set; }
  66.     public decimal FuelConsumption { get; set; }
  67.  
  68.     public string Drive(decimal distance)
  69.     {
  70.         if (distance * FuelConsumption > FuelQuantity)
  71.         {
  72.             return $"{this.GetType().Name} needs refueling";
  73.         }
  74.         FuelQuantity -= distance * FuelConsumption;
  75.         return $"{this.GetType().Name} travelled {distance} km";
  76.     }
  77.  
  78.     public virtual void Refuel(decimal quantity)
  79.     {
  80.         FuelQuantity += quantity;
  81.     }
  82.  
  83.     public override string ToString()
  84.     {
  85.         return $"{this.GetType().Name}: {this.FuelQuantity:f2}";
  86.     }
  87. }
  88.  
  89. public class Car : Vehicle
  90. {
  91.     private static readonly decimal ADDITIONAL_CONSUMPTION = 0.9m;
  92.  
  93.     public Car(decimal fuelQuantity, decimal fuelConsumption)
  94.         : base(fuelQuantity, fuelConsumption + ADDITIONAL_CONSUMPTION)
  95.     { }
  96. }
  97.  
  98. public class Truck : Vehicle
  99. {
  100.     private static readonly decimal ADDITIONAL_CONSUMPTION = 1.60m;
  101.     private static readonly decimal QTY_MODIFIER = 0.95m;
  102.  
  103.     public Truck(decimal fuelQuantity, decimal fuelConsumption)
  104.         : base(fuelQuantity, fuelConsumption + ADDITIONAL_CONSUMPTION)
  105.     { }
  106.  
  107.     public override void Refuel(decimal quantity)
  108.     {
  109.         FuelQuantity += (quantity * QTY_MODIFIER);
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement