Skip to content

Commit

Permalink
Catch and Throw ; NOT doing things
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonard Sperry committed Dec 7, 2020
1 parent eaa36a6 commit 854de29
Show file tree
Hide file tree
Showing 38 changed files with 1,667 additions and 1,480 deletions.
700 changes: 350 additions & 350 deletions .gitignore

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions CsvProcessor/Account.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace CsvProcessor
{
class Account
{
public int ID { get; set; }
public string Name { get; set; }
public decimal? Value { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Text;

namespace CsvProcessor
{
class Account
{
public int ID { get; set; }
public string Name { get; set; }
public decimal? Value { get; set; }
}
}
88 changes: 44 additions & 44 deletions CsvProcessor/AccountParsingLogic.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace CsvProcessor
{
class AccountParsingLogic : IAccountParsingLogic
{
public bool AccountIsValid(Account account)
{
return
account.ID > 0 &&
!string.IsNullOrEmpty(account.Name) &&
account.Value != null;
}

public int? GetAccountNumber(string input)
{
if (int.TryParse(input, out int accountNumber))
{
return accountNumber;
}
return null;
}

public string GetNameForSaving(string input)
{
if (string.IsNullOrEmpty(input))
{
return null;
}
return input.Length > 50 ? input.Substring(0, 50) : input;
}

public decimal? GetValue(string input)
{
if (decimal.TryParse(input, out decimal val))
{
return val;
}
return null;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;

namespace CsvProcessor
{
class AccountParsingLogic : IAccountParsingLogic
{
public bool AccountIsValid(Account account)
{
return
account.ID > 0 &&
!string.IsNullOrEmpty(account.Name) &&
account.Value != null;
}

public int? GetAccountNumber(string input)
{
if (int.TryParse(input, out int accountNumber))
{
return accountNumber;
}
return null;
}

public string GetNameForSaving(string input)
{
if (string.IsNullOrEmpty(input))
{
return null;
}
return input.Length > 50 ? input.Substring(0, 50) : input;
}

public decimal? GetValue(string input)
{
if (decimal.TryParse(input, out decimal val))
{
return val;
}
return null;
}
}
}
34 changes: 17 additions & 17 deletions CsvProcessor/AccountProvider.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace CsvProcessor
{
class AccountProvider : IAccountProvider
{
public void SaveAccounts(IEnumerable<Account> accounts)
{
foreach (var account in accounts)
{
// save to the database
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;

namespace CsvProcessor
{
class AccountProvider : IAccountProvider
{
public void SaveAccounts(IEnumerable<Account> accounts)
{
foreach (var account in accounts)
{
// save to the database
}
}
}
}
16 changes: 8 additions & 8 deletions CsvProcessor/CsvProcessor.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

</Project>
96 changes: 48 additions & 48 deletions CsvProcessor/CsvReader.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace CsvProcessor
{
class CsvReader<T> : ICsvReader<T> where T : class
{
string _path;
Func<string[], T> _lineParser;

public bool FileHasHeader { get; set; } = true;

public CsvReader(string path, Func<string[], T> lineParser)
{
_path = path;
}

public IEnumerable<T> Read()
{
StreamReader rdr = new StreamReader(_path);
if (FileHasHeader)
{
//ignore the first line
rdr.ReadLine();
}
string line;
T item;
while ((line = rdr.ReadLine()) != null)
{
item = null;
try
{
item = _lineParser(line.Split(','));
}
catch (Exception)
{
//log the error
}
if (item != null)
{
yield return item;
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace CsvProcessor
{
class CsvReader<T> : ICsvReader<T> where T : class
{
string _path;
Func<string[], T> _lineParser;

public bool FileHasHeader { get; set; } = true;

public CsvReader(string path, Func<string[], T> lineParser)
{
_path = path;
}

public IEnumerable<T> Read()
{
StreamReader rdr = new StreamReader(_path);
if (FileHasHeader)
{
//ignore the first line
rdr.ReadLine();
}
string line;
T item;
while ((line = rdr.ReadLine()) != null)
{
item = null;
try
{
item = _lineParser(line.Split(','));
}
catch (Exception)
{
//log the error
}
if (item != null)
{
yield return item;
}
}
}
}
}
32 changes: 16 additions & 16 deletions CsvProcessor/CustomFileReader.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace CsvProcessor
{
interface ICustomFileReader<T>
{
T ReadLine();
}


class CustomFileReader
{
}
}
using System;
using System.Collections.Generic;
using System.Text;

namespace CsvProcessor
{
interface ICustomFileReader<T>
{
T ReadLine();
}


class CustomFileReader
{
}
}
48 changes: 24 additions & 24 deletions CsvProcessor/Interfaces.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace CsvProcessor
{
interface ICsvReader<T> where T: class
{
IEnumerable<T> Read();
}

interface IAccountParsingLogic
{
string GetNameForSaving(string input);
int? GetAccountNumber(string input);
decimal? GetValue(string input);
bool AccountIsValid(Account account);
}

interface IAccountProvider
{
void SaveAccounts(IEnumerable<Account> accounts);
}
}
using System;
using System.Collections.Generic;
using System.Text;

namespace CsvProcessor
{
interface ICsvReader<T> where T: class
{
IEnumerable<T> Read();
}

interface IAccountParsingLogic
{
string GetNameForSaving(string input);
int? GetAccountNumber(string input);
decimal? GetValue(string input);
bool AccountIsValid(Account account);
}

interface IAccountProvider
{
void SaveAccounts(IEnumerable<Account> accounts);
}
}
Loading

0 comments on commit 854de29

Please sign in to comment.