static void Main(string[] args) { if (args.Length == 0) { Environment.Exit(0); } while (true) { FileExists(args[0]); StreamReader reader = new StreamReader(args[0]); string[] read = reader.ReadToEnd().Split(Environment.NewLine); reader.Close(); Ins214Set people = new Ins214Set(); foreach (var i in read) { if (i != "") { people.Add(Persona.CreateFromLine(i)); } } System.Console.WriteLine("\n1- Capturar\n2- Listar\n3- Buscar\n4- Modificar\n5- Eliminar\n6-salir"); System.Console.Write("Selecione una opciona: "); string option = Console.ReadLine(); switch (option) { case "1": Capturar(args[0], people); break; case "2": Listar(people); break; case "3": Buscar(people); break; case "4": Editar(args[0], people); break; case "5": Eliminar(args[0], people); break; case "6": Environment.Exit(0); break; default: System.Console.WriteLine("Esta opcion no existe!!!"); break; } } }
static void Listar(Ins214Set people) {// Se devuelve la lista sorteada por Id foreach (var i in people.ToSortedArray()) { Console.WriteLine(i); } }
static void Eliminar(string path, Ins214Set people) { Persona persona = Buscar(people); if (persona.Id == "") { return; } while (true) { Console.WriteLine("¿Desea eliminarlo (Y/N)?"); string opt = Console.ReadLine().ToUpper(); if (opt == "Y") { people.Remove(persona);//Se remueve la persona File.Delete(path); foreach (var i in people.ToSortedArray()) { if (i.Equals(persona)) { continue; } StreamWriter writer = File.AppendText(path); writer.WriteLine(i.ToWrite()); writer.Close(); } Console.WriteLine(); break; } else if (opt == "N") { break; } else { continue; } } }
static Persona Buscar(Ins214Set people) { string ced = ReadCedula("\nIntroduzca la cédula a buscar: "); Persona persona = new Persona("", "", "", 0, 0, ""); Console.WriteLine(); foreach (var i in people.ToSortedArray()) { if (i.Id == ced) { persona = i; Console.WriteLine(persona); } } if (persona.Id == "") { Console.WriteLine("No se ha podido encontrar la persona!!"); } return(persona); }
static void Capturar(string path, Ins214Set people) { while (true) { string ced = ReadCedula("\nCedula: "); Console.Write("\nNombre: "); string name = Console.ReadLine(); Console.Write("Apellidos: "); string ape = Console.ReadLine(); if (name == "" && ape == "") { break; } int age = ReadAge("Edad (7 - 120): ");; while (age < 7 || age > 120) { age = ReadAge("\nEdad (7 - 120): "); } char gender, state, grade; do { gender = ReadChar("\nGénero (M/F): "); } while (gender != 'M' && gender != 'F'); do { state = ReadChar("\nEstado Civil (S/C): "); } while (state != 'S' && state != 'C'); do { grade = ReadChar("\nGrado Académico (I/M/G/P): "); } while (grade != 'I' && grade != 'M' && grade != 'G' && grade != 'P'); decimal ahorros = ReadMoney("\nAhorros: "); string password = ReadPassword("\nContraseña: "); bool success = password == ReadPassword("\nConfirme contraseña: "); int datos = ToBits(age, gender, state, grade); Console.WriteLine(); if (!success) { continue; } Persona nuevo = new Persona(ced, name, ape, datos, ahorros, password); if (people.Contains(nuevo)) { Console.WriteLine("La cédula ya existe!!"); } else { while (true) { Console.WriteLine("\nGuardar (G); Rehacer (R); Salir (S)"); string opt = Console.ReadLine().ToUpper(); if (opt == "G") { StreamWriter writer = File.AppendText(path); writer.WriteLine(nuevo.ToWrite()); writer.Close(); break; } else if (opt == "R") { break; } else if (opt == "S") { Environment.Exit(0); } else { continue; } } } } }
static void Editar(string path, Ins214Set people) { Persona persona = Buscar(people); if (persona.Id == "") { return; } while (true) { string ced = ReadCedula("\nCedula: "); Console.Write("\nNombre: "); string name = Console.ReadLine(); Console.Write("Apellidos: "); string ape = Console.ReadLine(); if (name == "" && ape == "") { break; } int age = ReadAge("Edad (7 - 120): ");; while (age < 7 || age > 120) { age = ReadAge("\nEdad (7 - 120): "); } char gender, state, grade; do { gender = ReadChar("\nGénero (M/F): "); } while (gender != 'M' && gender != 'F'); do { state = ReadChar("\nEstado Civil (S/C): "); } while (state != 'S' && state != 'C'); do { grade = ReadChar("\nGrado Académico (I/M/G/P): "); } while (grade != 'I' && grade != 'M' && grade != 'G' && grade != 'P'); decimal ahorros = ReadMoney("\nAhorros: "); string password = ReadPassword("\nContraseña: "); bool success = password == ReadPassword("\nConfirme contraseña: "); int datos = ToBits(age, gender, state, grade); Console.WriteLine(); if (!success) { continue; } Persona nuevo = new Persona(ced, name, ape, datos, ahorros, password); if (nuevo.Equals(persona)) { File.Delete(path); foreach (var i in people.ToSortedArray()) { Persona line = i; if (line.Equals(nuevo)) { line = nuevo; } StreamWriter writer = File.AppendText(path); writer.WriteLine(line.ToWrite()); writer.Close(); } Console.WriteLine(); break; } else if (people.Contains(nuevo)) { Console.WriteLine("\nLa cédula ya existe!!"); } else { File.Delete(path); foreach (var i in people.ToSortedArray()) { Persona line = i; if (line.Equals(persona)) { line = nuevo; } StreamWriter writer = File.AppendText(path); writer.WriteLine(line.ToWrite()); writer.Close(); } Console.WriteLine(); break; } } }