I have been trying to work around this method but the output I get it's not what I need.
When the program run at main I get to insert data into the array and after that should be able to create a table adding the data within. The problem is get a really bad formatation. Does anyone could tell what's the problem of the LISTDATA<t> method? I haven't got this one.
Sorry if I've forgotten any useful info and thanks for looking.
What I have tried:
public class Program
{
private static void Main()
{
var client = new string[4, 7];
InsertData<clientheader>(client);
Console.Clear();
InsertData<clientheader>(client);
listData<clientheader>(client);
Console.ReadKey();
}
static void showHeader<t>(string[,] matrix)
{
string line = new String('-', 83);
int[] size = new int[] { 4, 10, 10, 15, 10, 10, 15 };
Console.WriteLine(line);
Console.Write("|");
for (int i = 0; i < matrix.GetLength(1) - 1; i++)
{
string space = new String(' ', size[i] - GetHeader<t>(i).Length);
string header = GetHeader<t>(i);
Console.Write($"{header.ToUpper()}{space}");
Console.Write("|");
}
Console.WriteLine();
}
static void listData<t>(string[,] matrix)
{
Console.Clear();
showHeader<t>(matrix);
string line = new String('-', 83);
int[] size = new int[] { 10, 15, 15, 15, 15, 15, 15 };
for (int i = 0; i < matrix.GetLength(0); i++)
{
Console.WriteLine(line);
Console.Write("|");
for (int j = 0; j < matrix.GetLength(1) - 1; j++)
{
if (matrix[i, j] == null) matrix[i, j] = "";
string space = new String(' ', size[i] - matrix[i, j].Length - 1);
Console.Write($"{matrix[i, j]}{space}");
Console.Write("|");
}
Console.WriteLine();
}
Console.WriteLine(line);
}
static int getInsertIndex(string[,] matrix)
{
for (int j = 0; j < matrix.GetLength(0); j++)
{
if (string.IsNullOrEmpty(matrix[j, 0])) return j;
}
return -1;
}
private static void InsertData<t>(string[,] matrix)
{
int n = getInsertIndex(matrix), id = 1;
matrix[n, 0] = Convert.ToString(id++);
int x = matrix.GetLength(1) - 1;
for (var j = 1; j < matrix.GetLength(1); j++)
{
do
{
Console.Write($"\nInsert {GetHeader<t>(j)}: ");
matrix[n, j] = Console.ReadLine();
} while (string.IsNullOrEmpty(matrix[0, j]));
}
}
private static string GetHeader<t>(int i) => Enum.GetName(typeof(T), i);
enum ClientHeader { Id, Name, Surname, Addres, CodPostal, Telephone, Email, State };
}
}