-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTableUsuarios.h
More file actions
215 lines (214 loc) · 7.06 KB
/
Copy pathHashTableUsuarios.h
File metadata and controls
215 lines (214 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#pragma once
#include "Usuario.h"
#include "HashTable.h"
#include "Registros.h"
class HashTableUsuarios : public Base
{
HashTable<Usuario>* usuariosH;
Datos* lectura;
int cant_usuarios;
int atUsuario;
Usuario usuarioSeleccionado;
int numeracion_aux;
ofstream archivo;
public:
HashTableUsuarios()
{
usuariosH = new HashTable<Usuario>([](Usuario u) {
int aux = 0;
for (int i = 0; i < u.getNombre().size(); ++i)
aux += u.getNombre()[i];
return aux % 1000;
});
lectura = new Datos(usuariosH);
lectura->~Datos();
atUsuario = -1;
cant_usuarios = lectura->cantidad_registros;
}
void menuUsuarios()
{
do
{
cout << " -------- Menu de Opciones de Usuario ----------" << "\n\n";
cout << " 1. Ingresar nuevo usuario " << "\n";
cout << " 2. Mostrar todos los usuarios " << "\n";
cout << " 3. Eliminar usuario por nombre " << "\n";
cout << " 4. Buscar usuario por nombre y mostrar datos " << "\n";
cout << " 5. Mostrar usuarios ordenados por edad ascendentemente " << "\n";
cout << " 6. Mostrar usuarios Ordenados por edad descendentemente " << "\n";
cout << " 7. Salir " << "\n";
cout << " -----------------------------------------------" << "\n";
verificarOpcion(7);
if (*opcion == 1) insercion();
if (*opcion == 2) listar();
if (*opcion == 3) eliminar();
if (*opcion == 4) buscar();
if (*opcion == 5) ordenarEdadAscendentemente();
if (*opcion == 6) ordenarEdadDescendentemente();
} while (*opcion != 7);
guardar();
}
bool seleccionarUsuario()
{
string nombre;
cout << "---------------- Elija el Usuario por nombre----------------" << endl << endl;
getline(cin, nombre);
int aux = 0;
for (int i = 0; i < nombre.size(); ++i) aux += nombre[i];
bool flat = usuariosH->buscar2([=](Usuario e) {return e.getNombre() == nombre; }, aux % 10000);
if (flat)
{
string aux_num;
cout << "Elija al usuario " << endl;
getline(cin, aux_num);
int num = stoi(aux_num);
usuarioSeleccionado = usuariosH->retornar_pos([=](Usuario e) {return e.getNombre() == nombre; }, num , aux % 10000);
cout << "\nSe eligio a "<<usuarioSeleccionado.toString2() << "\n";
return true;
}
else
{
cout << "Este usuario no existe" << endl;
return false;
}
}
void insercion()
{
string nombre, alias, pais, aux_edad, aux_telefono, email, opcion;
short edad;
int telefono;
int numeracion = numeracion_aux;
char opt;
archivo.open("datos.csv", ofstream::app);
do
{
cout << "Nombre :" << endl;
getline(cin, nombre);
cout << "Alias :" << endl;
getline(cin, alias);
cout << "Pais :" << endl;
getline(cin, pais);
cout << "Edad :" << endl;
getline(cin, aux_edad);
cout << "Telefono :" << endl;
getline(cin, aux_telefono);
cout << "Email :" << endl;
getline(cin, email);
edad = stoi(aux_edad);
cant_usuarios++;
telefono = stoi(aux_telefono);
edad = stoi(aux_edad);
usuariosH->add(Usuario(numeracion, nombre, alias, pais, edad, telefono, email, 0));
numeracion++;
cout << "Desea continuar registrando un usuario (Si: S, No:N)";
getline(cin, opcion);
opt = toupper(opcion[0]);
} while (opt != 'N');
archivo.close();
system("CLS");
}
void listar()
{
cout << "----------------Lista de Usuarios-----------------" << endl;
unsigned ti, tf;
ti = clock();
usuariosH->display();
tf = clock();
double tiempo = (double(tf - ti) / CLOCKS_PER_SEC);
Console::ForegroundColor = ConsoleColor::Red;
cout << "El tiempo de ejecucion para mostrar los datos en Hash Table es: " << tiempo << " segundos\n";
Console::ForegroundColor = ConsoleColor::White;
}
void ordenarEdadAscendentemente()
{
unsigned ti, tf;
ti = clock();
usuariosH->ordenarMergeSort([](Usuario i, Usuario j) {return i.getEdad() < j.getEdad(); });
tf = clock();
double tiempo = (double(tf - ti) / CLOCKS_PER_SEC);
Console::ForegroundColor = ConsoleColor::Red;
cout << "El tiempo de ejecucion para ordenar los datos en Hash Table es: " << tiempo <<" segundos\n";
Console::ForegroundColor = ConsoleColor::White;
}
void ordenarEdadDescendentemente()
{
unsigned ti, tf;
ti = clock();
usuariosH->ordenarMergeSort([](Usuario i, Usuario j) {return i.getEdad() > j.getEdad(); });
tf = clock();
double tiempo = (double(tf - ti) / CLOCKS_PER_SEC);
Console::ForegroundColor = ConsoleColor::Red;
cout << "El tiempo de ejecucion para ordenar los datos en Hash Table es: " << tiempo << " segundos\n";
Console::ForegroundColor = ConsoleColor::White;
}
void eliminar()
{
unsigned ti, tf;
string nombre;
cout << "Ingrese el nombre a eliminar: " << endl;
getline(cin, nombre);
int aux = 0;
ti = clock();
for (int i = 0; i < nombre.size(); ++i) aux += nombre[i];
bool flat = usuariosH->eliminar([=](Usuario e) {return e.getNombre() != nombre; }, aux % 10000, nombre);
if (flat)
{
cout << "Eliminado correctamente todos los usuarios con nombre " << nombre << "\n";
tf = clock();
double tiempo = (double(tf - ti) / CLOCKS_PER_SEC);
Console::ForegroundColor = ConsoleColor::Red;
cout << "El tiempo de ejecucion para eliminar el dato en Hash Table es: " << tiempo << " segundos\n";
Console::ForegroundColor = ConsoleColor::White;
}
if (!flat)cout << "No existe el usuario con nombre " << nombre << " registrado" << "\n";
}
void buscar()
{
unsigned ti, tf;
string nombre;
cout << "Ingrese el nombre a buscar : " << endl;
getline(cin, nombre);
int aux = 0;
ti = clock();
for (int i = 0; i < nombre.size(); ++i) aux += nombre[i];
bool flat = usuariosH->buscar([=](Usuario e) {return e.getNombre() == nombre; }, aux % 10000);
if (flat)
{
cout << nombre << "Se encontro correctamente el usuario con nombre " << nombre << "\n";
tf = clock();
double tiempo = (double(tf - ti) / CLOCKS_PER_SEC);
Console::ForegroundColor = ConsoleColor::Red;
cout << "El tiempo de ejecucion para buscar el dato en Hash Table es: " << tiempo << " segundos\n";
Console::ForegroundColor = ConsoleColor::White;
}
if (!flat)cout << "No existe el usuario con nombre" << nombre << " registrado" << "\n";
}
void guardar()
{
unsigned ti, tf;
ofstream miarchivo("datos.csv"); // BORRA EL CONTENIDO DEL ARCHIVO
ti = clock();
if (!miarchivo.fail())
{
for (int i = 0; i < cant_usuarios; i++)
{
for (auto it:usuariosH->getContainer()[i])
{
if(!usuariosH->getContainer().empty())
miarchivo << it.getNombre() << "," << it.getAlias() << "," << it.getPais() << "," << it.getEdad() << "," << it.getTelefono() << "," << it.getEmail() << "," << it.getPuntuacion() << endl;
}
}
miarchivo.flush();
miarchivo.close();
}
tf = clock();
double tiempo = (double(tf - ti) / CLOCKS_PER_SEC);
Console::ForegroundColor = ConsoleColor::Red;
cout << "El tiempo de ejecucion para guardar los datos en el archivo es: " << tiempo << " segundos\n";
Console::ForegroundColor = ConsoleColor::White;
}
void elegirUsuario(int posicion) { atUsuario = posicion - 1; }
int retornarUsuarioElegido() { return atUsuario; }
int retornarCantidad() { return cant_usuarios; }
HashTable<Usuario>* retornarUsuarios() { return usuariosH; }
};