28
static class Program
{
    static void Main(string carMake, string carModel, string carColour, string bikeModel, string bikeMake, string bikeColour, string truckMake, string truckModel, string truckColour)
    {
        car Mynewcar = new car();
        motorbike Mynewbike = new motorbike();
        truck Mynewtruck = new truck();

        int choice = 0;
        while (choice != 5)
        {

            Console.WriteLine("MENU");
            Console.WriteLine("What service do you need");
            Console.WriteLine("1. Car");
            Console.WriteLine("2. Motorbike");
            Console.WriteLine("3. Truck");
            Console.WriteLine("4. Search");
            Console.WriteLine("5. Exit");

            choice = int.Parse(Console.ReadLine());

            switch (choice)
            {
                case 1:
                    Console.WriteLine("What is the car make?");
                    Mynewcar.make = Console.ReadLine().ToLower();
                    carMake = Console.ReadLine();
                    Console.WriteLine("");

                    Console.WriteLine("What is the car model?");
                    Mynewcar.model = Console.ReadLine().ToLower();
                    carModel = Console.ReadLine();
                    Console.WriteLine("");

                    Console.WriteLine("What is the car Colour?");
                    Mynewcar.colour = Console.ReadLine().ToLower();
                    carColour = Console.ReadLine();
                    Console.WriteLine("");

                    break;
                case 2:
                    Console.WriteLine("what is the motorbike make");
                    Mynewbike.make = Console.ReadLine().ToLower();
                    bikeMake = Console.ReadLine();
                    Console.WriteLine("");

                    Console.WriteLine("what is the motorbike model");
                    Mynewbike.model = Console.ReadLine().ToLower();
                    bikeModel = Console.ReadLine();
                    Console.WriteLine("");

                    Console.WriteLine("what is the motorbike colour");
                    Mynewbike.colour = Console.ReadLine().ToLower();
                    bikeColour = Console.ReadLine();
                    Console.WriteLine("");

                    break;
                case 3:
                    Console.WriteLine("what is the trucks make");
                    Mynewtruck.make = Console.ReadLine().ToLower();
                    truckMake = Console.ReadLine();
                    Console.WriteLine("");

                    Console.WriteLine("what is the trucks model");
                    Mynewtruck.model = Console.ReadLine().ToLower();
                    truckModel = Console.ReadLine();
                    Console.WriteLine("");

                    Console.WriteLine("what is the trucks colour");
                    Mynewtruck.colour = Console.ReadLine().ToLower();
                    truckColour = Console.ReadLine();
                    Console.WriteLine("");

                    break;
                case 4:
                    string searchchoice = "";
                    Console.WriteLine("select Car, Motobike or truck to search?");
                    searchchoice = Console.ReadLine().ToLower();
                    if (searchchoice.Equals("car"))
                    {
                        Console.WriteLine("Car in inventory: {0} - {1} - {2}", carMake, carModel, carColour);
                    }
                    else if (searchchoice.Equals("motorbike"))
                    {
                        Console.WriteLine("Motorbike in inventory: {0} - {1} - {2}", bikeMake, bikeModel, bikeColour);
                    }
                    else
                    {
                        Console.WriteLine("Trucks in inventory: {0} - {1} - {2}", truckMake, truckModel, truckColour);
                    }
                    Console.ReadLine();
                    break;
                case 5:
                    break;
                default:
                    Console.WriteLine("Sorry, invalid selection");
                    break;
            }
        }
    }

    class car
    {
        public string make { get; set; }
        public string model { get; set; }
        public string colour { get; set; }

        public List<String> carList(car Mynewcar)
        {
            List<String> caradd = new List<String>();
            caradd.Add(Mynewcar.make);
            string carMake = Mynewcar.make;

            caradd.Add(Mynewcar.model);
            string carModel = Mynewcar.model;

            caradd.Add(Mynewcar.colour);
            string carColour = Mynewcar.model;


            return caradd;
        }

    }
    class motorbike : car
    {
        public List<String> bikeList(motorbike Mynewbike)
        {
            List<String> bikeadd = new List<String>();
            bikeadd.Add(Mynewbike.model);
            string bikeModel = Mynewbike.model;

            bikeadd.Add(Mynewbike.make);
            string bikeMake = Mynewbike.make;

            bikeadd.Add(Mynewbike.colour);
            string bikeColour = Mynewbike.colour;

            return bikeadd;
        }
    }
    class truck : car
    {
        public List<String> truckList(truck Mynewtruck)
        {
            List<String> truckadd = new List<String>();
            truckadd.Add(Mynewtruck.make);
            string truckMake = Mynewtruck.make;

            truckadd.Add(Mynewtruck.model);
            string truckModel = Mynewtruck.model;

            truckadd.Add(Mynewtruck.colour);
            string truckColour = Mynewtruck.colour;

            return truckadd;
        }
    }
}

I was just wondering whether anyone could have a look through my code to see which areas I can improve on.

Also I am struggling to add more than one item to the lists for car, bike and truck. How would you do it so you can add multiple instances of the properties and list them to the console?

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
willa
  • 629
  • 1
  • 9
  • 17
  • 1
    For future reference, you do not need nearly this much code for such a question. Most of this has absolutely no relavence to the question. Code does help us, but succinct code is the ticket. – AustinWBryan Oct 26 '20 at 18:47

5 Answers5

51

Thanks to AddRange:

Example:

public class Person
{ 
    private string Name;
    private string FirstName;

    public Person(string name, string firstname) => (Name, FirstName) = (name, firstname);
}

To add multiple Person to a List<>:

List<Person> listofPersons = new List<Person>();
listofPersons.AddRange(new List<Person>
{
    new Person("John1", "Doe" ),
    new Person("John2", "Doe" ),
    new Person("John3", "Doe" ),
 });
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Malick
  • 6,252
  • 2
  • 46
  • 59
26

Code check:

This is offtopic here but the people over at CodeReview are more than happy to help you.

I strongly suggest you to do so, there are several things that need attention in your code. Likewise I suggest that you do start reading tutorials since there is really no good reason not to do so.

Lists:

As you said yourself: you need a list of items. The way it is now you only store a reference to one item. Lucky there is exactly that to hold a group of related objects: a List.

Lists are very straightforward to use but take a look at the related documentation anyway.

A very simple example to keep multiple bikes in a list:

List<Motorbike> bikes = new List<Motorbike>();

bikes.add(new Bike { make = "Honda", color = "brown" });
bikes.add(new Bike { make = "Vroom", color = "red" });

And to iterate over the list you can use the foreach statement:

foreach(var bike in bikes) {
     Console.WriteLine(bike.make);
}
Community
  • 1
  • 1
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
  • 1
    You can add it like var bikes = new List(); bikes.AddRange(new List{{new Bike { make = "Honda", color = "brown" }}, {new Bike { make = "Vroom", color = "red" }}}); – Shiljo Paulson Nov 20 '16 at 19:14
  • What if I have a list inside another class, like this: https://pastebin.com/aWhfu8UM. I get a `Run-time exception (line 9): Object reference not set to an instance of an object.` error. My instantiate look similar to what you have posted but mine is giving an error – Si8 May 11 '17 at 18:20
12

Another useful way is with Concat.
More information in the official documentation.

List<string> first = new List<string> { "One", "Two", "Three" };
List<string> second = new List<string>() { "Four", "Five" };
first.Concat(second);

The output will be.

One
Two
Three
Four
Five

And there is another similar answer.

Jesse R. Jose
  • 263
  • 5
  • 16
8

In modern C# using extension methods, generics, and the params keyword, you can create a generic method on the List class that allows you to add an indefinite number of items to a list containing any type:

static class ListExtensions 
{
  static public void Add<T>(this List<T> list, params T[] additions)
  {
    foreach(T addition in additions)
    {
      list.Add(addition)
    }
  }
}

Example usage using a list of strings and a list of integers:

var stringList = new List<string>();
stringList.Add("Hello", "World", "!");

var intList = new List<int>();
intList.Add(1, 2, 3, 4, 5);
Jordan
  • 868
  • 1
  • 8
  • 25
-2

AddRange is a good method, if you are using a string list this would be an easy way to add "aa","bb","cc","dd" to the list list.AddRange("aa,bb,cc,dd".Split(',').ToList())