Skip to content
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
198 changes: 193 additions & 5 deletions Src/BootCamp.Chapter/BalanceStats.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,225 @@
namespace BootCamp.Chapter
using System;
using System.Text;
using System.Globalization;
using System.Runtime.Intrinsics.Arm;
using System.Reflection;
using System.Linq;

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)
{
//retireve the highest balance.
if (LastHighBalance < float.Parse(PersonDetails[i]))
{
LastHighBalance = float.Parse(PersonDetails[i]);
strHigh = PersonDetails[0];
}
//People with same high historic balances need to appended.
else if (LastHighBalance == float.Parse(PersonDetails[i]))
{
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)
{
//retireve the highest balance.
if (BiggestLoss > float.Parse(PersonDetails[i]))
{
BiggestLoss = float.Parse(PersonDetails[i]);
strHigh = PersonDetails[0];
}
//People with same high historic balances need to appended.
else if (BiggestLoss == float.Parse(PersonDetails[i]))
{
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);

//retireve the highest current balance.
if (CurrentRichestBal < float.Parse(PersonDetails.Last()))
{
CurrentRichestBal = float.Parse(PersonDetails.Last());
strHigh = PersonDetails[0];
}
//People with same high historic balances need to appended.
else if (CurrentRichestBal == float.Parse(PersonDetails.Last()))
{
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);

//retireve the highest current balance.
if (LeastCurrMoney > float.Parse(PersonDetails.Last()))
{
LeastCurrMoney = float.Parse(PersonDetails.Last());
strHigh = PersonDetails[0];
}
//People with same high historic balances need to appended.
else if (LeastCurrMoney == float.Parse(PersonDetails.Last()))
{
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>

</Project>
12 changes: 7 additions & 5 deletions Src/BootCamp.Chapter/PeoplesBalances.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ public static class PeoplesBalances
/// Line is made by name (no spaces), follow by balances separated by comma (",").
/// Example: "Gily, 1, 0". Means that currently Gily has 0, which dropped from initial 1.
/// </summary>
public static string[] Balances => new[]
public static string[] Balances => new string []
{
"Tom, 15.5, 200, 500, 600, 200, 500, 1000",
"Katherine, 85, 0, -500, 0, 500, 10000, 1500.99",
"Bill, 99999, , 99970, 99900",
"Catie, 0, 500, 990, 1300"
//"tom, 1, 4 , , , , 5 "
//"Tom, 1", "Avi, 1", "dae, 1"
"Tom, 15.5, 200, 500, 600, 200, 500, 1000",
"Katherine, 85, 0, 500, 0, -500, 10000, 1500.99",
"Bill, 99999, , 99970, 99900",
"Catie, 0, 500, 990, 99900"
};
}
}
12 changes: 10 additions & 2 deletions Src/BootCamp.Chapter/Program.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
namespace BootCamp.Chapter
using System;

namespace BootCamp.Chapter
{
class Program
{
static void Main(string[] args)
{
// Print each of the statistical output using Text Table with padding 3:
// - FindHighestBalanceEver
Console.WriteLine(BalanceStats.FindHighestBalanceEver(PeoplesBalances.Balances));
// - FindPersonWithBiggestLoss
Console.WriteLine(BalanceStats.FindPersonWithBiggestLoss(PeoplesBalances.Balances));
// - FindRichestPerson
Console.WriteLine(BalanceStats.FindRichestPerson(PeoplesBalances.Balances));
// - FindMostPoorPerson
Console.WriteLine(BalanceStats.FindMostPoorPerson(PeoplesBalances.Balances));
Console.WriteLine(TextTable.Build("Hello", 0));
Console.WriteLine(TextTable.Build($"Hello{Environment.NewLine}World!", 0));
Console.WriteLine(TextTable.Build("Hello", 4));
}
}
}
59 changes: 57 additions & 2 deletions Src/BootCamp.Chapter/TextTable.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
namespace BootCamp.Chapter
using System;
using System.Runtime.ConstrainedExecution;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace BootCamp.Chapter
{
/// <summary>
/// Part 1.
Expand Down Expand Up @@ -26,6 +31,25 @@ public static class TextTable
+-------+

*/
public static void PrintChar(char c, int strLength, StringBuilder sb)
{
for (int j = 0; j < (strLength); ++j) {sb.Append(c); }
}
public static void PrintHyphen(int strLength, StringBuilder sb)
{
sb.Append('+');
PrintChar('-', strLength, sb);
sb.Append($"+{Environment.NewLine}");
}
public static void PritPipe(int strLength, StringBuilder sb, int padding)
{
for (int j = 0; j < padding; ++j)
{
sb.Append('|');
PrintChar(' ', strLength, sb);
sb.Append($"|{Environment.NewLine}");
}
}

/// <summary>
/// Build a table for given message with given padding.
Expand All @@ -34,7 +58,38 @@ public static class TextTable
/// </summary>
public static string Build(string message, int padding)
{
return "";
if(message == null || message.Length == 0)
{
return "";
}
var sb = new StringBuilder();
var arr = message.Split("\r\n", StringSplitOptions.RemoveEmptyEntries);
for(int x = 0; x < arr.Length; ++x)
{
arr[x] = arr[x].PadLeft(padding + arr[x].Length);
arr[x] = arr[x].PadRight(padding + arr[x].Length);
}
var strLength = 0;
for (int i = 0; i < arr.Length; ++i)
{
if(strLength < arr[i].Length) { strLength = arr[i].Length; }

}

PrintHyphen(strLength, sb);
PritPipe(strLength, sb, padding);

for (int k = 0; k < arr.Length; ++k)
{
sb.Append('|');
sb.Append(arr[k].PadRight(strLength));
sb.Append($"|{Environment.NewLine}");
}

PritPipe(strLength, sb, padding);
PrintHyphen(strLength, sb);

return sb.ToString();
}
}
}
2 changes: 1 addition & 1 deletion Tests/BootCamp.Chapter.Tests/BootCamp.Chapter.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protected override IEnumerable<object[]> GetInput()
new object[]{Person1Balance1, "Tom has the least money. ¤1." },
new object[]{Person1Balance2, "Tom has the least money. ¤0." },
new object[]{Person2Balance1, "Gillie has the least money. ¤0." },
new object[]{Person3Balance3, "Tom has the least money. 1." },
new object[]{Person3Balance3, "Tom has the least money. ¤-1." },
new object[]{Person3Same, "Tom, Gillie and Agnes have the least money. ¤1." }
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ protected override IEnumerable<object[]> GetInput()
new object[]{Null, "N/A."},
new object[]{Empty, "N/A."},
new object[]{Person1Balance1, "N/A." },
new object[]{Person1Balance2, "Tom lost the most money. -¤1." },
new object[]{Person1Balance2, "N/A." },
new object[]{Person2Balance1, "N/A." },
new object[]{Person3Balance3, "Tom lost the most money. -¤4." },
new object[]{Person3Balance3, "Tom lost the most money. ¤-1." },
new object[]{Person3Same, "N/A." }
};
}
Expand Down