none
I have a List<Cars> and I wish to edit the data within using the cars reg? RRS feed

  • Question

  • I have a pre-existing file called inventory.txt which contains the make, model, type, year and reg of every car. I'd like the user to be able to edit the items in the text file after entering the car registration number (reg) eg. 101 and 

    If the reg matches a reg in the list<Car>, the details of the car. ie the make, model, and year will show up in the console window. And then the user should be able to edit these details.

    I was also wondering if it would be possible to create a text file detailing the edits made to the list.

    I'm new to c# so I have no idea where to begin to do this. Any help on this issue would be appreciated. Thank You.

    My code for the car Class:

    private string _make;
    private string _model;
    private int _year;
    private int _reg;
    private static int cohortSize = 0;
    
    class Car
        {
            public string Make
            {
                get
                {
                    return _make;
                }
    
                set
                {
                    _make = value;
                }
            }
    
            public string Model
            {
                get
                {
                    return _model;
                }
    
                set
                {
                    _model = value;
                }
            }
    
            public int Year
            {
                get
                {
                    return _year;
                }
    
                set
                {
                    _year = value;
                }
            }
    
            public int Reg
            {
                get
                {
                    return _reg;
                }
    
                set
                {
                    _reg = value;
                }
            }
    
    public Car(string make, string type, string model, int year, int reg)
            {
                Make = make;
                Type = type;
                Model = model;
                Year = year;
                Reg = reg;
            }
        }
    }
    namespace ConsoleApplication1
    {
        class Program
        {
    
            static List<Car> Cars = new List<Car>();
    
    static void Main(string[] args)
            { 
            getInventory();
            console.Readline();
            }
    
    static void getInventory() //method to get car info
            {
                //read the file into a text string
                string[] lines = System.IO.File.ReadAllLines(@"cars.txt");
    
                int invCounter = 0;
    
                int invLength = lines.Length / 8; //works out the number of carsin the inventory.txt file
    
                for (int i = 0; i <= invLength; i++) //for each car
                {
                    string make = lines[i + invCounter++]; //store the make
                    string type = lines[i + invCounter++]; //store the type
                    string model = lines[i + invCounter++]; //store the model
                    int year = int.Parse(lines[i + invCounter++]); //store the year
                    int reg = int.Parse(lines[i + invCounter++]); //store the reg
    
                    Car cr = new Car(make, type, model, year, reg); //create a new car object
    
                    Cars.Add(cr); //add the car to Cars list
                }
            }
        }
    }







    Thursday, April 11, 2019 3:17 PM

Answers

  • Here is a mock up w/o the text file, you should read/learn and apply in your project.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                var input = Console.ReadLine();
                if (int.TryParse(input, out var regValue))
                {
                    var carItem = GetCars().FirstOrDefault(car => car.Reg == regValue);
                    if (carItem != null)
                    {
                        Console.WriteLine(carItem.ToString());
                    }
                    else
                    {
                        Console.WriteLine("Not found");
                    }
                }
                else
                {
                    Console.WriteLine("Invalid input");
                }
    
                Console.ReadLine();
            }
    
            static List<Car> GetCars()
            {
                return new List<Car>()
                {
                    new Car() { Reg = 100, Make = "AAA", Model = "A1", Year = 2019},
                    new Car() { Reg = 101, Make = "BBB", Model = "B1", Year = 2018},
                    new Car() { Reg = 102, Make = "CCC", Model = "C1", Year = 2018}
                };
            }
        }
    
        class Car
        {
            public string Make { get; set; }
            public string Model { get; set; }
            public int Year { get; set; }
            public int Reg { get; set; }
            public override string ToString()
            {
                return $"{Reg}, {Make}, {Model},{Year}";
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, April 11, 2019 7:45 PM
    Moderator

All replies

  • Hello,

    We require you to present your code effort first before offering assistance. Also we have no idea what "reg" means nor details for a car.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, April 11, 2019 5:05 PM
    Moderator
  • Thank you. I've displayed my Car class on the original post.
    Thursday, April 11, 2019 5:24 PM
  • Here is a mock up w/o the text file, you should read/learn and apply in your project.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                var input = Console.ReadLine();
                if (int.TryParse(input, out var regValue))
                {
                    var carItem = GetCars().FirstOrDefault(car => car.Reg == regValue);
                    if (carItem != null)
                    {
                        Console.WriteLine(carItem.ToString());
                    }
                    else
                    {
                        Console.WriteLine("Not found");
                    }
                }
                else
                {
                    Console.WriteLine("Invalid input");
                }
    
                Console.ReadLine();
            }
    
            static List<Car> GetCars()
            {
                return new List<Car>()
                {
                    new Car() { Reg = 100, Make = "AAA", Model = "A1", Year = 2019},
                    new Car() { Reg = 101, Make = "BBB", Model = "B1", Year = 2018},
                    new Car() { Reg = 102, Make = "CCC", Model = "C1", Year = 2018}
                };
            }
        }
    
        class Car
        {
            public string Make { get; set; }
            public string Model { get; set; }
            public int Year { get; set; }
            public int Reg { get; set; }
            public override string ToString()
            {
                return $"{Reg}, {Make}, {Model},{Year}";
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, April 11, 2019 7:45 PM
    Moderator