//Metodo para salvar el ultimo dato evitando grabar todos los registros. private static void salvarDato(tipoGasto gasto) { StreamWriter ficheroWrite; string nombreArchivo = "contabilidadBD.txt"; string linea; try { linea = gasto.fecha + "|" + gasto.descripcion + "|" + gasto.categoria + "|" + gasto.importe.ToString(); ficheroWrite = File.AppendText(nombreArchivo); ficheroWrite.WriteLine(linea); ficheroWrite.Close(); } catch (PathTooLongException) { Console.WriteLine("Nombre de fichero destino demasiado largo!"); } catch (IOException e) { Console.WriteLine("No se ha podido escribir el archivo"); Console.WriteLine("Motivo: {0}", e.Message); } catch (Exception e) { Console.WriteLine("Error al guardar el archivo"); Console.WriteLine("Motivo: {0}", e.Message); } }
static void Main() { byte opcion; const int CAPACIDAD = 1000; int cantidad = 0; tipoGasto[] gastos = new tipoGasto[CAPACIDAD]; // Carga de datos if (File.Exists("gastos.txt")) { string[] datosDelFichero = File.ReadAllLines("gastos.txt"); int pos = 1; cantidad = Convert.ToInt32(datosDelFichero[0]); for (int i = 0; i < cantidad; i++) { gastos[i].descripcion = datosDelFichero[pos++]; gastos[i].categoria = datosDelFichero[pos++]; gastos[i].importe = Convert.ToDouble(datosDelFichero[pos++]); string[] fechaEnFichero = datosDelFichero[i].Split(); gastos[i].fecha.dia = Convert.ToInt32(fechaEnFichero[0]); gastos[i].fecha.mes = Convert.ToInt32(fechaEnFichero[0]); gastos[i].fecha.anyo = Convert.ToInt32(fechaEnFichero[0]); } } do { Console.WriteLine("1. Añadir un gasto "); Console.WriteLine("2. Ver informes "); Console.WriteLine("3. Buscar "); Console.WriteLine("0. Salir "); opcion = Convert.ToByte(Console.ReadLine()); switch (opcion) { case 1: if (cantidad >= CAPACIDAD) { Console.WriteLine("Base de datos llena"); } else { Console.Write("Descripcion: "); gastos[cantidad].descripcion = Console.ReadLine(); Console.Write("Categoria: "); gastos[cantidad].categoria = Console.ReadLine(); Console.Write("Importe: "); gastos[cantidad].importe = Convert.ToDouble(Console.ReadLine()); Console.Write("Dia: "); gastos[cantidad].fecha.dia = Convert.ToInt32(Console.ReadLine()); Console.Write("Mes: "); gastos[cantidad].fecha.mes = Convert.ToInt32(Console.ReadLine()); Console.Write("Año: "); gastos[cantidad].fecha.anyo = Convert.ToInt32(Console.ReadLine()); cantidad++; } break; case 2: Console.WriteLine("Elige opción "); Console.WriteLine("1. Buscar por categoría"); Console.WriteLine("2. Buscar por mes"); Console.WriteLine("3. Buscar entre dos fechas"); string opcion2 = Console.ReadLine(); if (opcion2 == "1") { Console.Write("Categoría a buscar"); string buscarCategoria = Console.ReadLine(); for (int i = 0; i < cantidad; i++) { if (gastos[i].categoria.ToUpper() == buscarCategoria.ToUpper()) { Console.WriteLine("descripción: " + gastos[i].descripcion); Console.WriteLine("Categoria: " + gastos[i].categoria); Console.WriteLine("Importe: " + gastos[i].importe); Console.WriteLine("Fecha: " + gastos[i].fecha.dia + "/" + gastos[i].fecha.mes + "/" + gastos[i].fecha.anyo); } } } else if (opcion2 == "2") { Console.Write("Mes a buscar: "); int buscarMes = Convert.ToInt32(Console.ReadLine()); for (int i = 0; i < cantidad; i++) { if (buscarMes == gastos[i].fecha.mes) { Console.WriteLine("descripción: " + gastos[i].descripcion); Console.WriteLine("Categoria: " + gastos[i].categoria); Console.WriteLine("Importe: " + gastos[i].importe); Console.WriteLine("Fecha: " + gastos[i].fecha.dia + "/" + gastos[i].fecha.mes + "/" + gastos[i].fecha.anyo); } } } else if (opcion2 == "3") { Console.WriteLine("Opción no disponible"); } else { Console.WriteLine("Opción incorrecta"); } break; case 3: if (cantidad == 0) { Console.WriteLine("No hay suficientes datos donde buscar"); } else { bool found = false; Console.Write("Introduce el texto que buscar: "); string busqueda = Console.ReadLine(); for (int i = 0; i < cantidad; i++) { if (gastos[i].descripcion.ToLower().Contains( busqueda.ToLower())) { Console.WriteLine((i + 1) + ": " + gastos[i].descripcion + " - " + gastos[i].categoria + " - " + gastos[i].importe + " - " + gastos[i].fecha.dia + "/" + gastos[i].fecha.mes + "/" + gastos[i].fecha.anyo); found = true; } } if (!found) { Console.WriteLine("No encontrado"); } } break; case 0: Console.WriteLine("Hasta otra!"); break; default: Console.WriteLine("Opción no válida"); break; } } while (opcion != 0); // Rellenar array temporal y guardar string[] datosComoArray = new string[cantidad * 4 + 1]; datosComoArray[0] = cantidad.ToString(); int posicion = 1; for (int i = 0; i < cantidad; i++) { datosComoArray[posicion++] = gastos[i].descripcion; datosComoArray[posicion++] = gastos[i].categoria; datosComoArray[posicion++] = gastos[i].importe.ToString(); datosComoArray[posicion++] = gastos[i].fecha.dia + "-" + gastos[i].fecha.mes + "-" + gastos[i].fecha.anyo; } File.WriteAllLines("gastos.txt", datosComoArray); }
static void Main() { List <tipoGasto> gastos = new List <tipoGasto>(); bool salir = false; gastos = cargarBD(); Console.WriteLine(); Console.WriteLine("SISTEMA DE CONTABILIDAD DOMÉSTICA"); do { Console.WriteLine(); Console.WriteLine("1. Añadir un nuevo gasto"); Console.WriteLine("2. Filtrar gastos"); Console.WriteLine("3. Buscar gastos"); Console.WriteLine("4. Modificar ficha"); Console.WriteLine("5. Borrar dato"); Console.WriteLine("6. Ordenador datos alfabéticamente"); Console.WriteLine("7. Normalizar descripciones"); Console.WriteLine("T. Terminar"); Console.WriteLine(); Console.WriteLine("Selección una opción: (pulse T para salir)"); string opcion = Console.ReadLine().ToUpper(); Console.WriteLine(); salir = (opcion == "T"); switch (opcion) { case "1": // Añadir tipoGasto nuevoRegistro; string leerFecha; bool fechaCorrecta = true; do { Console.Write("Fecha (Formato --> AAAAMMDD): "); leerFecha = Console.ReadLine(); int anyoNumero = Convert.ToInt32(leerFecha.Substring(0, 4)); int mesNumero = Convert.ToInt32(leerFecha.Substring(4, 2)); int diaNumero = Convert.ToInt32(leerFecha.Substring(6, 2)); if (((anyoNumero < 1000) || (anyoNumero > 3000)) || ((mesNumero < 1) || (mesNumero > 12)) || ((diaNumero < 1) || (diaNumero > 31))) { fechaCorrecta = false; Console.WriteLine("FORMATO DE FECHA ERRÓNEO"); Console.WriteLine(); } nuevoRegistro.fecha = leerFecha; }while (!fechaCorrecta); do { Console.Write("Descripción: "); nuevoRegistro.descripcion = Console.ReadLine(); if (nuevoRegistro.descripcion == "") { Console.WriteLine("No puede estar vacía"); Console.WriteLine(); } }while (nuevoRegistro.descripcion == ""); Console.Write("Categoría: "); nuevoRegistro.categoria = Console.ReadLine(); Console.Write("Ingreso: "); nuevoRegistro.importe = Convert.ToDouble(Console.ReadLine()); Console.WriteLine(nuevoRegistro.importe); gastos.Add(nuevoRegistro); salvarDato(nuevoRegistro); break; case "2": // Mostrar una categoría Console.WriteLine("Mostrar los datos"); Console.Write("Escriba la \"Categoría\" a buscar: "); string categoriaBuscar = Console.ReadLine(); Console.WriteLine("Escriba rango de fechas: "); Console.Write("Desde: "); int fechaDesde = Convert.ToInt32(Console.ReadLine()); Console.Write("Hasta: "); int fechaHasta = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(); double totalIngresos = 0; for (int i = 0; i < gastos.Count; i++) { if ((gastos[i].categoria == categoriaBuscar) && (Convert.ToInt32(gastos[i].fecha) >= fechaDesde) && (Convert.ToInt32(gastos[i].fecha) <= fechaHasta)) { string anyo = gastos[i].fecha.Substring(0, 4); string mes = gastos[i].fecha.Substring(4, 2); string dia = gastos[i].fecha.Substring(6, 2); Console.Write((i + 1) + " - " + dia + "/" + mes + "/" + anyo + " - " + gastos[i].descripcion + " (" + gastos[i].categoria + ") - " + gastos[i].importe.ToString("N2")); totalIngresos += gastos[i].importe; Console.WriteLine(); } } Console.WriteLine(); Console.WriteLine("TOTAL INGRESOS: {0}" , totalIngresos.ToString("N2")); totalIngresos = 0; break; case "3": // Buscar Console.WriteLine("Buscar gastos"); Console.Write("Descripción o Categoría: "); string buscarGastos = Console.ReadLine().ToUpper(); Console.WriteLine(); for (int i = 0; i < gastos.Count; i++) { // Localizamos el sexto espacio, si existe string descripcionMostrar = gastos[i].descripcion; int espaciosEncontrados = 0; int pos = 0; while (pos < descripcionMostrar.Length && espaciosEncontrados < 6) { if (descripcionMostrar[pos] == ' ') { espaciosEncontrados++; } pos++; } if (espaciosEncontrados == 6) { descripcionMostrar = descripcionMostrar.Substring( 0, pos - 1); } // Y finalmente mostramos if ((gastos[i].descripcion.ToUpper().Contains(buscarGastos)) || (gastos[i].categoria.ToUpper().Contains(buscarGastos))) { Console.Write("Nº " + (i + 1) + " - Fecha: " + gastos[i].fecha + " - Descripción: " + descripcionMostrar); Console.WriteLine(); } } break; case "4": // Modificar Console.WriteLine("Modificar ingreso"); Console.Write("Introduzca nº Ingreso: "); int modRegistro = Convert.ToInt32(Console.ReadLine()) - 1; Console.WriteLine(); if (modRegistro >= gastos.Count) { Console.WriteLine("Registro no encontrado"); } else { string respuesta; tipoGasto registroModificar = gastos[modRegistro]; Console.WriteLine("Fecha: {0}", gastos[modRegistro].fecha); Console.Write("Fecha (Intro = NO modifica): "); respuesta = Console.ReadLine(); if (respuesta != "") { registroModificar.fecha = respuesta; } Console.WriteLine(); Console.WriteLine("Descripción anterior: {0}", gastos[modRegistro].descripcion); Console.Write("Descripción (Intro = NO modifica): "); respuesta = Console.ReadLine(); if (respuesta != "") { registroModificar.descripcion = respuesta; } Console.WriteLine(); Console.WriteLine("Categoría anterior: {0}", gastos[modRegistro].categoria); Console.Write("Categoría (Intro = NO modifica): "); respuesta = Console.ReadLine(); if (respuesta != "") { registroModificar.categoria = respuesta; } Console.WriteLine(); Console.WriteLine("Importe anterior: {0}", gastos[modRegistro].importe.ToString("N2")); Console.Write("Importe (Intro = NO modifica): "); respuesta = Console.ReadLine(); if (respuesta != "") { registroModificar.importe = Convert.ToDouble(respuesta); } Console.WriteLine(); gastos[modRegistro] = registroModificar; } guardarTodosLosDatos(gastos); break; case "5": // Borrar Console.Write("Eliminar Nº Registro: "); int posicBorrar = Convert.ToInt32(Console.ReadLine()) - 1; if (posicBorrar >= gastos.Count) { Console.WriteLine("Registro no encontrado"); } else { Console.WriteLine("Desea borrar el Registro: \"{0}\"", posicBorrar + 1); Console.Write("Nº " + (posicBorrar + 1) + " - Fecha: " + gastos[posicBorrar].fecha + " - Descripción: " + gastos[posicBorrar].descripcion + " - Categoría: (" + gastos[posicBorrar].categoria + ") - Importe: " + gastos[posicBorrar].importe.ToString("N2")); Console.WriteLine(); Console.WriteLine("'S' = Confirmar / 'N' = Cancelar"); string confirmar = Console.ReadLine().ToUpper(); if (confirmar == "S") { gastos.RemoveAt(posicBorrar); Console.WriteLine("CONFIRMADO"); Console.WriteLine("Ha borrado el registro: {0}", posicBorrar + 1); } else { Console.WriteLine("CANCELADO"); } } guardarTodosLosDatos(gastos); break; case "6": // Ordenar for (int i = 0; i < gastos.Count - 1; i++) { for (int j = i + 1; j < gastos.Count; j++) { if ((gastos[i].fecha.CompareTo(gastos[j].fecha) > 0) || ((gastos[i].fecha == gastos[j].fecha) && (String.Compare(gastos[i].descripcion, gastos[j].descripcion, true) > 0))) { tipoGasto aux = gastos[i]; gastos[i] = gastos[j]; gastos[j] = aux; } } } Console.WriteLine("Datos ordenados"); guardarTodosLosDatos(gastos); break; case "7": // Normalizar descripciones Console.WriteLine("Vamos a proceder a normalizar las " + "descripciones"); Console.WriteLine(); for (int x = 0; x < gastos.Count; x++) { tipoGasto c = gastos[x]; // Espacios iniciales y finales c.descripcion = c.descripcion.Trim(); // Espacios intermedios redundantes while (c.descripcion.Contains(" ")) { c.descripcion = c.descripcion.Replace(" ", " "); } // Corregir mayúscuas if (c.descripcion == c.descripcion.ToUpper()) { c.descripcion = c.descripcion.Substring(0, 1) + c.descripcion.Substring(1).ToLower(); } // Y volcamos los cambios gastos[x] = c; } Console.WriteLine("Descripciones normalizadas!"); guardarTodosLosDatos(gastos); break; case "T": salir = true; break; default: Console.WriteLine("Opción no encontrada"); Console.WriteLine(); break; } }while (!salir); Console.WriteLine("¡Hasta otra!"); }
static void Main() { byte opcion; const int CAPACIDAD = 1000; int cantidad = 0; tipoGasto[] gastos = new tipoGasto[CAPACIDAD]; do { Console.WriteLine("1. Añadir un gasto "); Console.WriteLine("2. Ver informes "); Console.WriteLine("3. Buscar "); Console.WriteLine("0. Salir "); opcion = Convert.ToByte(Console.ReadLine()); switch (opcion) { case 1: if (cantidad >= CAPACIDAD) { Console.WriteLine("Base de datos llena"); } else { Console.Write("Descripcion: "); gastos[cantidad].descripcion = Console.ReadLine(); Console.Write("Categoria: "); gastos[cantidad].categoria = Console.ReadLine(); Console.Write("Importe: "); gastos[cantidad].importe = Convert.ToDouble(Console.ReadLine()); Console.Write("Dia: "); gastos[cantidad].fecha.dia = Convert.ToInt32(Console.ReadLine()); Console.Write("Mes: "); gastos[cantidad].fecha.mes = Convert.ToInt32(Console.ReadLine()); Console.Write("Año: "); gastos[cantidad].fecha.anyo = Convert.ToInt32(Console.ReadLine()); cantidad++; } break; case 2: Console.WriteLine("Elige opción "); Console.WriteLine("1. Buscar por categoría"); Console.WriteLine("2. Buscar por mes"); Console.WriteLine("3. Buscar entre dos fechas"); string opcion2 = Console.ReadLine(); if (opcion2 == "1") { Console.Write("Categoría a buscar"); string buscarCategoria = Console.ReadLine(); for (int i = 0; i < cantidad; i++) { if (gastos[i].categoria.ToUpper() == buscarCategoria.ToUpper()) { Console.WriteLine("descripción: " + gastos[i].descripcion); Console.WriteLine("Categoria: " + gastos[i].categoria); Console.WriteLine("Importe: " + gastos[i].importe); Console.WriteLine("Fecha: " + gastos[i].fecha.dia + "/" + gastos[i].fecha.mes + "/" + gastos[i].fecha.anyo); } } } else if (opcion2 == "2") { Console.Write("Mes a buscar: "); int buscarMes = Convert.ToInt32(Console.ReadLine()); for (int i = 0; i < cantidad; i++) { if (buscarMes == gastos[i].fecha.mes) { Console.WriteLine("descripción: " + gastos[i].descripcion); Console.WriteLine("Categoria: " + gastos[i].categoria); Console.WriteLine("Importe: " + gastos[i].importe); Console.WriteLine("Fecha: " + gastos[i].fecha.dia + "/" + gastos[i].fecha.mes + "/" + gastos[i].fecha.anyo); } } } else if (opcion2 == "3") { Console.WriteLine("Opción no disponible"); } else { Console.WriteLine("Opción incorrecta"); } break; case 3: if (cantidad == 0) { Console.WriteLine("No hay suficientes datos donde buscar"); } else { bool found = false; Console.Write("Introduce el texto que buscar: "); string busqueda = Console.ReadLine(); for (int i = 0; i < cantidad; i++) { if (gastos[i].descripcion.ToLower().Contains( busqueda.ToLower())) { Console.WriteLine((i + 1) + ": " + gastos[i].descripcion + " - " + gastos[i].categoria + " - " + gastos[i].importe + " - " + gastos[i].fecha.dia + "/" + gastos[i].fecha.mes + "/" + gastos[i].fecha.anyo); found = true; } } if (!found) { Console.WriteLine("No encontrado"); } } break; case 0: Console.WriteLine("Hasta otra!"); break; default: Console.WriteLine("Opción no válida"); break; } } while (opcion != 0); }