Upload project.
This commit is contained in:
11
Data-Import/CSVUtils.cs
Normal file
11
Data-Import/CSVUtils.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System.IO;
|
||||
|
||||
namespace Data_Import
|
||||
{
|
||||
public static class CSVUtils
|
||||
{
|
||||
public static void WriteCSV(string[] csv, string filename) => File.WriteAllLines(filename, csv);
|
||||
|
||||
public static string[] ReadCSV(string filename) => File.ReadAllLines(filename);
|
||||
}
|
||||
}
|
||||
48
Data-Import/Car.cs
Normal file
48
Data-Import/Car.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
9
Data-Import/Data-Import.csproj
Normal file
9
Data-Import/Data-Import.csproj
Normal file
@@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<RootNamespace>Data_Import</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
62
Data-Import/Program.cs
Normal file
62
Data-Import/Program.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Data_Import
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var carContext = GetCarsFromCSV("data.csv").ToList();
|
||||
|
||||
ExportCSVsByFuelType(carContext);
|
||||
|
||||
var validCars = GetValidCars(carContext);
|
||||
Console.WriteLine("Valid Cars:\n");
|
||||
foreach(var c in validCars)
|
||||
{
|
||||
Console.WriteLine(c.AsCSV());
|
||||
}
|
||||
Console.WriteLine();
|
||||
|
||||
var invalidCarCount = carContext.Count - validCars.Count();
|
||||
Console.WriteLine(String.Format("Invalid Car Count:\n{0}", invalidCarCount));
|
||||
}
|
||||
|
||||
private static void ExportCSVsByFuelType(List<Car> cars)
|
||||
{
|
||||
//Could use a map here?
|
||||
List<string> petrolCars = new List<string>(), dieselCars = new List<string>();
|
||||
|
||||
foreach (var c in cars)
|
||||
{
|
||||
if (c.FuelType == "Petrol")
|
||||
petrolCars.Add(c.AsCSV());
|
||||
else if (c.FuelType == "Diesel")
|
||||
dieselCars.Add(c.AsCSV());
|
||||
}
|
||||
|
||||
CSVUtils.WriteCSV(petrolCars.ToArray(), "petrol.csv");
|
||||
CSVUtils.WriteCSV(dieselCars.ToArray(), "diesel.csv");
|
||||
}
|
||||
|
||||
private static IEnumerable<Car> GetValidCars(List<Car> cars)
|
||||
{
|
||||
foreach (var car in cars)
|
||||
{
|
||||
if (car.HasValidRegistration())
|
||||
yield return car;
|
||||
}
|
||||
}
|
||||
|
||||
private static IEnumerable<Car> GetCarsFromCSV(string filename)
|
||||
{
|
||||
foreach(var carString in CSVUtils.ReadCSV(filename))
|
||||
{
|
||||
var c = new Car();
|
||||
yield return c.FromCSV(carString);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
41
Data-Import/data.csv
Normal file
41
Data-Import/data.csv
Normal file
@@ -0,0 +1,41 @@
|
||||
Car Registration,Make,Model,Colour,Fuel
|
||||
B640 BBJ,Peugeot,106,Red,Petrol
|
||||
NJ13 LXX,Ford,Fiesta,Blue,Petrol
|
||||
B640BBJ,VW,Golf,Black,Diesel
|
||||
LD57 RTE,Audi,A4,Grey,Petrol
|
||||
RB65 ROB,Seat,Cupra,Black,Petrol
|
||||
SK67 HSB,Nissan,Leaf,Green,Electric
|
||||
NHZ 6842,Seat,Ibiza,Black,Petrol
|
||||
NDR 543R,Volkswagen,Golf,Yellow,Diesel
|
||||
SK67 HSB,Porsche,911,Red,Petrol
|
||||
AD58 LDR,Mercedes,C Class,Silver,Petrol
|
||||
NDJ64 XYZ,Fiat,500,White,Petrol
|
||||
LT59 MAJ,Ford,Mondeo,Silver,Diesel
|
||||
MD60XBA,Ford,Fiesta,Silver,Diesel
|
||||
X201JKL,Jeep,Compass,Orange,Petrol
|
||||
SL12 JBV,Hyundai,i40,Grey,Deisel
|
||||
RB66 OLP,Audi,A1,White,petrol
|
||||
SN18 DDF,Hyundai,i20,Green,Petrol
|
||||
TR10 NNB,Renault,Megane,Blue,Petrol
|
||||
PO13 DFS,BMW,M4,Green,Petral
|
||||
KR09 RVN,Kia,C'eed,White,Petrol
|
||||
KR59DFE,Vauxhall,Corsa,Orange,Diesel
|
||||
X913 UOP,Toyota,Prius,Black,Hybrid
|
||||
CY21 MSK,Tesla,Cybertruck,Silver,Electric
|
||||
UL66 OPL,Ford,Mustang,Green,Diesel
|
||||
UE13 URT,Audi,RS6,Blue,Desel
|
||||
WE66 AFG,BMW,135i,Grey,Petrol
|
||||
JO19 FRS,Pegeot,Partner,White,Diesel
|
||||
JM13 LKO,Hyundai,i10,Black,Petrol
|
||||
DF66 ORT,Citreon,Saxo,Mustard,Petrol
|
||||
GL65 DOW,Suzuki,GSX100,Blue,Petrol
|
||||
VB07 QKD,Tesla,Model 3,Red,Electric
|
||||
NI55 SAN,Nissan,Juke,Yello,Petrol
|
||||
XL13 PSR,Mini,Cooper,Red,Petrol
|
||||
JW18 HSV,Nissan,Micra,Red,Petrol
|
||||
FL61 JER,Ford,Mustang,Yellow,Petrol
|
||||
DP67 IOA,Peugeot,3008,Black,Petrol
|
||||
DL56 0PP,Audi,S4,Red,Diesel
|
||||
WRI5 TYI,BMW,I3,White,Electric
|
||||
QO59 OTL,BMW,Coupe 3,Black,Petrol
|
||||
HJ66 NSF,Renault,Clio,Blue,Petrol
|
||||
|
Reference in New Issue
Block a user