Skip to content

Instantly share code, notes, and snippets.

@celsojr
Last active June 20, 2016 02:05
Show Gist options
  • Save celsojr/bb02aa6525ad8bdb7ea5bff7abf22a92 to your computer and use it in GitHub Desktop.
Save celsojr/bb02aa6525ad8bdb7ea5bff7abf22a92 to your computer and use it in GitHub Desktop.
Exemplo da atividade MAPA com a liguagem csharp
/*
* ////////////////////////// MAPA FILA BANCO C# //////////////////////////////
* Atividade MAPA desenvolvida para ajudar os amigos da faculdade UniCesumar
* Trata-se de uma simples implementação de um atendimento de uma fila de
* banco utilizando os recursos da linguagem c# 6
*
* Copyright(c) 2016 Celso Junior, [email protected]
*
* Licensed under MIT-style license:
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
using static System.ConsoleColor;
using static Categorias;
using static Menu;
enum Menu { Incluir = 1, Retirar, Exibir, Sair }
enum Categorias { C, N, P, R, A }
namespace MapaFilaBanco
{
internal class Program
{
static List<jfilabanco> fila;
static int clientes = 0;
static void Main()
{
fila = new List<jfilabanco>();
// Inclui 20 senhas na fila automaticamente
GerarMassaTeste();
var opt = 0;
do {
opt = MostrarMenu();
Opcao(opt);
} while (opt != (int)Sair);
}
static int MostrarMenu()
{
Clear();
ForegroundColor = White;
WriteLine("Menu principal\n");
ResetColor();
foreach (var item in Enum.GetValues(typeof(Menu)))
WriteLine($"{(int)item} - {item}");
var input = ReadLine();
int opcao;
if (!int.TryParse(input, out opcao))
return 0;
return opcao;
}
static void Opcao(int opt)
{
switch ((Menu)opt)
{
case Incluir:
IncluirClienteFila();
break;
case Retirar:
RetirarClienteFila();
break;
case Exibir:
ExibirFila();
break;
case Sair:
// Não faz nada e encerra imediatamente
break;
default:
ForegroundColor = Red;
WriteLine("Opção inválida!");
ResetColor();
Write("\nPressione ");
ForegroundColor = White;
Write("ENTER");
ResetColor();
Write(" para tentar novamente...");
ReadLine();
break;
}
}
static void IncluirClienteFila()
{
Clear();
WriteLine("C - Correntista");
WriteLine("N - Não-Correntista");
WriteLine("P - Prioridade (idosos, gestantes, etc.)");
WriteLine("R - Atendimento rápido (operações simples)");
WriteLine("A - Amigos do André Noel\n");
Write("Digite a categoria: ");
var categoria = default(Categorias);
try
{
categoria = (Categorias)Enum.Parse(typeof(Categorias), ReadLine(), true);
}
catch (Exception)
{
ForegroundColor = Red;
WriteLine("\nCategoria inválida! Operação cancelada.");
ResetColor();
Write("\nPressione ");
ForegroundColor = White;
Write("ENTER");
ResetColor();
Write(" para voltar...");
ReadLine();
return;
}
var cliente = new jfilabanco();
cliente.categoria = categoria.ToString();
cliente.numero = ++clientes;
fila.Add(cliente);
ForegroundColor = Yellow;
WriteLine("\nAdicionado!");
ResetColor();
Write("\nPressione ");
ForegroundColor = White;
Write("ENTER");
ResetColor();
Write(" para continuar...");
ReadLine();
}
static void RetirarClienteFila()
{
Clear();
if (clientes == 0)
{
ForegroundColor = Red;
WriteLine("Nenhuma senha retirada.");
ResetColor();
Write("\nPressione ");
ForegroundColor = White;
Write("ENTER");
ResetColor();
Write(" para continuar...");
ReadLine();
return;
}
var rnd = new Random();
var atendimentos = clientes;
for (var i = 0; i < atendimentos; i++)
{
var caixa = rnd.Next(1, 9);
switch (caixa)
{
case 1:
case 2:
Atender(caixa, P);
WriteLine("\n");
break;
case 3:
case 4:
Atender(caixa, N);
WriteLine("\n");
break;
case 5:
Atender(caixa, new[] { N, C });
WriteLine("\n");
break;
case 6:
case 7:
Atender(caixa, C);
WriteLine("\n");
break;
case 8:
Atender(caixa, R);
WriteLine("\n");
break;
}
}
WriteLine($"Total de {atendimentos} clientes atendidos");
Write("\nPressione ");
ForegroundColor = White;
Write("ENTER");
ResetColor();
Write(" para continuar...");
ReadLine();
}
static void Atender(int caixa, IEnumerable<Categorias> categorias)
{
if (Atender(caixa)) return;
foreach (var categoria in categorias)
{
var senhaPreferencial = fila.FirstOrDefault(c => c.categoria == categoria.ToString());
if (senhaPreferencial != null)
{
WriteLine($"Caixa {caixa} livre");
Write("Atendimento ");
ForegroundColor = Yellow;
Write("Preferencial");
ResetColor();
Write($" Senha: {senhaPreferencial.senha}");
fila.Remove(senhaPreferencial);
clientes--;
return;
}
}
var senhaNormal = fila.First();
WriteLine($"Caixa {caixa} livre");
Write("Atendimento ");
ForegroundColor = White;
Write("Normal");
ResetColor();
Write($" Senha: {senhaNormal.senha}");
fila.Remove(senhaNormal);
clientes--;
}
static void Atender(int caixa, Categorias categoria)
{
if (!Atender(caixa))
{
var senhaPreferencial = fila.FirstOrDefault(c => c.categoria == categoria.ToString());
if (senhaPreferencial != null)
{
WriteLine($"Caixa {caixa} livre");
Write("Atendimento ");
ForegroundColor = Yellow;
Write("Preferencial");
ResetColor();
Write($" Senha: {senhaPreferencial.senha}");
fila.Remove(senhaPreferencial);
}
else
{
var senhaNormal = fila.First();
WriteLine($"Caixa {caixa} livre");
Write("Atendimento ");
ForegroundColor = White;
Write("Normal");
ResetColor();
Write($" Senha: {senhaNormal.senha}");
fila.Remove(senhaNormal);
}
clientes--;
}
}
static bool Atender(int caixa)
{
var senhaPreferencial = fila.FirstOrDefault(c => c.categoria == A.ToString());
if (senhaPreferencial != null)
{
WriteLine($"Caixa {caixa} livre");
Write("Atendimento ");
ForegroundColor = Yellow;
Write("Preferencial");
ResetColor();
Write($" Senha: {senhaPreferencial.senha}");
fila.Remove(senhaPreferencial);
clientes--;
return true;
}
return false;
}
static void ExibirFila()
{
Clear();
WriteLine($"Senhas retiradas em {DateTime.Now:dd/MM/yyyy}\n");
ForegroundColor = White;
if (fila.Count != 0)
{
fila.ForEach(a => WriteLine(a.senha));
}
else
{
WriteLine("Nenhuma senha retirada no momento.");
}
ResetColor();
Write("\nPressione ");
ForegroundColor = White;
Write("ENTER");
ResetColor();
Write(" para continuar...");
ReadLine();
}
/// <summary>
/// Método criado apenas para propósitos de testes
/// </summary>
static void GerarMassaTeste()
{
var rnd = new Random();
for (int i = 0; i < 20; i++)
{
var limite = Enum.GetValues(typeof(Categorias)).Length;
var categoria = (Categorias)rnd.Next(0, limite);
fila.Add(new jfilabanco { categoria = categoria.ToString(), numero = ++clientes });
}
}
}
internal class jfilabanco
{
public string categoria { get; set; }
public int numero { get; set; }
public string senha => $"{categoria}-{numero:00}";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment