Permalink
Cannot retrieve contributors at this time
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?
OPIM3220-CarInventory/VehicleInventory/FileHelper.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
49 lines (42 sloc)
1.51 KB
This file contains 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.IO; | |
using System.Linq; | |
using System.Text; | |
namespace VehicleInventory | |
{ | |
// This is a static class that is used to load the inventory from a text file | |
static class FileHelper | |
{ | |
// 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<string> LoadInventory(string filepath) | |
{ | |
// Skip the first line because it contains column headings | |
List<string> 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<Car> GetCars(string filepath) | |
{ | |
List<Car> result = new List<Car>(); | |
List<string> inventory = LoadInventory(filepath); | |
foreach (string vehicle in inventory) | |
{ | |
Car car = new Car(); | |
string[] carinfo = vehicle.Split(","); | |
car.InventoryNumber = carinfo[0]; | |
car.Year = Convert.ToDouble(carinfo[1]); | |
car.Make = carinfo[2]; | |
car.Model = carinfo[3]; | |
car.Mileage = Convert.ToDouble(carinfo[4]); | |
car.Price = Convert.ToDecimal(carinfo[5]); | |
result.Add(car); | |
} | |
return result; | |
} | |
} | |
} |