Skip to content
Permalink
0789231091
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
131 lines (113 sloc) 5.69 KB
using System;
using System.Collections.Generic;
using System.Linq;
namespace VehicleInventory
{
class Program
{
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<Car> 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<Car> searchResult = new List<Car>();
// Based on the value of our action variable (user's response), do one of the following:
switch (action)
{
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": // 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": // 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": // 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": // 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();
// 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());
}
Console.WriteLine();
Console.WriteLine("Press return to search again");
Console.ReadLine();
}
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();
}
}
}
}
}