This repository was archived by the owner on Jun 19, 2021. It is now read-only.
forked from pmadridb/lcdrefactor
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathImpresorLCD.java
362 lines (314 loc) · 10.4 KB
/
ImpresorLCD.java
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ImpresorLCD {
// Puntos fijos
private final int[] pf1;
private final int[] pf2;
private final int[] pf3;
private final int[] pf4;
private final int[] pf5;
private String[][] matrizImpr;
static final String CARACTER_VERTICAL = "|";
static final String CARACTER_HORIZONTAL = "-";
static final String POSICION_X = "X";
static final String POSICION_Y = "Y";
// TODO code application logic here
//String entrada = JOptionPane.showInputDialog("Digite el numero");
private int size;
// Calcula el numero de filasDig
private int filasDig;
private int columDig;
private int totalFilas;
private int totalColum;
public ImpresorLCD() {
// Inicializa variables
this.pf1 = new int[2];
this.pf2 = new int[2];
this.pf3 = new int[2];
this.pf4 = new int[2];
this.pf5 = new int[2];
}
/**
*
* Metodo encargado de añadir una linea a la matriz de Impresion
*
* @param matriz Matriz Impresion
* @param punto Punto Pivote
* @param posFija Posicion Fija
* @param size Tamaño Segmento
* @param caracter Caracter Segmento
*/
private void adicionarLinea(String[][] matriz, int[] punto, String posFija,
int size, String caracter) {
if (posFija.equalsIgnoreCase(POSICION_X))
{
for (int y = 1; y <= size; y++)
{
int valor = punto[1] + y;
matriz[punto[0]][valor] = caracter;
}
}
else
{
for (int i = 1; i <= size; i++)
{
int valor = punto[0] + i;
matriz[valor][punto[1]] = caracter;
}
}
}
/**
*
* Metodo encargado de un segmento a la matriz de Impresion
*
* @param segmento Segmento a adicionar
*/
private void adicionarSegmento(int segmento) {
switch (segmento) {
case 1:
adicionarLinea(this.matrizImpr, this.pf1, POSICION_Y,
this.size, CARACTER_VERTICAL);
break;
case 2:
adicionarLinea(this.matrizImpr, this.pf2, POSICION_Y,
this.size, CARACTER_VERTICAL);
break;
case 3:
adicionarLinea(this.matrizImpr, this.pf5, POSICION_Y,
this.size, CARACTER_VERTICAL);
break;
case 4:
adicionarLinea(this.matrizImpr, this.pf4, POSICION_Y,
this.size, CARACTER_VERTICAL);
break;
case 5:
adicionarLinea(this.matrizImpr, this.pf1, POSICION_X,
this.size, CARACTER_HORIZONTAL);
break;
case 6:
adicionarLinea(this.matrizImpr, this.pf2, POSICION_X,
this.size, CARACTER_HORIZONTAL);
break;
case 7:
adicionarLinea(this.matrizImpr, this.pf3, POSICION_X,
this.size, CARACTER_HORIZONTAL);
break;
default:
break;
}
}
/**
*
* Metodo encargado de definir los segmentos que componen un digito y
* a partir de los segmentos adicionar la representacion del digito a la matriz
*
* @param numero Digito
*/
private void adicionarDigito(int numero) {
// Establece los segmentos de cada numero
List<Integer> segList = new ArrayList<>();
switch (numero) {
case 1:
segList.add(3);
segList.add(4);
break;
case 2:
segList.add(5);
segList.add(3);
segList.add(6);
segList.add(2);
segList.add(7);
break;
case 3:
segList.add(5);
segList.add(3);
segList.add(6);
segList.add(4);
segList.add(7);
break;
case 4:
segList.add(1);
segList.add(6);
segList.add(3);
segList.add(4);
break;
case 5:
segList.add(5);
segList.add(1);
segList.add(6);
segList.add(4);
segList.add(7);
break;
case 6:
segList.add(5);
segList.add(1);
segList.add(6);
segList.add(2);
segList.add(7);
segList.add(4);
break;
case 7:
segList.add(5);
segList.add(3);
segList.add(4);
break;
case 8:
segList.add(1);
segList.add(2);
segList.add(3);
segList.add(4);
segList.add(5);
segList.add(6);
segList.add(7);
break;
case 9:
segList.add(1);
segList.add(3);
segList.add(4);
segList.add(5);
segList.add(6);
segList.add(7);
break;
case 0:
segList.add(1);
segList.add(2);
segList.add(3);
segList.add(4);
segList.add(5);
segList.add(7);
break;
default:
break;
}
Iterator<Integer> iterator = segList.iterator();
while (iterator.hasNext()) {
adicionarSegmento(iterator.next());
}
}
/**
*
* Metodo encargado de imprimir un numero
*
* @param size Tamaño Segmento Digitos
* @param numeroImp Numero a Imprimir
* @param espacio Espacio Entre digitos
*/
private void imprimirNumero(int size, String numeroImp, int espacio)
{
int pivotX = 0;
char[] digitos;
this.size = size;
// Calcula el numero de filas cada digito
this.filasDig = (2 * this.size) + 3;
// Calcula el numero de columna de cada digito
this.columDig = this.size + 2;
// Calcula el total de filas de la matriz en la que se almacenaran los digitos
this.totalFilas = this.filasDig;
// Calcula el total de columnas de la matriz en la que se almacenaran los digitos
this.totalColum = (this.columDig * numeroImp.length())
+ (espacio * numeroImp.length());
// crea matriz para almacenar los numero a imprimir
this.matrizImpr = new String[this.totalFilas][this.totalColum];
// crea el arreglo de digitos
digitos = numeroImp.toCharArray();
// Inicializa matriz
for (int i = 0; i < this.totalFilas; i++) {
for (int j = 0; j < this.totalColum; j++) {
this.matrizImpr[i][j] = " ";
}
}
for (char digito : digitos) {
//Valida que el caracter sea un digito
if( ! Character.isDigit(digito))
{
throw new IllegalArgumentException("Caracter " + digito
+ " no es un digito");
}
int numero = Integer.parseInt(String.valueOf(digito));
//Calcula puntos fijos
this.pf1[0] = 0;
this.pf1[1] = 0 + pivotX;
this.pf2[0] = (this.filasDig / 2);
this.pf2[1] = 0 + pivotX;
this.pf3[0] = (this.filasDig - 1);
this.pf3[1] = 0 + pivotX;
this.pf4[0] = (this.columDig - 1);
this.pf4[1] = (this.filasDig / 2) + pivotX;
this.pf5[0] = 0;
this.pf5[1] = (this.columDig - 1) + pivotX;
pivotX = pivotX + this.columDig + espacio;
adicionarDigito(numero);
}
// Imprime matriz
for (int i = 0; i < this.totalFilas; i++) {
for (int j = 0; j < this.totalColum; j++) {
System.out.print(this.matrizImpr[i][j]);
}
System.out.println();
}
}
/**
*
* Metodo encargado de procesar la entrada que contiene el size del segmento
* de los digitos y los digitos a imprimir
*
* @param comando Entrada que contiene el size del segmento de los digito
* y el numero a imprimir
* @param espacioDig Espacio Entre digitos
*/
public void procesar(String comando, int espacioDig) {
String[] parametros;
int tam;
if (!comando.contains(",")) {
throw new IllegalArgumentException("Cadena " + comando
+ " no contiene caracter ,");
}
//Se hace el split de la cadena
parametros = comando.split(",");
//Valida la cantidad de parametros
if(parametros.length>2)
{
throw new IllegalArgumentException("Cadena " + comando
+ " contiene mas caracter ,");
}
//Valida la cantidad de parametros
if(parametros.length<2)
{
throw new IllegalArgumentException("Cadena " + comando
+ " no contiene los parametros requeridos");
}
//Valida que el parametro size sea un numerico
if(isNumeric(parametros[0]))
{
tam = Integer.parseInt(parametros[0]);
// se valida que el size este entre 1 y 10
if(tam <1 || tam >10)
{
throw new IllegalArgumentException("El parametro size ["+tam
+ "] debe estar entre 1 y 10");
}
}
else
{
throw new IllegalArgumentException("Parametro Size [" + parametros[0]
+ "] no es un numero");
}
// Realiza la impresion del numero
imprimirNumero(tam, parametros[1],espacioDig);
}
/**
*
* Metodo encargado de validar si una cadena es numerica
*
* @param cadena Cadena
*/
static boolean isNumeric(String cadena) {
try {
Integer.parseInt(cadena);
return true;
} catch (NumberFormatException ex) {
return false;
}
}
}