Home > OS >  C#: Is there a way to return a side by side array with commas after what you input on a list on the
C#: Is there a way to return a side by side array with commas after what you input on a list on the

Time:11-14

I am new to C#. This is my code. When I run this code, I input the car name, mileage, year, make, model, color, and bodytype to a list that looks like this

* Name of Car: Car 1
 Enter Mileage: 4000 (my input)
 Enter year: 2022 (my input)
 Enter make: ford (my input, etc.)
 Enter model: fusion
 Enter color: red
 Enter bodytype: coupe
 Name of Car: Car 2 
 Enter Mileage: 4000
 Enter year: 2022
 Enter make: ford
 Enter model: fusion
 Enter color: red
 Enter bodytype: coupe
 Name of Car: Car 3 
 Enter Mileage: 4000
 Enter year: 2022
 Enter make: ford
 Enter model: fusion
 Enter color: red
 Enter bodytype: coupe*

and I want to return what I input for car 1, car 2, car 3 as three side by side arrays with commas looking like this?

*car 1, 4000, 2022, ford, fusion, red, coupe
car 2, 4000, 2022, ford, fusion, red, coupe
car 3, 4000, 2022, ford, fusion, red coupe*

Below is what I wrote for code, at the very end of this code is where I want to print the side by side arrays. Applying concepts of objects and methods, what should I write in order print that side by side array on the console window?

using System;
using System.Management.Instrumentation;
using Assignment5;

namespace Assignment5
{
    public class Car
    {
        //public:
        public void Set_Name_of_Car(string name)
        {
            name_of_car = name;
        }

        public void Set_Body_Type(string Type) //methods that you can call 
        {
            bodytype = Type; 
        }

        public void Set_Make(string Color, string Make, string Model )
        {
            color = Color;
            make = Make;
            model = Model;
        }

        public void Set_Mileage(int Mileage, int Year)
        {
            mileage = Mileage;
            year = Year; 
        }

        public void Show()
        {
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
        }
    }

}
namespace Program
{ 
    internal class Program
    {
        static void Main(string[] args)
        {
                Car car1 = carInfo();
                Car car2 = carInfo();
                Car car3 = carInfo();
        }

        public static Car  carInfo()
        {
            Console.WriteLine("Enter Name of Car: ");
            string n_Aim = Console.ReadLine();
            Console.WriteLine("Enter Mileage:");
            int miles = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter Year:");
            int yr = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter Make:");
            string meigh_k = Console.ReadLine();
            Console.WriteLine("Enter Model:");
            string mod_L = Console.ReadLine();
            Console.WriteLine("Enter Color:");
            string Col_R = Console.ReadLine();
            Console.WriteLine("Enter Bodytype: ");
            string Bdy_Typ = Console.ReadLine();

            Car inputcar = new Car();
            inputcar.Set_Name_of_Car(n_Aim); 
            inputcar.Set_Mileage(miles, yr);
            inputcar.Set_Make(Col_R, meigh_k, mod_L); 
            inputcar.Set_Body_Type(Bdy_Typ);
            return inputcar;
        }
    }
}

What I want to try is using but I don't think my instructor wants us to use that method because it's too simplistic. Do you have any other suggestions please? and thank you :D

Console.WriteLine("car 1, 4000, 2022, ford, fusion, red, coupe");
Console.WriteLine("car 2, 4000, 2022, ford, fusion, red, coupe");
Console.WriteLine("car 3, 4000, 2022, ford, fusion, red coupe");

CodePudding user response:

override the tostring function for said class

namespace Assignment5
{
    public class Car
    {
        //public:
        public void Set_Name_of_Car(string name)
        {
            name_of_car = name;
        }
        public void Set_Body_Type(string Type) //methods that you can call 
        {
            bodytype = Type; 
        }

        public void Set_Make(string Color, string Make, string Model )
        {
            color = Color;
            make = Make;
            model = Model;
        }

        public void Set_Mileage(int Mileage, int Year)
        {
            mileage = Mileage;
            year = Year; 
        }

        public void Show()
        {
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
        }

        public override string ToString()
        {
           return name_of_car   ", "   mileage   ", "   year   ", "   make    ", "   model    ", "   color   ", "   bodytype 
        }
    }
    
}

then just print the string for each car

static void Main(string[] args)
        {
                Car car1 = carInfo();
                Car car2 = carInfo();
                Car car3 = carInfo();
                Console.WriteLine(car1.ToString());
                Console.WriteLine(car2.ToString());
                Console.WriteLine(car3.ToString());
        }

you can also do this as it will automatically call the ToString method

static void Main(string[] args)
        {
                Car car1 = carInfo();
                Car car2 = carInfo();
                Car car3 = carInfo();
                Console.WriteLine(car1);
                Console.WriteLine(car2);
                Console.WriteLine(car3);
        }

CodePudding user response:

You can override the ToString method of Car that every class inherits from object to let a car display itself. In the car class, add this method:

public override string ToString()
{
    return $"{name_of_car}, {mileage}, {year}, {make}, {model}, {color}, {bodytype}";
}

You also better add the cars to a list. This simplifies the handling of cars, as you can easily process any number of cars now, without having to add more car variables.

var cars = new List<Car>();
cars.Add(carInfo());
cars.Add(carInfo());
cars.Add(carInfo());

foreach (Car car in cars) {
    Console.WriteLine(car);
}

Note that Console.WriteLine automatically uses the ToString method to display any objects.

The Show method in Car is no more required. Input/output (Console.ReadLine/WriteLine) should be done in a data class.

You could also have the user input cars until he presses Enter without entering a name. To do this return null from the input method:

public static Car  carInfo()
{
    Console.WriteLine("Enter Name of Car: ");
    string n_Aim = Console.ReadLine();
    if (n_Aim = "") {
        return null;
    }
    Console.WriteLine("Enter Mileage:");
    ...
}

and then

var cars = new List<Car>();
while (true) {
    Car car = carInfo();
    if (car == null) {
        break; // Exit the loop
    }
    cars.Add(carInfo());
}

foreach (Car car in cars) {
    Console.WriteLine(car);
}
  • Related