Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter1/Homework/8 #813

Open
wants to merge 1 commit into
base: Chapter1/Homework/8
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
213 changes: 208 additions & 5 deletions Src/BootCamp.Chapter/BalanceStats.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,240 @@
namespace BootCamp.Chapter
using System.Globalization;
using System.Linq;
using System.Text;
using System;

namespace BootCamp.Chapter
{
public static class BalanceStats
{
public static string FormatString(string strHigh)
{
//below block is for formatting the text to be written.
int index = 0;
int HoldIndex = 0;
while (true)
{
index = strHigh.IndexOf(",", index);
if (index > 0)
{
HoldIndex = index;
++index;
continue;
}
else break;
}

//To replace last found ',' with and replace the ',' with ", "
if (HoldIndex > 0)
{
var sb = new StringBuilder(strHigh);
sb.Remove(HoldIndex, 1);
sb.Insert(HoldIndex, " and ");
sb.Replace(",", ", ");
strHigh = sb.ToString();
}
return strHigh;
}
/// <summary>
/// Return name and balance(current) of person who had the biggest historic balance.
/// </summary>
public static string FindHighestBalanceEver(string[] peopleAndBalances)
{
return "";

float LastHighBalance = -99999999.00f;
string strHigh = "N/A.";

//If the balances are not having any items or null return N/A.
if (peopleAndBalances == null || peopleAndBalances.Length == 0)
{
return "N/A.";
}

foreach (string str in peopleAndBalances)
{
//if the strings are empty we shall continue with next iteration
if (str.Length == 0)
continue;

//strings have balances, so split them with ','.
string[] PersonDetails = str.Split(",", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

for (int i = 1; i < PersonDetails.Length; ++i)
{
float balance;
bool tryFloat = float.TryParse(PersonDetails[i], NumberStyles.Currency, CultureInfo.CreateSpecificCulture("en-GB"), out balance);
if (!tryFloat) continue;
//retireve the highest balance.
if (LastHighBalance < balance)
{
LastHighBalance = balance;
strHigh = PersonDetails[0];
}
//People with same high historic balances need to appended.
else if (LastHighBalance == balance)
{
strHigh = string.Concat(strHigh, ",", PersonDetails[0]);
}
}
}
//For the array of balances, if there are no balances then return N/A.
if (strHigh.CompareTo("N/A.") == 0) return strHigh;

strHigh = FormatString(strHigh);

return strHigh = string.Concat(strHigh, $" had the most money ever. ¤{LastHighBalance}.");


}

/// <summary>
/// Return name and loss of a person with a biggest loss (balance change negative).
/// </summary>
public static string FindPersonWithBiggestLoss(string[] peopleAndBalances)
{
return "";
float BiggestLoss = 999999999f;
string strHigh = "N/A.";

//If the balances are not having any items or null return N/A.
if (peopleAndBalances == null || peopleAndBalances.Length == 0)
{
return "N/A.";
}

foreach (string str in peopleAndBalances)
{
//if the strings are empty we shall continue with next iteration
if (str.Length == 0)
continue;

//strings have balances, so split them with ','.
string[] PersonDetails = str.Split(",", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

for (int i = 1; i < PersonDetails.Length; ++i)
{

float balance;
bool tryFloat = float.TryParse(PersonDetails[i], NumberStyles.Currency, CultureInfo.CreateSpecificCulture("en-GB"), out balance);
if (!tryFloat) continue;
//retireve the highest balance.
if (BiggestLoss > balance)
{
BiggestLoss = balance;
strHigh = PersonDetails[0];
}
//People with same high historic balances need to appended.
else if (BiggestLoss == balance)
{
strHigh = string.Concat(strHigh, ",", PersonDetails[0]);
}
}
}

if (BiggestLoss >= 0) return "N/A.";
//For the array of balances, if there are no balances then return N/A.
if (strHigh.CompareTo("N/A.") == 0) return strHigh;

strHigh = FormatString(strHigh);
CultureInfo culture = new CultureInfo("de-De");
return strHigh = string.Concat(strHigh, $" lost the most money. ¤{BiggestLoss}.");
}

/// <summary>
/// Return name and current money of the richest person.
/// </summary>
public static string FindRichestPerson(string[] peopleAndBalances)
{
return "";
float CurrentRichestBal = -99999999.00f;
string strHigh = "N/A.";
bool bFlag = false;

//If the balances are not having any items or null return N/A.
if (peopleAndBalances == null || peopleAndBalances.Length == 0)
{
return "N/A.";
}

foreach (string str in peopleAndBalances)
{
//if the strings are empty we shall continue with next iteration
if (str.Length == 0)
continue;

//strings have balances, so split them with ','.
string[] PersonDetails = str.Split(",", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

float balance;
bool tryFloat = float.TryParse(PersonDetails.Last(), NumberStyles.Currency, CultureInfo.CreateSpecificCulture("en-GB"), out balance);
if (!tryFloat) continue;

//retireve the highest current balance.
if (CurrentRichestBal < balance)
{
CurrentRichestBal = balance;
strHigh = PersonDetails[0];
}
//People with same high historic balances need to appended.
else if (CurrentRichestBal == balance)
{
bFlag = true;
strHigh = string.Concat(strHigh, ",", PersonDetails[0]);
}
}
//For the array of balances, if there are no balances then return N/A.
if (strHigh.CompareTo("N/A.") == 0) return strHigh;

strHigh = FormatString(strHigh);
if (bFlag) return strHigh = string.Concat(strHigh, $" are the richest people. ¤{CurrentRichestBal}.");
return strHigh = string.Concat(strHigh, $" is the richest person. ¤{CurrentRichestBal}.");
}

/// <summary>
/// Return name and current money of the most poor person.
/// </summary>
public static string FindMostPoorPerson(string[] peopleAndBalances)
{
return "";
float LeastCurrMoney = 99999999.00f;
string strHigh = "N/A.";
bool bFlag = false;

//If the balances are not having any items or null return N/A.
if (peopleAndBalances == null || peopleAndBalances.Length == 0)
{
return "N/A.";
}

foreach (string str in peopleAndBalances)
{
//if the strings are empty we shall continue with next iteration
if (str.Length == 0)
continue;

//strings have balances, so split them with ','.
string[] PersonDetails = str.Split(",", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

float balance;
bool tryFloat = float.TryParse(PersonDetails.Last(), NumberStyles.Currency, CultureInfo.CreateSpecificCulture("en-GB"), out balance);
if (!tryFloat) continue;

//retireve the highest current balance.
if (LeastCurrMoney > balance)
{
LeastCurrMoney = balance;
strHigh = PersonDetails[0];
}
//People with same high historic balances need to appended.
else if (LeastCurrMoney == balance)
{
bFlag = true;
strHigh = string.Concat(strHigh, ",", PersonDetails[0]);
}
}
//For the array of balances, if there are no balances then return N/A.
if (strHigh.CompareTo("N/A.") == 0) return strHigh;

strHigh = FormatString(strHigh);
if (bFlag) return strHigh = string.Concat(strHigh, $" have the least money. ¤{LeastCurrMoney}.");
return strHigh = string.Concat(strHigh, $" has the least money. ¤{LeastCurrMoney}.");
}
}
}
2 changes: 1 addition & 1 deletion Src/BootCamp.Chapter/BootCamp.Chapter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netcoreapp6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
61 changes: 59 additions & 2 deletions Src/BootCamp.Chapter/FileCleaner.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,77 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;

namespace BootCamp.Chapter
{
public static class FileCleaner
{
static void CheckForInvalidCharInName(string[] lineArr)
{
string str = lineArr[0];
if (!(str.All(x => Char.IsLetter(x) || x == ' ' || x == '\'' || x == '-')))
throw new InvalidBalancesException("Invalid Character in Name", new Exception());
}

static void CheckForInvalidCharInBalance(string[] lineArr)
{
float balance = 0.0f;
for (int i = 1; i < lineArr.Length; i++)
{
bool tryFloat = float.TryParse(lineArr[i], NumberStyles.Currency, CultureInfo.CreateSpecificCulture("en-GB"), out balance);
if (!tryFloat) throw new InvalidBalancesException("Balance is not valid", new Exception());
}

}

/// <summary>
/// Cleans up dirtyFileName
/// </summary>
/// <param name="dirtyFile">Dirty file with "_" placed in random places.</param>
/// <param name="cleanedFile">Cleaned up file without any "_".</param>
public static void Clean(string dirtyFile, string cleanedFile)
{
File.WriteAllText(cleanedFile, "a");
}
try
{
if (dirtyFile == null || dirtyFile.Length == 0)
{
throw new ArgumentException();
}

if (!File.Exists(dirtyFile))
{
throw new ArgumentException();
}

string stringFile = File.ReadAllText(dirtyFile);

if (stringFile.Length > 0)
{
if (stringFile.Contains('_'))
{
stringFile = stringFile.Replace("_", "");
}
var arrStr = stringFile.Split("\r\n", StringSplitOptions.RemoveEmptyEntries);
foreach (var str in arrStr)
{
var lineArr = str.Split(",", StringSplitOptions.RemoveEmptyEntries);
CheckForInvalidCharInName(lineArr);
CheckForInvalidCharInBalance(lineArr);
}
File.WriteAllText(cleanedFile, stringFile.ToString());
}
File.WriteAllText(cleanedFile, stringFile.ToString());
}
catch(InvalidBalancesException e)
{
throw e;
}

}
}
}
Loading