fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public abstract class Vehicle
  6. {
  7. public int ID { get; private set; }
  8. public string Brand { get; private set; }
  9. public string Model { get; private set; }
  10. public bool IsRented { get; private set; }
  11.  
  12. protected Vehicle(int id, string brand, string model)
  13. {
  14. ID = id;
  15. Brand = brand;
  16. Model = model;
  17. IsRented = false;
  18. }
  19.  
  20. public void RentVehicle()
  21. {
  22. if (!IsRented)
  23. {
  24. IsRented = true;
  25. Console.WriteLine($"Vehicle {ID} has been rented.");
  26. }
  27. else
  28. {
  29. Console.WriteLine($"Vehicle {ID} is already rented.");
  30. }
  31. }
  32.  
  33. public void ReturnVehicle()
  34. {
  35. if (IsRented)
  36. {
  37. IsRented = false;
  38. Console.WriteLine($"Vehicle {ID} has been returned.");
  39. }
  40. else
  41. {
  42. Console.WriteLine($"Vehicle {ID} is not currently rented.");
  43. }
  44. }
  45.  
  46. public abstract void DisplayInfo();
  47. }
  48.  
  49. public class Car : Vehicle
  50. {
  51. public int NumberOfDoors { get; private set; }
  52.  
  53. public Car(int id, string brand, string model, int numberOfDoors)
  54. : base(id, brand, model)
  55. {
  56. NumberOfDoors = numberOfDoors;
  57. }
  58.  
  59. public override void DisplayInfo()
  60. {
  61. Console.WriteLine($"Car ID: {ID}, Brand: {Brand}, Model: {Model}, Doors: {NumberOfDoors}, Rented: {IsRented}");
  62. }
  63. }
  64.  
  65. public class Truck : Vehicle
  66. {
  67. public int LoadCapacity { get; private set; }
  68.  
  69. public Truck(int id, string brand, string model, int loadCapacity)
  70. : base(id, brand, model)
  71. {
  72. LoadCapacity = loadCapacity;
  73. }
  74.  
  75. public override void DisplayInfo()
  76. {
  77. Console.WriteLine($"Truck ID: {ID}, Brand: {Brand}, Model: {Model}, Load Capacity: {LoadCapacity}kg, Rented: {IsRented}");
  78. }
  79. }
  80.  
  81. public class Motorcycle : Vehicle
  82. {
  83. public bool HasSideCar { get; private set; }
  84.  
  85. public Motorcycle(int id, string brand, string model, bool hasSideCar)
  86. : base(id, brand, model)
  87. {
  88. HasSideCar = hasSideCar;
  89. }
  90.  
  91. public override void DisplayInfo()
  92. {
  93. Console.WriteLine($"Motorcycle ID: {ID}, Brand: {Brand}, Model: {Model}, Sidecar: {HasSideCar}, Rented: {IsRented}");
  94. }
  95. }
  96.  
  97. public static class VehicleUtils
  98. {
  99. public static double CalculateFine(int daysLate)
  100. {
  101. double dailyFine = 50.0; // فرض غرامة يومية
  102. return daysLate * dailyFine;
  103. }
  104.  
  105. public static void DisplayRentalPolicy()
  106. {
  107. Console.WriteLine("Rental Policy: Late returns will incur a daily fine of $50.");
  108. }
  109. }
  110.  
  111. public class VehicleOffice
  112. {
  113. private List<Vehicle> vehicles = new List<Vehicle>();
  114.  
  115. public void AddVehicle(Vehicle vehicle)
  116. {
  117. vehicles.Add(vehicle);
  118. }
  119.  
  120. public void DisplayAllVehicles()
  121. {
  122. foreach (var vehicle in vehicles)
  123. {
  124. vehicle.DisplayInfo();
  125. }
  126. }
  127.  
  128. public Vehicle SearchVehicleById(int id)
  129. {
  130. return vehicles.FirstOrDefault(v => v.ID == id);
  131. }
  132. }
  133.  
  134. class Program
  135. {
  136. static void Main(string[] args)
  137. {
  138. VehicleOffice office = new VehicleOffice();
  139.  
  140. // Adding Vehicles
  141. office.AddVehicle(new Car(1, "Toyota", "Corolla", 4));
  142. office.AddVehicle(new Truck(2, "Ford", "F-150", 1000));
  143. office.AddVehicle(new Motorcycle(3, "Harley-Davidson", "Street 750", false));
  144.  
  145. // Display all vehicles
  146. Console.WriteLine("All Vehicles:");
  147. office.DisplayAllVehicles();
  148.  
  149. // Search for a vehicle and rent it
  150. Console.WriteLine("\nSearching for Vehicle with ID 1:");
  151. Vehicle vehicle = office.SearchVehicleById(1);
  152. if (vehicle != null)
  153. {
  154. vehicle.RentVehicle();
  155. vehicle.DisplayInfo();
  156.  
  157. // Returning the vehicle
  158. vehicle.ReturnVehicle();
  159. vehicle.DisplayInfo();
  160. }
  161. else
  162. {
  163. Console.WriteLine("Vehicle not found.");
  164. }
  165.  
  166. // Display rental policy
  167. VehicleUtils.DisplayRentalPolicy();
  168.  
  169. // Example of calculating fine
  170. int daysLate = 3; // عدد الأيام المتأخرة
  171. double fine = VehicleUtils.CalculateFine(daysLate);
  172. Console.WriteLine($"Total fine for {daysLate} days late: ${fine}");
  173. }
  174. }
  175.  
Success #stdin #stdout 0.07s 29420KB
stdin
Standard input is empty
stdout
All Vehicles:
Car ID: 1, Brand: Toyota, Model: Corolla, Doors: 4, Rented: False
Truck ID: 2, Brand: Ford, Model: F-150, Load Capacity: 1000kg, Rented: False
Motorcycle ID: 3, Brand: Harley-Davidson, Model: Street 750, Sidecar: False, Rented: False

Searching for Vehicle with ID 1:
Vehicle 1 has been rented.
Car ID: 1, Brand: Toyota, Model: Corolla, Doors: 4, Rented: True
Vehicle 1 has been returned.
Car ID: 1, Brand: Toyota, Model: Corolla, Doors: 4, Rented: False
Rental Policy: Late returns will incur a daily fine of $50.
Total fine for 3 days late: $150