• Lenguaje

    Java

  • Descripción

    Programa de repartición de despensas, que lleva:
    - Fecha.
    - Nombre del beneficiado.
    - Edad.
    - Dirección.
    - Número de integrantes de su familia.
    - Tipo de sangre.
    Se deberán guardar los datos y presentar pantalla limpia en cada captura.

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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//Guarda este código en un archivo llamado Main.java
//package ....beneficiados;

import java.io.*;
import java.util.*;

public class Main {

    public static Scanner keyboard = new Scanner(System.in);
    public static PrintStream out = System.out;

    public static void pause_screen(String mensage) {
        out.print(mensage + "\nPresiona <ENTER> para continuar . . . ");
        keyboard.nextLine();
        out.println();
    }

    public static String read_str(String message) {
        out.print(message + ": ");
        return keyboard.nextLine();
    }

    public static int read_integer(String message) {
        try {
            return Integer.parseInt(read_str(message));
        } catch (NumberFormatException e) {
            out.print("N\u00FAmero incorrecto");
            return read_integer(message);
        }
    }

    public static String path = "beneficiados.tsv";

    public static void main(String[] args) {

        ForEachFunction<Beneficiado> print = new ForEachFunction<Beneficiado>() {
            @Override
            public void call(Beneficiado beneficiado, Object params) {
                out.println(beneficiado);
                int[] counter = (int[]) params;
                counter[0]++;
            }
        };
        ForEachFunction<Beneficiado> printOnFile = new ForEachFunction<Beneficiado>() {
            @Override
            public void call(Beneficiado beneficiado, Object params) {
                PrintStream file_stream = (PrintStream) params;
                file_stream.print(beneficiado.getNombre() + "\t");
                file_stream.print(beneficiado.getDia() + "\t");
                file_stream.print(beneficiado.getMes() + "\t");
                file_stream.print(beneficiado.getAno() + "\t");
                file_stream.print(beneficiado.getEdad() + "\t");
                file_stream.print(beneficiado.getDireccion() + "\t");
                file_stream.print(beneficiado.getIntegrantes_de_familia() + "\t");
                file_stream.print(beneficiado.getTipo_de_sangre() + "\n");
            }
        };
        if(!System.getProperties().get("os.name").equals("Linux") && System.console()!=null)
            try {
                out = new PrintStream(System.out, true, "CP850");
                keyboard = new Scanner(System.in, "CP850");
            } catch (UnsupportedEncodingException e) {}
        Vector<Beneficiado> vector = new Vector<Beneficiado>();
        int n;
        Beneficiado datum = null, beneficiado;
        int[] counter = {0};
        int i, main_option, suboption;
        String[] fields;
        try {
            Scanner file_stream = new Scanner(new FileReader(path));
            while (file_stream.hasNextLine()) {
                fields = file_stream.nextLine().split("\t");
                beneficiado = new Beneficiado();
                beneficiado.setNombre(fields[0]);
                beneficiado.setDia(Integer.parseInt(fields[1]));
                beneficiado.setMes(Integer.parseInt(fields[2]));
                beneficiado.setAno(Integer.parseInt(fields[3]));
                beneficiado.setEdad(Integer.parseInt(fields[4]));
                beneficiado.setDireccion(fields[5]);
                beneficiado.setIntegrantes_de_familia(Integer.parseInt(fields[6]));
                beneficiado.setTipo_de_sangre(fields[7]);
                vector.add(beneficiado);
            }
            file_stream.close();
        } catch (FileNotFoundException e) {}
        beneficiado = new Beneficiado();
        do {
            out.println("MEN\u00DA");
            out.println("1.- Altas");
            out.println("2.- Consultas");
            out.println("3.- Actualizaciones");
            out.println("4.- Bajas");
            out.println("5.- Ordenar registros");
            out.println("6.- Listar registros");
            out.println("7.- Salir");
            do {
                main_option = read_integer ("Selecciona una opci\u00F3n");
                if(main_option<1 || main_option>7)
                    out.println("Opci\u00F3n incorrecta");
            } while (main_option<1 || main_option>7);
            out.println();
            if (vector.isEmpty() && main_option!=1 && main_option!=7) {
                pause_screen("No hay registros.\n");
                continue;
            }
            if (main_option<5) {
                beneficiado.setNombre(read_str ("Ingresa el nombre del beneficiado"));
                i = vector.indexOf(beneficiado);
                datum = i<0 ? null : vector.get(i);
                if (datum!=null) {
                    out.println();
                    print.call(datum, counter);
                }
            }
            if (main_option==1 && datum!=null)
                out.println("El registro ya existe.");
            else if (main_option>=2 && main_option<=4 && datum==null)
                out.println("\nRegistro no encontrado.");
            else switch (main_option) {
            case 1:
                beneficiado.setDia(read_integer ("Ingresa el dia"));
                beneficiado.setMes(read_integer ("Ingresa el mes"));
                beneficiado.setAno(read_integer ("Ingresa el ano"));
                beneficiado.setEdad(read_integer ("Ingresa el edad"));
                beneficiado.setDireccion(read_str ("Ingresa el direccion"));
                beneficiado.setIntegrantes_de_familia(read_integer ("Ingresa el integrantes de familia"));
                beneficiado.setTipo_de_sangre(read_str ("Ingresa el tipo de sangre"));
                vector.add(beneficiado);
                beneficiado = new Beneficiado();
                out.println("\nRegistro agregado correctamente.");
                break;
            case 3:
                out.println("Men\u00FA de actualizaci\u00F3n de campos");
                out.println("1.- dia");
                out.println("2.- mes");
                out.println("3.- ano");
                out.println("4.- edad");
                out.println("5.- direccion");
                out.println("6.- integrantes de familia");
                out.println("7.- tipo de sangre");
                do {
                    suboption = read_integer ("Selecciona el n\u00FAmero de campo a modificar");
                    if (suboption<1 || suboption>7)
                        out.println("Opci\u00F3n incorrecta");
                } while (suboption<1 || suboption>7);
                switch (suboption) {
                    case 1:
                        datum.setDia(read_integer ("Ingresa el nuevo dia"));
                        break;
                    case 2:
                        datum.setMes(read_integer ("Ingresa el nuevo mes"));
                        break;
                    case 3:
                        datum.setAno(read_integer ("Ingresa el nuevo ano"));
                        break;
                    case 4:
                        datum.setEdad(read_integer ("Ingresa el nuevo edad"));
                        break;
                    case 5:
                        datum.setDireccion(read_str ("Ingresa el nuevo direccion"));
                        break;
                    case 6:
                        datum.setIntegrantes_de_familia(read_integer ("Ingresa el nuevo integrantes de familia"));
                        break;
                    case 7:
                        datum.setTipo_de_sangre(read_str ("Ingresa el nuevo tipo de sangre"));
                        break;
                }
                out.println("\nRegistro actualizado correctamente.");
                break;
            case 4:
                vector.remove(datum);
                out.println("Registro eliminado correctamente.");
                break;
            case 5:
                Collections.sort(vector);
                out.println("Registros ordenados correctamente.");
                break;
            case 6:
                n = vector.size();
                counter[0] = 0;
                for (i=0; i<n; i++)
                    print.call(vector.get(i), counter);
                out.println("Total de registros: " + counter[0] + ".");
                break;
            }
            if (main_option<7 && main_option>=1)
                pause_screen("");
        } while (main_option!=7);
        try {
            PrintStream output_stream = new PrintStream(path);
            n = vector.size();
            for (i=0; i<n; i++)
                printOnFile.call(vector.get(i), output_stream);
            output_stream.close();
        } catch (FileNotFoundException e) {}
    }
}

interface ForEachFunction<T extends Comparable<T>> {
    void call(T datum, Object params);
}

class Beneficiado implements Comparable<Beneficiado> {

    private String nombre;
    private int dia;
    private int mes;
    private int ano;
    private int edad;
    private String direccion;
    private int integrantes_de_familia;
    private String tipo_de_sangre;

    @Override
    public boolean equals(Object beneficiado) {
        return this==beneficiado || (beneficiado instanceof Beneficiado && nombre.equals(((Beneficiado)beneficiado).nombre));
    }

    @Override
    public int compareTo(Beneficiado beneficiado) {
        return nombre.compareTo(beneficiado.nombre);
    }

    @Override
    public String toString() {
        return
            "nombre                : " + nombre + "\n" +
            "dia                   : " + dia + "\n" +
            "mes                   : " + mes + "\n" +
            "ano                   : " + ano + "\n" +
            "edad                  : " + edad + "\n" +
            "direccion             : " + direccion + "\n" +
            "integrantes de familia: " + integrantes_de_familia + "\n" +
            "tipo de sangre        : " + tipo_de_sangre + "\n";
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public int getDia() {
        return dia;
    }

    public void setDia(int dia) {
        this.dia = dia;
    }

    public int getMes() {
        return mes;
    }

    public void setMes(int mes) {
        this.mes = mes;
    }

    public int getAno() {
        return ano;
    }

    public void setAno(int ano) {
        this.ano = ano;
    }

    public int getEdad() {
        return edad;
    }

    public void setEdad(int edad) {
        this.edad = edad;
    }

    public String getDireccion() {
        return direccion;
    }

    public void setDireccion(String direccion) {
        this.direccion = direccion;
    }

    public int getIntegrantes_de_familia() {
        return integrantes_de_familia;
    }

    public void setIntegrantes_de_familia(int integrantes_de_familia) {
        this.integrantes_de_familia = integrantes_de_familia;
    }

    public String getTipo_de_sangre() {
        return tipo_de_sangre;
    }

    public void setTipo_de_sangre(String tipo_de_sangre) {
        this.tipo_de_sangre = tipo_de_sangre;
    }
}