diff --git a/Src/BootCamp.Chapter/BalanceStats.cs b/Src/BootCamp.Chapter/BalanceStats.cs index f78ebd2c4..d84836a90 100644 --- a/Src/BootCamp.Chapter/BalanceStats.cs +++ b/Src/BootCamp.Chapter/BalanceStats.cs @@ -1,13 +1,87 @@ -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; + } /// /// Return name and balance(current) of person who had the biggest historic balance. /// 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}."); + } /// @@ -15,7 +89,47 @@ public static string FindHighestBalanceEver(string[] peopleAndBalances) /// 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}."); } /// @@ -23,7 +137,44 @@ public static string FindPersonWithBiggestLoss(string[] peopleAndBalances) /// 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}."); } /// @@ -31,7 +182,44 @@ public static string FindRichestPerson(string[] peopleAndBalances) /// 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}."); } } } diff --git a/Src/BootCamp.Chapter/BootCamp.Chapter.csproj b/Src/BootCamp.Chapter/BootCamp.Chapter.csproj index 958d2f1da..fa2362844 100644 --- a/Src/BootCamp.Chapter/BootCamp.Chapter.csproj +++ b/Src/BootCamp.Chapter/BootCamp.Chapter.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.0 + netcoreapp6.0 diff --git a/Src/BootCamp.Chapter/PeoplesBalances.cs b/Src/BootCamp.Chapter/PeoplesBalances.cs index c10fde43c..d21632e63 100644 --- a/Src/BootCamp.Chapter/PeoplesBalances.cs +++ b/Src/BootCamp.Chapter/PeoplesBalances.cs @@ -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. /// - 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" }; } } diff --git a/Src/BootCamp.Chapter/Program.cs b/Src/BootCamp.Chapter/Program.cs index 02f631c29..204456e24 100644 --- a/Src/BootCamp.Chapter/Program.cs +++ b/Src/BootCamp.Chapter/Program.cs @@ -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)); } } } diff --git a/Src/BootCamp.Chapter/TextTable.cs b/Src/BootCamp.Chapter/TextTable.cs index 611cc28aa..c5d0a7067 100644 --- a/Src/BootCamp.Chapter/TextTable.cs +++ b/Src/BootCamp.Chapter/TextTable.cs @@ -1,4 +1,9 @@ -namespace BootCamp.Chapter +using System; +using System.Runtime.ConstrainedExecution; +using System.Security.Cryptography.X509Certificates; +using System.Text; + +namespace BootCamp.Chapter { /// /// Part 1. @@ -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}"); + } + } /// /// Build a table for given message with given padding. @@ -34,7 +58,38 @@ public static class TextTable /// 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(); } } } diff --git a/Tests/BootCamp.Chapter.Tests/BootCamp.Chapter.Tests.csproj b/Tests/BootCamp.Chapter.Tests/BootCamp.Chapter.Tests.csproj index 3a8778c77..ffca4abc4 100644 --- a/Tests/BootCamp.Chapter.Tests/BootCamp.Chapter.Tests.csproj +++ b/Tests/BootCamp.Chapter.Tests/BootCamp.Chapter.Tests.csproj @@ -1,7 +1,7 @@ - netcoreapp3.0 + netcoreapp6.0 false diff --git a/Tests/BootCamp.Chapter.Tests/Input/MostPoorPersonExpectations.cs b/Tests/BootCamp.Chapter.Tests/Input/MostPoorPersonExpectations.cs index 9861cb703..6cda6bfcd 100644 --- a/Tests/BootCamp.Chapter.Tests/Input/MostPoorPersonExpectations.cs +++ b/Tests/BootCamp.Chapter.Tests/Input/MostPoorPersonExpectations.cs @@ -14,7 +14,7 @@ protected override IEnumerable 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." } }; } diff --git a/Tests/BootCamp.Chapter.Tests/Input/PersonWithBiggestLossExpectations.cs b/Tests/BootCamp.Chapter.Tests/Input/PersonWithBiggestLossExpectations.cs index f5179afb4..3164482d0 100644 --- a/Tests/BootCamp.Chapter.Tests/Input/PersonWithBiggestLossExpectations.cs +++ b/Tests/BootCamp.Chapter.Tests/Input/PersonWithBiggestLossExpectations.cs @@ -11,9 +11,9 @@ protected override IEnumerable 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." } }; }