Skip to content

Commit

Permalink
Adicionando a classe ValidateText.cs para realizar as validações do t…
Browse files Browse the repository at this point in the history
…exto JSON. Correções também do print no arquivo RegrasDeNegocio.docx
  • Loading branch information
marquescharlon committed Mar 15, 2023
1 parent 6db5868 commit 44afd41
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 51 deletions.
61 changes: 10 additions & 51 deletions AppConverterConsole/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,12 @@ public static void JsonToCSV()
Console.Clear();

// Pedir que seja informado na execução do programa
Console.WriteLine("\n");
Console.WriteLine("Informar o texto JSON");

var texto_json = Console.ReadLine();

if (texto_json == "")
{
Console.WriteLine("O campo está vazio. Preencha-o novamente!");
Thread.Sleep(4000);
Converter.JsonToCSV();
}
else if (!IsValidJson(texto_json))
{
Console.Write("\n");
Console.WriteLine("JSON no formato inválido. Preencha-o novamente!");
Thread.Sleep(4000);
Converter.JsonToCSV();
}
ValidateText.CheckCondition(texto_json);

// 3. Como ler a informação JSON para realizar a manipulação?

Expand Down Expand Up @@ -85,12 +75,14 @@ public static void JsonToCSV()
count_corpo++;
}

var item_temp = corpo[1].ToString();
var cont_linha = 0;

foreach (var item in corpo)
{
if (item != null)
{
textoCSV[0 + 1] += "\"" + texto_json?.Split("\"" + item + "\"")[1].Split("\"")[1] + "\",";

textoCSV[cont_linha + 1] += "\"" + texto_json?.Split("\"" + item + "\"")[1].Split("\"")[1] + "\",";
}
}

Expand All @@ -106,45 +98,12 @@ public static void JsonToCSV()
}

Console.WriteLine("\n");
Console.WriteLine("Esse é o texto convertido em um formato CSV:");
Console.WriteLine("-----------------------------------------------");
Console.WriteLine("| Texto convertido em um formato CSV: |");
Console.WriteLine("-----------------------------------------------");
Console.WriteLine("\n");
Console.WriteLine(resultado);
Menu.LimparTela();

static bool IsValidJson(string jsonString)
{
int tamanho = 0;
bool sequencia = false;

foreach (char c in jsonString)
{
switch (c)
{
case '{':
case '[':
if (!sequencia)
{
tamanho++;
}
break;

case '}':
case ']':
if (!sequencia)
{
tamanho--;
}
break;

case '"':
sequencia = !sequencia;
break;
}
}

return tamanho == 0 && !sequencia;
}
}

}
}
63 changes: 63 additions & 0 deletions AppConverterConsole/ValidateText.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
namespace AppConverterConsole
{
public static class ValidateText
{
public static bool IsValidJson(string json_text)
{
int tamanho = 0;
bool sequencia = false;

foreach (char c in json_text)
{
switch (c)
{
case '{':
case '[':
if (!sequencia)
{
tamanho++;
}
break;
case '}':
case ']':
if (!sequencia)
{
tamanho--;
}
break;
case '"':
sequencia = !sequencia;
break;
}
}
return tamanho == 0 && !sequencia;
}

public static void CheckCondition(string json_text)
{
if (json_text.StartsWith("[{") || json_text.StartsWith("{"))
{
if (json_text == "")
{
Mensagem("O campo está vazio. Preencha-o novamente!");
}
else if (!IsValidJson(json_text))
{
Mensagem("JSON no formato inválido. Preencha-o novamente!");
}
}
else
{
Console.Write("\n");
Mensagem("JSON no formato inválido. Preencha-o novamente!");
}
}

public static void Mensagem(string text)
{
Console.WriteLine(text);
Thread.Sleep(4000);
Converter.JsonToCSV();
}
}
}
Binary file modified RegrasDeNegocio.docx
Binary file not shown.

0 comments on commit 44afd41

Please sign in to comment.