Created
November 10, 2021 13:10
-
-
Save Lanse505/b06013fe66771c393c2d8740b14d8c62 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Threading; | |
using FirstAssignment.Writer; | |
namespace FirstAssignment{ | |
internal class Program{ | |
private readonly struct Person{ | |
public Person(int index, string name, int age, int ageInYears){ | |
Index = index; | |
Name = name; | |
Age = age; | |
Ageinyears = ageInYears; | |
} | |
public int Index{ get; } | |
public string Name{ get; } | |
public int Age{ get; } | |
public int Ageinyears{ get; } | |
} | |
public static void Main(string[] args){ | |
var familyWriter = new TableWriter("Id", "Namn", "Ålder"); | |
var expandedWriter = new TableWriter("Id", "Namn", "Ålder", "Ålder i dagar", "Äldst", "Yngst"); | |
var ageTotals = new TableWriter("Sammanlagd Ålder", "Genomsnittlig Ålder"); | |
var totalAge = 0; | |
var expandedContent = new List<Person>(); | |
Console.WriteLine("Välkommen till Familje-Kalkylatorn!"); | |
DramaticPause(); | |
Console.WriteLine("Låt oss starta!"); | |
LongSleep(); | |
Console.WriteLine("Vill du köra i 'Extended Information' läge?"); | |
Console.WriteLine("Y = Yes, N = No"); | |
var extendedInformation = Console.ReadLine(); | |
if (extendedInformation == null || extendedInformation.ToUpper() != "Y" && extendedInformation.ToUpper() != "N"){ | |
throw new ArgumentException("'Extended Information Mode' didn't receive a corrent input, should either be Y or N"); | |
} | |
for (var i = 0; i < 4; i++){ | |
var printableName = $"Skriv namnet på person {i + 1}:"; | |
var printableAge = $"Skriv åldern på person {i + 1}:"; | |
Console.WriteLine(printableName); | |
var name = Console.ReadLine(); | |
ShortSleep(); | |
Console.WriteLine(printableAge); | |
var age = Console.ReadLine(); | |
ShortSleep(); | |
Console.WriteLine(); | |
var ageInt = ValidateAndFormatAge(age); | |
totalAge += ageInt; | |
switch (extendedInformation.ToUpper()){ | |
case "N": | |
familyWriter.AddRow(i, name, age); | |
break; | |
case "Y":{ | |
var person = new Person(i, name, ageInt, ageInt * 365); | |
expandedContent.Add(person); | |
break; | |
} | |
} | |
} | |
if (extendedInformation.ToUpper() == "Y"){ | |
var oldestById = new KeyValuePair<int, int>(); | |
var youngestById = new KeyValuePair<int, int>(); | |
foreach (var person in expandedContent){ | |
if (person.Age > oldestById.Value){ | |
oldestById = new KeyValuePair<int, int>(person.Index, person.Age); | |
} | |
if (person.Age < youngestById.Value){ | |
youngestById = new KeyValuePair<int, int>(person.Index, person.Age); | |
} | |
} | |
foreach (var person in expandedContent){ | |
var isOldest = person.Index == oldestById.Key; | |
var isYoungest = person.Index == youngestById.Key; | |
expandedWriter.AddRow(person.Index, person.Name, person.Age, person.Ageinyears, isOldest, isYoungest); | |
} | |
} | |
ageTotals.AddRow(totalAge, totalAge / 4); | |
WriteEmptyLines(2); | |
Console.WriteLine("Tabulerar!"); | |
DramaticPause(); | |
Console.WriteLine(); | |
if (extendedInformation.ToUpper() != "Y"){ | |
familyWriter.Print(); | |
WriteEmptyLines(1); | |
ageTotals.Print(); | |
} | |
else{ | |
expandedWriter.Print(); | |
WriteEmptyLines(1); | |
ageTotals.Print(); | |
} | |
} | |
private static int ValidateAndFormatAge(string age){ | |
int ageInt; | |
try{ | |
ageInt = int.Parse(age); | |
} | |
catch (Exception e){ | |
Console.WriteLine(e); | |
throw; | |
} | |
return ageInt; | |
} | |
private static void ShortSleep(){ | |
Thread.Sleep(500); | |
} | |
private static void LongSleep(){ | |
Thread.Sleep(1500); | |
} | |
private static void DramaticPause(){ | |
for (int j = 0; j < 3; j++){ | |
Thread.Sleep(1000); | |
Console.Write("."); | |
} | |
Console.WriteLine(); | |
} | |
private static void WriteEmptyLines(int amount){ | |
for (int i = 0; i < amount; i++){ | |
Console.WriteLine(); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace FirstAssignment.Writer{ | |
public class TableWriter{ | |
private List<object> Columns{ get; } | |
private List<object[]> Rows{ get; } | |
public TableWriter(params string[] columns){ | |
if (columns == null || columns.Length == 0){ | |
throw new ArgumentException("Input Arguments are either null or none provided!"); | |
} | |
Columns = new List<object>(columns); | |
Rows = new List<object[]>(); | |
} | |
// Adds the content rows to the table | |
public void AddRow(params object[] values){ | |
// Input validation, throws if: The values param is null, The values param is empty, The values param doesn't match up with the columns size. | |
if (values == null || values.Length == 0 || values.Length != Columns.Count){ | |
throw new ArgumentException("Input arguments are either: Null, Input values were 0, or the number of Input values didn't match the number of columns"); | |
} | |
Rows.Add(values); | |
} | |
// Gets a list of the greatest found sizes of entries in each column. | |
// Aka if row 4 has the longest string, it will get added to this list as the fourth entry. | |
private List<int> GetMaxStringLength(){ | |
List<int> colSize = new List<int>(); | |
// Cycles over each row and column and finds the longest possible entry for each column to determine the content width needed for the table. | |
for (int i = 0; i < Columns.Count; i++){ | |
List<object> colRow = new List<object>(); | |
int greatestLength = 0; | |
colRow.Add(Columns[i]); | |
colRow.AddRange(Rows.Select(t => t[i])); | |
for (int k = 0; k < colRow.Count; k++){ | |
int length = colRow[k].ToString().Length; | |
if (length > greatestLength){ | |
greatestLength = length; | |
} | |
} | |
colSize.Add(greatestLength); | |
} | |
return colSize; | |
} | |
public override string ToString(){ | |
StringBuilder tableString = new StringBuilder(); | |
List<int> colSize = GetMaxStringLength(); | |
// This handles and deals with formatting out rows and columns into something we can work with. | |
string rowFormat = Enumerable // Creates an Enumerable (Countable) | |
.Range(0, Columns.Count) // With a Range of 0-(Size of _columns) | |
.Select(size => " | {" + size + ",-" + colSize[size] + "}") // Loops through the range with the columns maximum length | |
.Aggregate((sum, nextValue) => sum + nextValue) + " |"; // Sums it all together | |
string columnHeaders = string.Format(rowFormat, Columns.ToArray()); // Creates the formatted Header string | |
List<string> values = Rows.Select(row => string.Format(rowFormat, row)).ToList(); // formats out rows and outputs them as a joint list of strings. | |
// Grabs the longest row length. | |
int maxRowSize = Math.Max(0, Rows.Any() ? Rows.Max(row => string.Format(rowFormat, row).Length) : 0); | |
// Finalizes the final table divider line size. | |
int maxColSize = Math.Max(maxRowSize, columnHeaders.Length); | |
string divLine = string.Join("", Enumerable.Repeat("-", Math.Max(maxColSize - 1, 0))); | |
string divider = $" {divLine} "; | |
// Time to build this Lego Table! | |
tableString.AppendLine(divider); // Adds the top divider line | |
tableString.AppendLine(columnHeaders); // adds the column header line | |
// Loops through all of our finalized row strings | |
foreach (var row in values){ | |
tableString.AppendLine(divider); // Adds a divider line | |
tableString.AppendLine(row); // Adds the content row string | |
} | |
tableString.AppendLine(divider); // adds an ending divider line | |
return tableString.ToString(); | |
} | |
// Prints the finalized table! | |
public void Print(){ | |
Console.WriteLine(ToString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment