Advertisement
elena1234

Factory design pattern

Apr 14th, 2021 (edited)
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.75 KB | None | 0 0
  1. using System;
  2. using Vehicles.Exceptions;
  3. using Vehicles.Models;
  4.  
  5. namespace Vehicles.Factory
  6. {
  7.    public class VehiclesCreator
  8.     {
  9.         public Vehicle Create(string type, double fuelQuantity, double fuelConsumption)
  10.         {
  11.             Vehicle vehicle = null;
  12.             if(type == "Car")
  13.             {
  14.                 vehicle = new Car(fuelQuantity, fuelConsumption);
  15.             }
  16.  
  17.             else if(type == "Truck")
  18.             {
  19.                 vehicle = new Truck (fuelQuantity, fuelConsumption);
  20.             }
  21.            
  22.             else if (vehicle == null)
  23.             {
  24.                 throw new ArgumentException(ExceptionMessages.InvalidVehicleTypeMessage);
  25.             }
  26.  
  27.             return vehicle;
  28.         }
  29.     }
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement