Car design

mail@pastecode.io avatar
unknown
csharp
16 days ago
1.8 kB
0
Indexable
Never
public interface ICar
{
    string Make { get; }
    string Model { get; }
    int Year { get; }
    decimal Price { get; }

    void Start();
    void DisplayInfo();
}

public abstract class CarBase : ICar
{
    public abstract string Make { get; }
    public abstract string Model { get; }
    public abstract int Year { get; }
    public abstract decimal Price { get; }

    public virtual void Start()
    {
        Console.WriteLine($"Starting the {Year} {Make} {Model}...");
    }

    public virtual void DisplayInfo()
    {
        Console.WriteLine($"Car: {Year} {Make} {Model}");
        Console.WriteLine($"Price: ${Price}");
    }
}

public class Sedan : CarBase
{
    public override string Make => "Honda";
    public override string Model => "Accord";
    public override int Year => 2022;
    public override decimal Price => 25000m;

    public int TrunkSize { get; set; }

    public override void DisplayInfo()
    {
        base.DisplayInfo();
        Console.WriteLine($"Trunk Size: {TrunkSize} cubic feet");
    }
}

public class SUV : CarBase
{
    public override string Make => "Toyota";
    public override string Model => "Rav4";
    public override int Year => 2023;
    public override decimal Price => 30000m;

    public int TowingCapacity { get; set; }

    public override void DisplayInfo()
    {
        base.DisplayInfo();
        Console.WriteLine($"Towing Capacity: {TowingCapacity} lbs");
    }
}
class Program
{
    static void Main(string[] args)
    {
        ICar sedan = new Sedan { TrunkSize = 15 };
        ICar suv = new SUV { TowingCapacity = 5000 };

        sedan.DisplayInfo();
        sedan.Start();

        Console.WriteLine();

        suv.DisplayInfo();
        suv.Start();
    }
}
Leave a Comment