RB-Technical-Test/Data-Import/Car.cs

49 lines
1.5 KiB
C#
Raw Normal View History

2020-06-26 12:56:07 +00:00
using System;
using System.Runtime.CompilerServices;
namespace Data_Import
{
class Car
{
private string registration;
public string Registration { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public string Colour { get; set; }
public string FuelType { get; set; }
public bool HasValidRegistration()
{
//The order of these if statements could be improved, however they are written in this order for readability.
if (Registration.Length != 8)
return false;
if (!char.IsLetter(Registration[0]) || !char.IsLetter(Registration[1]))
return false;
if (!char.IsNumber(Registration[2]) || !char.IsNumber(Registration[3]))
return false;
if (Registration[4] != ' ')
return false;
if (!char.IsLetter(Registration[5]) || !char.IsLetter(Registration[6]) || !char.IsLetter(Registration[7]))
return false;
return true;
}
public string AsCSV()
{
return string.Format("{0},{1},{2},{3},{4}", Registration, Make, Model, Colour, FuelType);
}
public Car FromCSV(string csv)
{
var splitCSV = csv.Split(',');
Registration = splitCSV[0];
Make = splitCSV[1];
Model = splitCSV[2];
Colour = splitCSV[3];
FuelType = splitCSV[4];
return this;
}
}
}