From f3f3f8a3de55fcbb014f34bb8b87f41115eed908 Mon Sep 17 00:00:00 2001 From: Shayn Coffey Date: Tue, 17 Jun 2025 20:59:17 -0400 Subject: [PATCH 1/2] Excercise completed --- LoggingKata.Test/TacoParserTests.cs | 27 ++++++--- LoggingKata/Program.cs | 86 +++++++++++++++++++++-------- LoggingKata/TacoBell.cs | 26 +++++++++ LoggingKata/TacoParser.cs | 60 +++++++++++++------- 4 files changed, 147 insertions(+), 52 deletions(-) create mode 100644 LoggingKata/TacoBell.cs diff --git a/LoggingKata.Test/TacoParserTests.cs b/LoggingKata.Test/TacoParserTests.cs index eccce48e..e473f0f7 100644 --- a/LoggingKata.Test/TacoParserTests.cs +++ b/LoggingKata.Test/TacoParserTests.cs @@ -1,18 +1,25 @@ using System; +using System.Collections.Generic; using Xunit; + namespace LoggingKata.Test { public class TacoParserTests { + private IEnumerable expected; + private object line; + private object tacoParserInstance; + + [Fact] public void ShouldReturnNonNullObject() { - //Arrange - var tacoParser = new TacoParser(); + //Arrange -write the code we need in order to call the method we're testing + var tacoParserInstance = new TacoParser(); //Act - var actual = tacoParser.Parse("34.073638, -84.677017, Taco Bell Acwort..."); + var actual = tacoParserInstance.Parse("34.073638, -84.677017, Taco Bell Acwort..."); //Assert Assert.NotNull(actual); @@ -20,25 +27,27 @@ public void ShouldReturnNonNullObject() } [Theory] - [InlineData("34.073638, -84.677017, Taco Bell Acwort...", -84.677017)] + [InlineData("34.073638, -84.677017, Taco Bell Acwort...", 34.073638)] //Add additional inline data. Refer to your CSV file. - public void ShouldParseLongitude(string line, double expected) + public void ShouldParseLat(string line, double expected) { - // TODO: Complete the test with Arrange, Act, Assert steps below. + // Done: Complete the test with Arrange, Act, Assert steps below. // Note: "line" string represents input data we will Parse // to extract the Longitude. // Each "line" from your .csv file // represents a TacoBell location - //Arrange + //Arrange - write the code we need in order to call the method we're testing + var tacoParserInstance = new TacoParser(); //Act - + var actual = tacoParserInstance.Parse(line); //Assert + Assert.Equal(expected, actual.Location.Latitude); } - //TODO: Create a test called ShouldParseLatitude + //Done: Create a test called ShouldParseLatitude } } diff --git a/LoggingKata/Program.cs b/LoggingKata/Program.cs index bccf9eba..0367b34e 100644 --- a/LoggingKata/Program.cs +++ b/LoggingKata/Program.cs @@ -2,12 +2,16 @@ using System.Linq; using System.IO; using GeoCoordinatePortable; +using System.Collections.Generic; namespace LoggingKata { class Program { static readonly ILog logger = new TacoLogger(); + private static ITrackable locB; + private static GeoCoordinate corB; + private static double distance; const string csvPath = "TacoBell-US-AL.csv"; static void Main(string[] args) @@ -19,7 +23,20 @@ static void Main(string[] args) // Use File.ReadAllLines(path) to grab all the lines from your csv file. // Optional: Log an error if you get 0 lines and a warning if you get 1 line - var lines = File.ReadAllLines(csvPath); + string[] lines = File.ReadAllLines(csvPath); + if (lines.Length == 0) + { + logger.LogError("file has no input"); + + } + + if (lines.Length == 1) + { + logger.LogWarning("file only has one line of input"); + + } + + // This will display the first item in your lines array logger.LogInfo($"Lines: {lines[0]}"); @@ -28,45 +45,68 @@ static void Main(string[] args) var parser = new TacoParser(); // Use the Select LINQ method to parse every line in lines collection - var locations = lines.Select(parser.Parse).ToArray(); + var locations = lines.Select(line => parser.Parse(line)).ToArray(); - // Complete the Parse method in TacoParser class first and then START BELOW ---------- - // TODO: Create two `ITrackable` variables with initial values of `null`. + // Done: Create two `ITrackable` variables with initial values of `null`. // These will be used to store your two Taco Bells that are the farthest from each other. - - // TODO: Create a `double` variable to store the distance - // TODO: Add the Geolocation library to enable location comparisons: using GeoCoordinatePortable; - // Look up what methods you have access to within this library. + // Done: Create a `double` variable to store the distance + + ITrackable tacoBell1 = null; + ITrackable tacoBell2 = null; + double distant = 0; + + + // Done: Add the Geolocation library to enable location comparisons: using GeoCoordinatePortable; // NESTED LOOPS SECTION---------------------------- - - // FIRST FOR LOOP - - // TODO: Create a loop to go through each item in your collection of locations. - // This loop will let you select one location at a time to act as the "starting point" or "origin" location. - // Naming suggestion for variable: `locA` - // TODO: Once you have locA, create a new Coordinate object called `corA` with your locA's latitude and longitude. - // SECOND FOR LOOP - - // TODO: Now, Inside the scope of your first loop, create another loop to iterate through locations again. - // This allows you to pick a "destination" location for each "origin" location from the first loop. - // Naming suggestion for variable: `locB` + for (int i = 0; i < locations.Length; i++) + { + // Do a loop for your locations to grab each location as the origin (perhaps: 'locA') + var locA = locations[i]; + + // Create a new corA Coordinate with your locA's lat and long + var corA = new GeoCoordinate(); + corA.Latitude = locA.Location.Latitude; + corA.Longitude = locA.Location.Longitude; + + // Now do another loop on the locations with the scope of your first loop, so you can grab the "destination" loca + for (int j = 0; j < locations.Length; j++) + { + // Create a new Coordinate with your locB's lat and long + + // Now, compare the two using '.GetDistanceTo()', which returns a double + + var locB = locations[j]; + var corB = new GeoCoordinate(); + corB.Latitude = locB.Location.Latitude; + corB.Longitude = locB.Location.Longitude; + } + + + // Now, compare the two using '.GetDistanceTo()', which returns a double - // TODO: Once you have locB, create a new Coordinate object called `corB` with your locB's latitude and longitude. + // If the distance is greater than the currently saved distance, update the distance and the two 'ITrackable' vari + if (corA.GetDistanceTo(corB) > distance) + { + distant = corA.GetDistanceTo(corB); + tacoBell1 = locA; + tacoBell2 = locB; - // TODO: Now, still being inside the scope of the second for loop, compare the two locations using `.GetDistanceTo()` method, which returns a double. - // If the distance is greater than the currently saved distance, update the distance variable and the two `ITrackable` variables you set above. + } + } // NESTED LOOPS SECTION COMPLETE --------------------- // Once you've looped through everything, you've found the two Taco Bells farthest away from each other. // Display these two Taco Bell locations to the console. + logger.LogInfo($"{tacoBell1.Name} and {tacoBell2.Name} are the farthest apart"); - } } -} +} \ No newline at end of file diff --git a/LoggingKata/TacoBell.cs b/LoggingKata/TacoBell.cs new file mode 100644 index 00000000..3687a74f --- /dev/null +++ b/LoggingKata/TacoBell.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LoggingKata +{ + public class TacoBell : ITrackable + { + public TacoBell() + { + } + + public string Name { get; set; } + public Point Location { get; set; } + + + + + } + + + + +} \ No newline at end of file diff --git a/LoggingKata/TacoParser.cs b/LoggingKata/TacoParser.cs index 21c6b731..c4f07796 100644 --- a/LoggingKata/TacoParser.cs +++ b/LoggingKata/TacoParser.cs @@ -1,4 +1,7 @@ -namespace LoggingKata +using System; +using System.Net.Http.Headers; + +namespace LoggingKata { /// /// Parses a POI file to locate all the Taco Bells @@ -6,7 +9,7 @@ public class TacoParser { readonly ILog logger = new TacoLogger(); - + public ITrackable Parse(string line) { logger.LogInfo("Begin parsing"); @@ -18,35 +21,52 @@ public ITrackable Parse(string line) if (cells.Length < 3) { // Log error message and return null - return null; + logger.LogWarning("less than three items. incomplete data"); } - // TODO: Grab the latitude from your array at index 0 + // grab the latitude from your array at index 0 + var latitude = double.Parse(cells[0]); // You're going to need to parse your string as a `double` // which is similar to parsing a string as an `int` - - - // TODO: Grab the longitude from your array at index 1 - // You're going to need to parse your string as a `double` + + + // grab the longitude from your array at index 1 + var longitude = double.Parse(cells[1]); + + // Done You're going to need to parse your string as a `double` // which is similar to parsing a string as an `int` - - - // TODO: Grab the name from your array at index 2 - - // TODO: Create a TacoBell class + + // grab the name from your array at index 2 + var name = cells[2]; + + + // Done: Create a TacoBell class // that conforms to ITrackable - - // TODO: Create an instance of the Point Struct - // TODO: Set the values of the point correctly (Latitude and Longitude) - // TODO: Create an instance of the TacoBell class - // TODO: Set the values of the class correctly (Name and Location) - // TODO: Then, return the instance of your TacoBell class, + // Done: Create an instance of the Point Struct + // Done: Set the values of the point correctly (Latitude and Longitude) + + // DOne: Create an instance of the TacoBell class + // Done: Set the values of the class correctly (Name and Location) + var point = new Point(); + point.Latitude = latitude; + point.Longitude = longitude; + + var tacoBell = new TacoBell(); + tacoBell.Name = name; + tacoBell.Location = point; + + // Done: Then, return the instance of your TacoBell class, // since it conforms to ITrackable - return null; + return tacoBell; + } + + public object Parse(object line) + { + throw new NotImplementedException(); } } } From f54ec06ac9d28f1d03e24e86bdcffa6fb18d8ea5 Mon Sep 17 00:00:00 2001 From: Shayn Coffey Date: Mon, 23 Jun 2025 19:50:43 -0400 Subject: [PATCH 2/2] exercise completed --- LoggingKata.Test/TacoParserTests.cs | 26 ++++++++++++++++++++++++-- LoggingKata/Program.cs | 16 +++++++++------- LoggingKata/TacoParser.cs | 5 +---- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/LoggingKata.Test/TacoParserTests.cs b/LoggingKata.Test/TacoParserTests.cs index e473f0f7..85be7943 100644 --- a/LoggingKata.Test/TacoParserTests.cs +++ b/LoggingKata.Test/TacoParserTests.cs @@ -20,7 +20,7 @@ public void ShouldReturnNonNullObject() //Act var actual = tacoParserInstance.Parse("34.073638, -84.677017, Taco Bell Acwort..."); - + //Assert Assert.NotNull(actual); @@ -28,6 +28,11 @@ public void ShouldReturnNonNullObject() [Theory] [InlineData("34.073638, -84.677017, Taco Bell Acwort...", 34.073638)] + [InlineData("30.39371,-87.68332,Taco Bell Fole...", 30.39371)] + [InlineData("34.8831,-84.293899,Taco Bell Blue Ridg...", 34.8831)] + [InlineData("32.571331,-85.499655,Taco Bell Auburn...", 32.571331)] + + //Add additional inline data. Refer to your CSV file. public void ShouldParseLat(string line, double expected) { @@ -46,8 +51,25 @@ public void ShouldParseLat(string line, double expected) Assert.Equal(expected, actual.Location.Latitude); } + + - //Done: Create a test called ShouldParseLatitude + //Done: Create a test called ShouldParseLongitude + public void ShouldParseLong(string line, double expected) + { + //Arrange - write the code we need in order to call the method we're testing + var tacoParserInstance = new TacoParser(); + //Act + var actual = tacoParserInstance.Parse(line); + //Assert + Assert.Equal(expected, actual.Location.Longitude); + + + + + + + } } } diff --git a/LoggingKata/Program.cs b/LoggingKata/Program.cs index 0367b34e..c8bb62a6 100644 --- a/LoggingKata/Program.cs +++ b/LoggingKata/Program.cs @@ -24,7 +24,7 @@ static void Main(string[] args) // Use File.ReadAllLines(path) to grab all the lines from your csv file. // Optional: Log an error if you get 0 lines and a warning if you get 1 line string[] lines = File.ReadAllLines(csvPath); - if (lines.Length == 0) + //if (lines.Length == 0) { logger.LogError("file has no input"); @@ -85,19 +85,21 @@ static void Main(string[] args) var corB = new GeoCoordinate(); corB.Latitude = locB.Location.Latitude; corB.Longitude = locB.Location.Longitude; + + if (corA.GetDistanceTo(corB) > distance) + { + distant = corA.GetDistanceTo(corB); + tacoBell1 = locA; + tacoBell2 = locB; + + } } // Now, compare the two using '.GetDistanceTo()', which returns a double // If the distance is greater than the currently saved distance, update the distance and the two 'ITrackable' vari - if (corA.GetDistanceTo(corB) > distance) - { - distant = corA.GetDistanceTo(corB); - tacoBell1 = locA; - tacoBell2 = locB; - } } // NESTED LOOPS SECTION COMPLETE --------------------- diff --git a/LoggingKata/TacoParser.cs b/LoggingKata/TacoParser.cs index c4f07796..212cd17b 100644 --- a/LoggingKata/TacoParser.cs +++ b/LoggingKata/TacoParser.cs @@ -64,9 +64,6 @@ public ITrackable Parse(string line) return tacoBell; } - public object Parse(object line) - { - throw new NotImplementedException(); - } + } }