Home > Back-end >  Array list show in console all the users added C#
Array list show in console all the users added C#

Time:10-26

Hi im trying to show all my list once the user adds all the users he wants but im getting this output.

Choose one option:
1
New user:
Insert data of the user:
Name:
Daniel
Score:
50
Status Course:
Fail
Choose one option:
1
New user:
Insert data of the user:
Name:
Jake
Score:
100
Status Course:
Pass
Choose one option:
2
There are 2 Users
1 . ElMaestro.User
2 . ElMaestro.User

I have 3 differents classes the main class called Program

namespace ElMaestro
{
    class Program
    {
        private static UserAdd userAdd = new UserAdd();
        static void Main(string[] args)
        {
            Boolean exit = false;
            int options = 0;
            showOptions();

            while (!exit)
            {
                Console.WriteLine("Choose one option: ");
                options = Convert.ToInt32(Console.ReadLine());

                switch (options)
                {
                    case 0:
                        showOptions();
                        break;
                    case 1:
                        addUser();
                        break;
                    case 2:
                        usuarioAdd.printList();
                        break;
                    case 3:
                        exit = true;
                        break;
                }
            }
        }

        public static void showOptions()
        {
            Console.WriteLine("\b Choose one option: ");
            Console.WriteLine("\t 0 - Print options.");
            Console.WriteLine("\t 1 - Add user.");
            Console.WriteLine("\t 2 - List all users.");
            Console.WriteLine("\t 3 - Salir de la aplicacion.");
        }

        public static void addUser()
        {
            Console.WriteLine("New user: ");
            userAdd.User();
        }
    }
}

My class called UserAdd where my list is created and have the respective methods:

namespace ElMaestro
{
    internal class UserAdd
    {
        private List<User> list = new List<User>();

        public void User()
        {
            addData();
           
        }

        public void printList()
        {
            Console.WriteLine("There are "   list.Count " Users ");
            for(int i = 0; i < list.Count; i  )
            {
                Console.WriteLine((i   1)   " . "   list[i]);
                
            }
        }

        public void addData()
        {
            Console.WriteLine("Insert data of the user: ");

            String name = "";
            int score = 0;
            String course = "";

            Console.WriteLine("Name: ");
            name = Console.ReadLine();
            Console.WriteLine("Score: ");
            score = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Status Course: ");
            course = Console.ReadLine(); 

            list.Add(new User(name, score, course));
        }
    }
}

And my las class called User:

namespace ElMaestro
{
    internal class User
    {
        private String name;
        private int score;
        private String course;

        public User(String name, int score, String course)
        {
            this.name = name;
            this.score = score;
            this.course = course;
        }

        public void display()
        {
            Console.WriteLine("Nombre: "   name);
            Console.WriteLine("Puntaje del curso: "   score);
            Console.WriteLine("Estado del curso: "   course);
        }

        public String getName()
        {
            return name;
        }

    }
}

I know that for printing a list you need to use a for loop but im not getting the info that i want to show.

CodePudding user response:

When you print an object, the program transforms the object to a string, by default, this conversion returns the class name of the object, in this case "ElMaestro.User", the best practice in this case is to override the method toString() in the User class, i.e. like this

public override string ToString()
{
    return "User: "   name   " "   score   " "   course;
}

You can learn more about this topic in https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/how-to-override-the-tostring-method

  • Related