• Lenguaje

    Java

  • Descripción

    Registro de clientes

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
//Guarda este código en un archivo llamado Main.java
//package ....clientes;

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 = "clientes.tsv";

    public static void main(String[] args) {

        ForEachFunction<Cliente> print = new ForEachFunction<Cliente>() {
            @Override
            public void call(Cliente cliente, Object params) {
                out.println(cliente);
                int[] counter = (int[]) params;
                counter[0]++;
            }
        };
        ForEachFunction<Cliente> printOnFile = new ForEachFunction<Cliente>() {
            @Override
            public void call(Cliente cliente, Object params) {
                PrintStream file_stream = (PrintStream) params;
                file_stream.print(cliente.getRFC() + "\t");
                file_stream.print(cliente.getEstado() + "\t");
                file_stream.print(cliente.getTelefono() + "\t");
                file_stream.print(cliente.getMunicipio() + "\t");
                file_stream.print(cliente.getRazon_social() + "\t");
                file_stream.print(cliente.getCodigo_postal() + "\t");
                file_stream.print(cliente.getDireccion_fiscal() + "\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<Cliente> vector = new Vector<Cliente>();
        int n;
        Cliente datum = null, cliente;
        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");
                cliente = new Cliente();
                cliente.setRFC(fields[0]);
                cliente.setEstado(fields[1]);
                cliente.setTelefono(fields[2]);
                cliente.setMunicipio(fields[3]);
                cliente.setRazon_social(fields[4]);
                cliente.setCodigo_postal(fields[5]);
                cliente.setDireccion_fiscal(fields[6]);
                vector.add(cliente);
            }
            file_stream.close();
        } catch (FileNotFoundException e) {}
        cliente = new Cliente();
        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) {
                cliente.setRFC(read_str ("Ingresa el RFC del cliente"));
                i = vector.indexOf(cliente);
                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:
                cliente.setEstado(read_str ("Ingresa el estado"));
                cliente.setTelefono(read_str ("Ingresa el telefono"));
                cliente.setMunicipio(read_str ("Ingresa el municipio"));
                cliente.setRazon_social(read_str ("Ingresa el razon social"));
                cliente.setCodigo_postal(read_str ("Ingresa el codigo postal"));
                cliente.setDireccion_fiscal(read_str ("Ingresa el direccion fiscal"));
                vector.add(cliente);
                cliente = new Cliente();
                out.println("\nRegistro agregado correctamente.");
                break;
            case 3:
                out.println("Men\u00FA de actualizaci\u00F3n de campos");
                out.println("1.- estado");
                out.println("2.- telefono");
                out.println("3.- municipio");
                out.println("4.- razon social");
                out.println("5.- codigo postal");
                out.println("6.- direccion fiscal");
                do {
                    suboption = read_integer ("Selecciona el n\u00FAmero de campo a modificar");
                    if (suboption<1 || suboption>6)
                        out.println("Opci\u00F3n incorrecta");
                } while (suboption<1 || suboption>6);
                switch (suboption) {
                    case 1:
                        datum.setEstado(read_str ("Ingresa el nuevo estado"));
                        break;
                    case 2:
                        datum.setTelefono(read_str ("Ingresa el nuevo telefono"));
                        break;
                    case 3:
                        datum.setMunicipio(read_str ("Ingresa el nuevo municipio"));
                        break;
                    case 4:
                        datum.setRazon_social(read_str ("Ingresa el nuevo razon social"));
                        break;
                    case 5:
                        datum.setCodigo_postal(read_str ("Ingresa el nuevo codigo postal"));
                        break;
                    case 6:
                        datum.setDireccion_fiscal(read_str ("Ingresa el nuevo direccion fiscal"));
                        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 Cliente implements Comparable<Cliente> {

    private String RFC;
    private String estado;
    private String telefono;
    private String municipio;
    private String razon_social;
    private String codigo_postal;
    private String direccion_fiscal;

    @Override
    public boolean equals(Object cliente) {
        return this==cliente || (cliente instanceof Cliente && RFC.equals(((Cliente)cliente).RFC));
    }

    @Override
    public int compareTo(Cliente cliente) {
        return RFC.compareTo(cliente.RFC);
    }

    @Override
    public String toString() {
        return
            "RFC             : " + RFC + "\n" +
            "estado          : " + estado + "\n" +
            "telefono        : " + telefono + "\n" +
            "municipio       : " + municipio + "\n" +
            "razon social    : " + razon_social + "\n" +
            "codigo postal   : " + codigo_postal + "\n" +
            "direccion fiscal: " + direccion_fiscal + "\n";
    }

    public String getRFC() {
        return RFC;
    }

    public void setRFC(String RFC) {
        this.RFC = RFC;
    }

    public String getEstado() {
        return estado;
    }

    public void setEstado(String estado) {
        this.estado = estado;
    }

    public String getTelefono() {
        return telefono;
    }

    public void setTelefono(String telefono) {
        this.telefono = telefono;
    }

    public String getMunicipio() {
        return municipio;
    }

    public void setMunicipio(String municipio) {
        this.municipio = municipio;
    }

    public String getRazon_social() {
        return razon_social;
    }

    public void setRazon_social(String razon_social) {
        this.razon_social = razon_social;
    }

    public String getCodigo_postal() {
        return codigo_postal;
    }

    public void setCodigo_postal(String codigo_postal) {
        this.codigo_postal = codigo_postal;
    }

    public String getDireccion_fiscal() {
        return direccion_fiscal;
    }

    public void setDireccion_fiscal(String direccion_fiscal) {
        this.direccion_fiscal = direccion_fiscal;
    }
}