create a specific vehicle type?

Dexter

New member
Joined
Mar 24, 2022
Messages
1
Programming Experience
Beginner
0


I have a small project which I am working on as a beginner in OOP and I would like to ask for some help on a particular problem I encountered.

I have Vehicle class that looks like this:
C#:
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace CarGarageSoftware
{
    class Vehicle
    {

 
        public string Lplate
        public string Make;
        public string LMOT;
        public string Esize;
        public string Transmission;
        public string Mileage;
        public string FuleT;

class PersonalUseCars:Vehicle
    {
   
    }
    class BussinessCars:Vehicle
    {
        public string CompanyN;
        public string CompanyA;
        public string FleetN;
    }
    class WorkVehicle:Vehicle
    {
        public string CompanyN;
        public string CompanyA;
        public string purpose;
        public string Type;
    }
    class Motobykes:Vehicle
    {

    }




And I have an order class that makes orders and I have a constructer in that class that takes data from a text file reads it and creates a new vehicle. The constructer looks like this:
C#:
public Order(string ordLine, string vehLine,string[] logLine)
        {
            orderDetails = new JobDetails(ordLine);
       
       
            vehicleDetails = new Vehicle();
            mechanicLog = new WorkLog();
            for(int i = 0; i < logLine.Length; i = i + 2)
            {
                mechanicLog.MakeNewEntry(logLine[i], logLine[i + 1]);
            }
            orderName = $"O_{vehicleDetails.Lplate}_{orderDetails.CustomerName}";

        }

So I would like to have vehicleDetails which is the data to create a specific vehicle type. I would like to know which is the fastest and simplest way I could achieve such a result?
 
Last edited by a moderator:
The simplest solution (but is also a brittle solution), is to simply have a switch statement that decides what subclass to instantiate, and then fill in the details that are specific to that subclass, and then outside switch statement fill in the details that are generic to all the vehicles.

In pseudo code:
C#:
enum VehicleType { PersonalCar, BusinessCar, WorkCar, Motorcycle };
:
public Order(VehicleType vehicleType, ...)
{
    Vehicle vehicle = null;

    switch (vehicleType)
    {
        case VehicleType.WorkCar:
            var work = new WorkVehicle();
            vehicle = work;
            work.CompanyN = ...;
            break;

        case VehicleType.Motorcycle:
            :
    }

    work.Lplate = ...;
}
 
Back
Top Bottom