ARTICLE AD BOX
Whenever I use the "clear" command to delete the previous text and display new text, the console advances several lines, and I have to scroll to see the desired text above. I don't know the possible reasons why this is happening.
I'm working on a simple project with an interactive menu, and each time someone clicks on a menu option, I want the menu to clear, the function to be displayed, the user to be prompted to press a key to exit, and when the key is pressed, the menu to return, clearing the text and displaying only the menu.
static int LerInt(string mensagem) { int valor; while (true) { Console.WriteLine(mensagem); try { valor = int.Parse(Console.ReadLine()); return valor; } catch { Console.WriteLine("Valor inválido. Introduza um número inteiro."); } } } static double LerDouble(string mensagem) { double valor; while (true) { Console.WriteLine(mensagem); try { valor = double.Parse(Console.ReadLine()); return valor; } catch { Console.WriteLine("Valor inválido. Introduza um número decimal."); } } } static void Main(string[] args) { int numeroMenu = 0; string[] nomes = null; double[] preco = null; int[] stock = null; string[] historicoProduto = new string[100]; int[] historicoQuantidade = new int[100]; double[] historicoValor = new double[100]; int totalVendas = 0; do { Console.WriteLine("--------------------------------------------"); Console.WriteLine("Bem vindo ao menu de gestão da sua loja!"); Console.WriteLine("--------------------------------------------"); Console.WriteLine("\n1 - Registar novo produto"); Console.WriteLine("2 - Listar produtos disponiveis."); Console.WriteLine("3 - Registar uma venda."); Console.WriteLine("4 - Desfazer uma venda."); Console.WriteLine("5 - Hsitorico de vendas"); Console.WriteLine("6 - Verificar produto mais vendido"); Console.WriteLine("0 - Sair"); do { Console.WriteLine("Digite uma tecla entre 0 a 5 para acessar o menu "); numeroMenu = int.Parse(Console.ReadLine()); } while (numeroMenu < 0 || numeroMenu > 6); switch (numeroMenu) { case 0: break; case 1: int produto = LerInt("Digite a quantidade de produtos que deseja registrar:"); nomes = new string[produto]; preco = new double[produto]; stock = new int[produto]; (nomes, preco, stock) = RegistarProdutos(nomes, preco, stock); break; case 2: Listar(nomes, preco, stock); break; case 3: RegistarVenda(nomes, preco, stock, historicoProduto, historicoQuantidade, historicoValor, ref totalVendas); break; case 4: DesfazerVenda(nomes, stock, historicoProduto, historicoQuantidade, historicoValor, ref totalVendas); break; case 5: MostrarHistorico(historicoProduto, historicoQuantidade, historicoValor, totalVendas); break; case 6: MostrarProdutoMaisVendido(historicoProduto, historicoQuantidade, totalVendas); break; } } while (numeroMenu != 0); } static (string[] nomes, double[] preco, int[] stock) RegistarProdutos(string[] nomes, double[] preco, int[] stock) { for (int i = 0; i < nomes.Length; i++) { Console.WriteLine("Digite o nome do produto:"); nomes[i] = Console.ReadLine(); preco[i] = LerDouble("O valor do produto é:"); stock[i] = LerInt("Quantidade em stock:"); } Console.WriteLine("Produtos registados: " + string.Join(", ", nomes)); return (nomes, preco, stock); } static void Listar(string[] nomes, double[] preco, int[] stock) { if (nomes == null || preco == null || stock == null) { Console.WriteLine("\nRegiste um produto antes de listar"); } else { for (int i = 0; i < nomes.Length; i++) { Console.WriteLine("Produtos disponiveis: " + nomes[i] + " Preço: " + preco[i] + " Stock disponivel: " + stock[i]); } } } static void RegistarVenda(string[] nomes, double[] preco, int[] stock, string[] historicoProduto, int[] historicoQuantidade, double[] historicoValor, ref int totalVendas) { if (nomes == null) { Console.WriteLine("Nenhum produto registado ainda."); } else { Console.WriteLine("\n---- Produtos Disponiveis ----"); for (int i = 0; i < nomes.Length; i++) { Console.WriteLine((i + 1) + " Produto: " + nomes[i] + " | Stock: " + stock[i]); } int escolha = LerInt("Escolha o número do produto vendido:") - 1; if (escolha < 0 || escolha >= nomes.Length) { Console.WriteLine("Produto inválido."); return; } int quantidade = LerInt("Quantidade vendida:"); if (quantidade > stock[escolha]) { Console.WriteLine("Stock insuficiente."); return; } stock[escolha] -= quantidade; historicoProduto[totalVendas] = nomes[escolha]; historicoQuantidade[totalVendas] = quantidade; historicoValor[totalVendas] = quantidade * preco[escolha]; totalVendas++; Console.WriteLine("Venda registada com sucesso!"); Console.WriteLine("Produto: " + nomes[escolha] + " | Quantidade: " + quantidade + " | Stock restante: " + stock[escolha]); } } static void DesfazerVenda(string[] nomes, int[] stock, string[] historicoProduto, int[] historicoQuantidade, double[] historicoValor, ref int totalVendas) { if (totalVendas == 0) { Console.WriteLine("Nao existem vendas para desfazer."); return; } Console.WriteLine("-----HISTÓRICO DE VENDAS-----"); for(int i = 0; i < totalVendas; i++) { Console.WriteLine((i + 1) + " Produto: " + historicoProduto[i] + " | Quantidade: " + historicoQuantidade[i] + " | Valor: " + historicoValor[i]); } int escolha = LerInt("Escolha o número da venda que deseja desfazer:") - 1; if (escolha < 0 || escolha >= totalVendas) { Console.WriteLine("Venda inválida."); return; } string produto = historicoProduto[escolha]; int quantidade = historicoQuantidade[escolha]; int indexProduto = Array.IndexOf(nomes, produto); if (indexProduto != -1) { stock[indexProduto] += quantidade; } Console.WriteLine("Venda anulada! Stock reposto para o produto " + produto + " ."); for (int i = escolha; i < totalVendas - 1; i++) { historicoProduto[i] = historicoProduto[i + 1]; historicoQuantidade[i] = historicoQuantidade[i + 1]; historicoValor[i] = historicoValor[i + 1]; } totalVendas--; Console.WriteLine("Venda removida do histórico com sucesso."); } static void MostrarHistorico(string[] produtos, int[] quantidades, double[] valores, int total) { if (total == 0) { Console.WriteLine("Nenhuma venda registada ainda. "); return; } else { Console.WriteLine("----- HISTÓRICO DE VENDAS -----"); for (int i = 0; i < total; i++) { Console.WriteLine((i + 1) + " Produto: " + produtos[i] + " | Quantidade: " + quantidades[i] + " | Valor total: " + valores[i]); } } } static void MostrarProdutoMaisVendido(string[] historicoProduto, int[] historicoQuantidade, int totalVendas) { if (totalVendas == 0) { Console.WriteLine("Nenhuma venda registada ainda."); return; } string[] nomesUnicos = new string[100]; int[] totalPorProduto = new int[100]; int contador = 0; for (int i = 0; i < totalVendas; i++) { string nome = historicoProduto[i]; int quantidade = historicoQuantidade[i]; int index = Array.IndexOf(nomesUnicos, nome); if (index == -1) { nomesUnicos[contador] = nome; totalPorProduto[contador] = quantidade; contador++; } else { totalPorProduto[index] += quantidade; } } int maxIndex = 0; for(int i = 1; i < contador; i++) { if (totalPorProduto[i] > totalPorProduto[maxIndex]) { maxIndex = i; } } Console.WriteLine("----- PRODUTO MAIS VENDIDO -----"); Console.WriteLine("Produto: " + nomesUnicos[maxIndex]); Console.WriteLine("Quantidade vendida: " + totalPorProduto[maxIndex]); }