1

I am new at programming in C# and I'm trying to create a method that allows me to create a table and visualize the data inside of a matrix. However none of the methods I have tried seems to work as supposed.

Basically the method works to visualize the header which is the Enum, however I need to read the data from the matrix and the output has to be in the form of a table. Here´s the code so far:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{
    enum clientHeader { Id, Name, Surname, Addres, Cod_Postal, Telephone, Email, State };

    class Program
    {
        static void Main(string[] args)
        {
            string[,] client = new string[3, 7];

            insertData<clientHeader>(client);
            Console.Clear();
            showHeader<clientHeader>(client);

            listData<clientHeader>(client); 

            Console.ReadKey();
        }

       static void insertData<T>(string[,] matrix)
       {
            int x = matrix.GetLowerBound(1);
            matrix[0, x] = "1";

            for (int j = 1; j < matrix.GetLength(1); j++)
            {
                do
                {
                    Console.Write($"\nInsert {GetHeader<T>(j)}: ");
                    matrix[0, j] = Console.ReadLine();
                } while (String.IsNullOrEmpty(matrix[0, j]));
            }
        }

        static void listData<T>(string[,] matrix)
        {
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    Console.Write($"{matrix[i, j]}\t");
                }
            }

            Console.WriteLine();
        }
    }

    private static string GetHeader<T>(int i) => Enum.GetName(typeof(T), i);

        static void showHeader<T>(string[,] matrix)
        {
            int x = matrix.GetUpperBound(1);
            string[] array = new string[x];

            for (int i = 0; i < array.Length; i++)
            {
                array[i] = GetHeader<T>(i);
            }

            PrintLine();
            PrintRow(array);
            PrintLine();
        }

        static int tableWidth = 120;

        static void PrintLine()
        {
            Console.WriteLine(new string('-', tableWidth));
        }

        static void PrintRow(params string[] columns)
        {
            int width = (tableWidth - columns.Length) / columns.Length;
            string row = "|";

            foreach (string column in columns)
            {
                row += AlignCentre(column, width) + "|";
            }

            Console.WriteLine(row);
        }

        static string AlignCentre(string text, int width)
        {
            text = text.Length > width ? text.Substring(0, width - 3) + "..." : text;

            if (string.IsNullOrEmpty(text))
            {
                return new string(' ', width);
            }
            else
            {
                return text.PadRight(width - (width - text.Length) / 2).PadLeft(width);
            }
        }
    }
}
3
  • The code on the main (ListData) is supossed to read the matrix and display it such the Enum is being shown as output Commented Aug 17, 2018 at 23:42
  • 1
    This question is too broad - please edit it and ask a specific question. What is the output you're expecting, what are you seeing instead, and what part of the code is problematic. "none of the methods I have tried seems to work as supposed" does not tell us what the problem is. Commented Aug 17, 2018 at 23:59
  • the method ListData<T>(string[,] matrix) was meant to read multiples lines from a large matrix Commented Aug 18, 2018 at 10:43

2 Answers 2

2

In the code below, the main modification I made was to have your ListData method act like your ShowHeader method, so that the data is aligned in columns underneath the header:

private static void ListData<T>(string[,] matrix)
{
    var array = new string[matrix.GetUpperBound(1)];

    for (var i = 0; i < array.Length; i++)
    {
        array[i] = matrix[0, i];
    }

    PrintRow(array);
    PrintLine();
}

See if that works for you. Here's the full code with some other refactoring:

private static void InsertData<T>(string[,] matrix)
{
    // Just add dummy data while testing
    matrix[0, 0] = "1";
    matrix[0, 1] = "FirstName";
    matrix[0, 2] = "LastName";
    matrix[0, 3] = "123 Main St";
    matrix[0, 4] = "98765";
    matrix[0, 5] = "(123) 456-7890";
    matrix[0, 6] = "[email protected]";
    return;

    matrix[0, matrix.GetLowerBound(1)] = "1";

    for (var j = 1; j < matrix.GetLength(1); j++)
    {
        do
        {
            Console.Write($"\nInsert {GetHeader<T>(j)}: ");
            matrix[0, j] = Console.ReadLine();
        } while (string.IsNullOrEmpty(matrix[0, j]));
    }
}

private static void ListData<T>(string[,] matrix)
{
    var array = new string[matrix.GetUpperBound(1)];

    for (var i = 0; i < array.Length; i++)
    {
        array[i] = matrix[0, i];
    }

    PrintRow(array);
    PrintLine();
}

private static string GetHeader<T>(int i) => Enum.GetName(typeof(T), i);

private static void ShowHeader<T>(string[,] matrix)
{
    var array = new string[matrix.GetUpperBound(1)];

    for (var i = 0; i < array.Length; i++)
    {
        array[i] = GetHeader<T>(i);
    }

    PrintLine();
    PrintRow(array);
    PrintLine();
}

private static void PrintLine()
{
    Console.WriteLine(new string('-', Console.WindowWidth - 1));
}

private static void PrintRow(IReadOnlyCollection<string> columns)
{
    var width = (Console.WindowWidth - 1 - columns.Count) / columns.Count;
    var row = columns.Aggregate("|", (current, column) => current + AlignCentre(column, width) + "|");
    Console.WriteLine(row);
}

static string AlignCentre(string text, int width)
{
    text = text.Length > width ? text.Substring(0, width - 3) + "..." : text;

    return string.IsNullOrEmpty(text)
        ? new string(' ', width)
        : text.PadRight(width - (width - text.Length) / 2).PadLeft(width);
}

enum ClientHeader { Id, Name, Surname, Addres, CodPostal, Telephone, Email, State };

private static void Main()
{
    var client = new string[3, 7];
    InsertData<ClientHeader>(client);
    Console.Clear();
    ShowHeader<ClientHeader>(client);
    ListData<ClientHeader>(client);

    GetKeyFromUser("\nDone! Press any key to exit...");
}

private static ConsoleKeyInfo GetKeyFromUser(string prompt)
{
    Console.Write(prompt);
    var key = Console.ReadKey();
    Console.WriteLine();
    return key;
}

Output

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

yes, I get that this way it allows me to read the next line such as the Enum but what if I wanted to read an entire matrix with several lines, not just one such as the code above. How could I do that? I'm trying to apply to both codes I got but I haven't get there to read several lines...
You could wrap the code in an outer loop for (int j = 0; j < array.GetUpperBound(0); j++) { // existing code here, except use j` for the first index: array[i] = matrix[j, i]; }` I will try to update the sample later when I'm at my computer.
0

I tried doing like this, don't know if it's what you meant, however the program crashes. Still doesn't read second line, I think the variable text from the method AlignCenter is null for some reason.

  private static void ListData<T>(string[,] matrix)
        {
            var array = new string[matrix.GetUpperBound(1)];
            for (int j = 0; j < array.GetUpperBound(0); j++)
            {
                for (var i = 0; i < array.Length; i++)
                {
                    array[i] = matrix[j, i];
                }

                PrintRow(array);
                PrintLine();
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.