diff --git a/VehicleInventory/Car.cs b/VehicleInventory/Car.cs index 4a38178..9b10598 100644 --- a/VehicleInventory/Car.cs +++ b/VehicleInventory/Car.cs @@ -3,11 +3,17 @@ using System; using System.Collections.Generic; using System.Text; +// You'll often see that programmers break their code into multiple files +// So long as the files are all held in the same namespace, they're accessible +// to the rest of your program with no extra work. + namespace VehicleInventory { - public class Car + // Declare a class called car + class Car { + // Set properties public string InventoryNumber; public double Year; public string Make; @@ -15,8 +21,10 @@ namespace VehicleInventory public double Mileage; public decimal Price; + // Declare a method to get vehicle details public string GetDetails() { + // String interpolation to describe the vehicle return $"{InventoryNumber} - {Year} {Make} {Model} - {Mileage} miles - {Price.ToString("C")}"; } } diff --git a/VehicleInventory/FileHelper.cs b/VehicleInventory/FileHelper.cs index 9ac16d8..8eacd5b 100644 --- a/VehicleInventory/FileHelper.cs +++ b/VehicleInventory/FileHelper.cs @@ -6,15 +6,23 @@ using System.Text; namespace VehicleInventory { + // This is a static class that is used to load the inventory from a text file static class FileHelper { - public static List LoadInventory(string filepath) + // This private function loads a text file into a list of strings based on its lines. + // It can be marked as private because it will only be called by the GetCars function + // which is accessible to other classes + private static List LoadInventory(string filepath) { + // Skip the first line because it contains column headings List result = File.ReadAllLines(filepath).ToList().Skip(1).ToList(); return result; } + // Take the result of the LoadInventory function, split the lines into an array, + // then map the array to a Car instance. Add the car instance to a list of things + // that will be returned, and once complete return them all to the calling function public static List GetCars(string filepath) { List result = new List(); diff --git a/VehicleInventory/Program.cs b/VehicleInventory/Program.cs index 42ee083..5300314 100644 --- a/VehicleInventory/Program.cs +++ b/VehicleInventory/Program.cs @@ -8,68 +8,107 @@ namespace VehicleInventory { static void Main(string[] args) { + // filepath will be unique to your computer. You must tell the program where the + // VehicleInventory.csv file can be found on your hard disk. string filepath = @"/Volumes/krb11010/3220/CarInventory-Complete/VehicleInventory.csv"; + + // Use our static FileHelper class defined in the FileHelper file to load our inventory List cars = FileHelper.GetCars(filepath); + // This is a neat trick to make a command line application never complete. We use a + // while loop with a condition that always evaluates to true (or in this case, is the + // keyword true. This way, or program operates inside an infinite loop, and only closes + // if we meet a condition that tells it to explicitly. while (true) { + // Clear the window Console.Clear(); + + // Provide instructions how to use the program Console.WriteLine("Input the number for what you're trying to search by:"); Console.WriteLine("1 - Search by manufacturer"); Console.WriteLine("2 - Search by year"); Console.WriteLine("3 - Search by mileage range"); Console.WriteLine("4 - Search by price range"); Console.WriteLine("10 - Exit"); + + // Get a response from the person for what they wish to do string action = Console.ReadLine(); + // SCOPE! We want to access our search results later, so we declare an empty list for them + // now, use our switch statement to fill them in, and process them after the switch is complete List searchResult = new List(); + + // Based on the value of our action variable (user's response), do one of the following: switch (action) { - case "1": + case "1": // Search by manufacturer + + // Ask for which manufacturer they're looking for? Console.WriteLine("What vehicle manufacturer is the customer interested in?"); string make = Console.ReadLine(); + // Use Linq to parse through the cars list, and retrieve only those entries where + // the Make property equals the value we have stored in the make variable. + // Linq uses a lambda expression to determine which entries should be taken. It is + // essentially passing a short function that is evaluated for each item in the cars + // list. "w" can be anything you want, it simply refers to the item in the list that + // is currently being evaluated. searchResult = cars.Where(w => w.Make == make).ToList(); break; - case "2": + case "2": // Search by year + // Same process as condition 1 Console.WriteLine("What year vehicle is the customer interested in?"); double year = Convert.ToDouble(Console.ReadLine()); searchResult = cars.Where(w => w.Year == year).ToList(); break; - case "3": + case "3": // Search by mileage range Console.WriteLine("What is the minimum number of acceptable miles?"); double mileMinimum = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("What is the maximum number of acceptable miles?"); double mileMaximum = Convert.ToDouble(Console.ReadLine()); + // Essentially the same as the first case, but this time we have to verify that the + // results are greater than the minimum AND (&&) below the maximum. searchResult = cars.Where(w => w.Mileage >= mileMinimum && w.Mileage <= mileMaximum).ToList(); break; - case "4": + case "4": // Search by price range Console.WriteLine("What is the minimum price the customer wants to pay?"); decimal priceMinimum = Convert.ToDecimal(Console.ReadLine()); Console.WriteLine("What is the maximum price the customer wants to pay?"); decimal priceMaximum = Convert.ToDecimal(Console.ReadLine()); + // Same idea as case 3 searchResult = cars.Where(w => w.Price >= priceMinimum && w.Price <= priceMaximum).ToList(); break; - case "10": + case "10": // The person wants to exit the program + // Environment is a static class that represents the current computing "environment" and + // contains a method that lets you exit the program. When your program exits, it should + // specify an exit code, and 0 means "Program exited intentionally" Environment.Exit(0); break; default: + // This code runs if none of the above cases are met. We can pretty safely assume + // our user input a bad value. Console.WriteLine("Please input a valid response. Hit return to try again..."); Console.ReadLine(); - continue; // Start over, don't display results + + // Start the loop over because we have no results to display and we need to ask what to do again + continue; } + // Did we find results? if (searchResult.Count > 0) { + // We did, lets show them Console.Clear(); Console.WriteLine("Search Results"); Console.WriteLine(); + // Loop through the results, using the GetDetails method on the Car class to print out details foreach (Car car in searchResult) { Console.WriteLine(car.GetDetails()); @@ -79,9 +118,13 @@ namespace VehicleInventory Console.WriteLine("Press return to search again"); Console.ReadLine(); } - - // else No results, try again - + else + { + // No results were found, let the user know and allow them to try again. + Console.WriteLine("No results were found, sorry!"); + Console.WriteLine("Press return to search again"); + Console.ReadLine(); + } } } }