Skip to content

Commit ecc38c5

Browse files
committed
19-C#-ENUMERACIONES
1 parent 72bab44 commit ecc38c5

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
╔══════════════════════════════════════╗
3+
║ Autor: Kenys Alvarado ║
4+
║ GitHub: https://github.com/Kenysdev ║
5+
║ 2024 - C# ║
6+
╚══════════════════════════════════════╝
7+
------------------------------------------
8+
* ENUMERACIONES
9+
------------------------------------------
10+
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/enum
11+
https://learn.microsoft.com/es-es/dotnet/api/system.enum?view=net-8.0
12+
*/
13+
14+
#pragma warning disable CA1050
15+
class Program {
16+
/*
17+
* EJERCICIO 1:
18+
* Empleando tu lenguaje, explora la definición del tipo de dato
19+
* que sirva para definir enumeraciones (Enum).
20+
* Crea un Enum que represente los días de la semana del lunes
21+
* al domingo, en ese orden. Con ese enumerado, crea una operación
22+
* que muestre el nombre del día de la semana dependiendo del número entero
23+
* utilizado (del 1 al 7).
24+
*/
25+
enum Weekday {
26+
MONDAY = 1,
27+
TUESDAY = 2,
28+
WEDNESDAY = 3,
29+
THURSDAY = 4,
30+
FRIDAY = 5,
31+
SATURDAY = 6,
32+
SUNDAY = 7
33+
}
34+
35+
static string GetDay(int num) {
36+
return Enum.GetName(typeof(Weekday), num) ?? "'o'";
37+
}
38+
39+
static int GetNumDay(string day) {
40+
return Enum.TryParse(day, out Weekday weekday) ? (int)weekday : 0;
41+
}
42+
43+
/*
44+
* EJERCICIO 2:
45+
* Crea un pequeño sistema de gestión del estado de pedidos.
46+
* Implementa una clase que defina un pedido con las siguientes características:
47+
* - El pedido tiene un identificador y un estado.
48+
* - El estado es un Enum con estos valores: PENDIENTE, ENVIADO, ENTREGADO y CANCELADO.
49+
* - Implementa las funciones que sirvan para modificar el estado:
50+
* - Pedido enviado
51+
* - Pedido cancelado
52+
* - Pedido entregado
53+
* (Establece una lógica, por ejemplo, no se puede entregar si no se ha enviado, etc...)
54+
* - Implementa una función para mostrar un texto descriptivo según el estado actual.
55+
* - Crea diferentes pedidos y muestra cómo se interactúa con ellos.
56+
*/
57+
enum Status {
58+
PENDING,
59+
SHIPPED,
60+
DELIVERED,
61+
CANCELED
62+
}
63+
64+
class Order(string identifier) {
65+
Status _status = Status.PENDING;
66+
67+
public void Send() {
68+
Console.WriteLine("\nEnviar:");
69+
if (_status == Status.PENDING) {
70+
_status = Status.SHIPPED;
71+
Console.WriteLine(Info());
72+
} else {
73+
Console.WriteLine($"invalid operation, {Info()}");
74+
}
75+
}
76+
77+
public void Cancelled() {
78+
Console.WriteLine("\nCancelar:");
79+
if (_status == Status.PENDING) {
80+
_status = Status.CANCELED;
81+
Console.WriteLine(Info());
82+
} else {
83+
Console.WriteLine($"invalid operation, {Info()}");
84+
}
85+
}
86+
87+
public void Delivered() {
88+
Console.WriteLine("\nEntregar:");
89+
if (_status == Status.SHIPPED) {
90+
_status = Status.DELIVERED;
91+
Console.WriteLine(Info());
92+
} else {
93+
Console.WriteLine($"invalid operation, {Info()}");
94+
}
95+
}
96+
97+
public string Info() {
98+
return $"{identifier} is {_status}";
99+
}
100+
}
101+
102+
103+
static void Main() {
104+
// _____________________________________
105+
Console.WriteLine(GetDay(7));
106+
Console.WriteLine(GetDay(3));
107+
Console.WriteLine(GetDay(8));
108+
109+
Console.WriteLine(GetNumDay("TUESDAY"));
110+
Console.WriteLine(GetNumDay("FRIDAY"));
111+
Console.WriteLine(GetNumDay("abc"));
112+
113+
// _____________________________________
114+
// Creación de pedidos
115+
Order libro1 = new("libro1");
116+
Order libro2 = new("libro2");
117+
Order libro3 = new("libro3");
118+
119+
// Positivas
120+
Console.WriteLine("\n-----\nOperaciones exitosas:\n-----");
121+
libro1.Send();
122+
libro1.Delivered();
123+
libro2.Cancelled();
124+
125+
// Negativas
126+
Console.WriteLine("\n-----\nOperaciones inválidas:\n-----");
127+
libro3.Delivered();
128+
libro2.Cancelled();
129+
libro1.Send();
130+
131+
// Info
132+
Console.WriteLine("\n-----\nEstado de órdenes\n-----");
133+
Console.WriteLine(libro1.Info());
134+
Console.WriteLine(libro2.Info());
135+
Console.WriteLine(libro3.Info());
136+
}
137+
}

0 commit comments

Comments
 (0)