-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculator.java
698 lines (655 loc) · 20.8 KB
/
Calculator.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
import java.util.Scanner;
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Calculator {
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
int option;
do {
System.out.print("\tOperation menu\t\n");
System.out.print(
"\n 1. Basics operations\n 2. Conversions\n 3. Matrix Operations\n 4. Other Operations\n 5. Exit");
System.out.print("\nGive me an option: ");
try {
option = input.nextInt();
} catch (Exception e) {
System.out.println("Invalid option\n");
main(args);
break;
}
switch (option) {
case 1:
System.out.println(
"\tBasics operations menu\n 1. Add\n 2. Substraction\n 3. Multiplication\n 4. Decimal division\n 5. Division with integer and remainder\n 6. Power\n 7. Cancel");
System.out.print("\nGive me an option: ");
option = input.nextInt();
basicsOperations(option);
break;
case 2:
System.out.println(
"\tConversions menu\n 1. Decimal to Binary\n 2. Decimal to Hexadecimal\n 3. Binary to Decimal\n 4. Binary to Hexadecimal\n 5. Hexadecimal to Decimal\n 6. Hexadecimal to Binary\n 7. Cancel");
System.out.print("\nGive me an option: ");
option = input.nextInt();
conversions(option);
break;
case 3:
System.out.println(
"\tMatrix Operations menu\n 1. Add of 2 matrix\n 2. Substraction of 2 matrix\n 3. Transpose matrix of any size\n 4. Multiplication od 2 matrix\n 5. Cancel");
System.out.print("\nGive me an option: ");
option = input.nextInt();
matrixOperations(option);
break;
case 4:
System.out.println(
"\tOther Operations menu\n 1. Order N prime numbers\n 2. Order N numbers with Fibonacci sequence\n 3. Age of a person\n 4. Obtain the IVA in a product\n 5. Cancel");
System.out.print("\nGive me an option: ");
option = input.nextInt();
otherOperations(option);
break;
case 5:
methods.exit();
break;
default:
System.out.println("Invalid option\n");
break;
}
try {
if (option < 1 || option > 5) {
System.out.println("Invalid option\n");
main(args);
break;
}
} catch (Exception e) {
main(args);
break;
}
System.out.print("\nDo you want other operation? (1. Yes / Press other key for exit): ");
try {
option = input.nextInt();
if (option == 1) {
} else {
System.out.println("Invalid option\n");
}
} catch (Exception e) {
methods.exit();
break;
}
} while (option != 5);
}
public static void basicsOperations(int option) {
Integer data, power;
float result = 0, number, divisor, dividend;
switch (option) {
case 1:
// Add
System.out.print("How many numbers: ");
try {
data = Integer.parseInt(input.next());
} catch (Exception e) {
System.out.println("Ensure that you enter a number.");
basicsOperations(1);
break;
}
if (data < 2) {
System.out.println("The number must be greater than 1.");
basicsOperations(1);
break;
}
try {
for (int i = 0; i < data; i++) {
System.out.print("Number " + (i + 1) + ": ");
number = input.nextInt();
result += number;
}
} catch (Exception e) {
System.out.println("Ensure that you enter a number.");
basicsOperations(1);
break;
}
System.out.println("Result: " + result);
break;
case 2:
// Substraction
System.out.print("How many numbers: ");
try {
data = Integer.parseInt(input.next());
} catch (Exception e) {
System.out.println("Ensure that you enter a number.");
basicsOperations(2);
break;
}
if (data < 2) {
System.out.println("The number must be greater than 1.");
basicsOperations(2);
break;
}
System.out.print("Number 1: ");
try {
result = input.nextInt();
for (int i = 1; i < data; i++) {
System.out.print("Number " + (i + 1) + ": ");
number = input.nextInt();
result -= number;
}
} catch (Exception e) {
System.out.println("Ensure that you enter a number.");
basicsOperations(2);
break;
}
System.out.println("Result: " + result);
break;
case 3:
// Multiplication
result = 1;
System.out.print("How many numbers: ");
try {
data = Integer.parseInt(input.next());
if (data < 2) {
System.out.println("The number must be greater than 1.");
basicsOperations(3);
break;
}
} catch (Exception e) {
System.out.println("Ensure that you enter a number.");
basicsOperations(3);
break;
}
try {
for (int i = 1; i <= data; i++) {
System.out.print("Number " + i + ": ");
number = input.nextInt();
result = result * number;
}
} catch (Exception e) {
System.out.println("Ensure that you enter a number.");
basicsOperations(3);
break;
}
System.out.println("Result: " + result);
break;
case 4:
// Decimal division
try {
System.out.print("Dividend: ");
dividend = input.nextInt();
System.out.print("Divisor: ");
divisor = input.nextInt();
result = dividend / divisor;
} catch (Exception e) {
System.out.println("Ensure that you enter a number.");
basicsOperations(4);
break;
}
System.out.println("Result: " + result);
break;
case 5:
// Division with integer and remainder
try {
System.out.print("Dividend: ");
dividend = input.nextInt();
System.out.print("Divisor: ");
divisor = input.nextInt();
result = dividend / divisor;
} catch (Exception e) {
System.out.println("Ensure that you enter a number.");
basicsOperations(5);
break;
}
System.out.println("Result: " + (int) (result) + "\nResidue: " + (dividend % divisor));
break;
case 6:
// Power
try {
System.out.print("Number: ");
number = input.nextInt();
System.out.print("Power: ");
power = input.nextInt();
} catch (Exception e) {
System.out.println("Ensure that you enter a number.");
basicsOperations(6);
break;
}
System.out.println("Result: " + Math.pow(number, power));
break;
case 7:
// return
break;
}
}
public static void conversions(int option) {
String hexadecimalNumber, binaryNumber;
String numberStr;
BigInteger binary;
switch (option) {
case 1:
// Decimal to binary
System.out.print("Decimal: ");
numberStr = input.next();
if (!numberStr.matches("[0-9]+")) {
System.out.println("Ensure that you enter a number from 0-9.");
conversions(1);
break;
}
BigInteger bigDecimal = new BigInteger(numberStr, 10);
System.out.println("Binary: " + bigDecimal.toString(2));
break;
case 2:
// Decimal to hexadecimal
System.out.print("Decimal: ");
numberStr = input.next();
if (!numberStr.matches("[0-9]+")) {
System.out.println("Ensure that you enter a number from 0-9.");
conversions(1);
break;
}
bigDecimal = new BigInteger(numberStr, 10);
System.out.println("Hexadecimal: " + bigDecimal.toString(16).toUpperCase());
break;
case 3:
// Binary to decimal
System.out.print("Binary: ");
binaryNumber = input.next();
try {
binary = new BigInteger(binaryNumber, 2);
} catch (Exception e) {
System.out.println("Remember that the binary numbers are 0 and 1");
conversions(4);
break;
}
System.out.println("Decimal: " + binary.toString(10));
break;
case 4:
// binary to hexadecimal
System.out.print("Binary: ");
binaryNumber = input.next();
try {
binary = new BigInteger(binaryNumber, 2);
} catch (Exception e) {
System.out.println("Remember that the binary numbers are 0 and 1");
conversions(4);
break;
}
System.out.println("Hexadecimal: " + binary.toString(16).toUpperCase());
break;
case 5:
// hexadecimal to decimal
System.out.print("Hexadecimal: ");
try {
hexadecimalNumber = input.next();
if (!validationes.validHexadecimal(hexadecimalNumber)) {
System.out.println("Remember that the hexadecimal numbers are 0-9 and A-F");
conversions(5);
break;
}
} catch (Exception e) {
System.out.println("Remember that the hexadecimal numbers are 0-9 and A-F");
conversions(5);
break;
}
BigInteger hexadecimal = new BigInteger(hexadecimalNumber, 16);
System.out.println("Decimal: " + hexadecimal.toString(10));
break;
case 6:
// hexadecimal to binary
System.out.print("Hexadecimal: ");
try {
hexadecimalNumber = input.next();
if (!validationes.validHexadecimal(hexadecimalNumber)) {
System.out.println("Remember that the hexadecimal numbers are 0-9 and A-F");
conversions(5);
break;
}
} catch (Exception e) {
System.out.println("Remember that the hexadecimal numbers are 0-9 and A-F");
conversions(5);
break;
}
hexadecimal = new BigInteger(hexadecimalNumber, 16);
System.out.println("Binary: " + hexadecimal.toString(2));
break;
case 7:
// return
break;
}
}
public static void matrixOperations(int option) {
int rows = 0;
int cols = 0;
Matrix matrixOne;
Matrix matrixTwo;
switch (option) {
case 1:
// Add of 2 matrix
try {
System.out.print("Rows: ");
rows = input.nextInt();
System.out.print("Cols: ");
cols = input.nextInt();
matrixOne = new Matrix(rows, cols);
matrixTwo = new Matrix(rows, cols);
System.out.println("Matrix 1");
matrixOne.fill();
System.out.println("Matrix 2");
matrixTwo.fill();
} catch (Exception e) {
System.out.println("Ensure that you enter a number.");
matrixOperations(1);
break;
}
System.out.println(matrixOne.add(matrixTwo));
break;
case 2:
// Substraction of 2 matrix
try {
System.out.print("Enter rows: ");
rows = input.nextInt();
System.out.print("Enter cols: ");
cols = input.nextInt();
matrixOne = new Matrix(rows, cols);
matrixTwo = new Matrix(rows, cols);
System.out.println("Matrix 1");
matrixOne.fill();
System.out.println("Matrix 2");
matrixTwo.fill();
if (rows < 2 || cols < 2) {
System.out.println("Ensure that you enter a number greater than 1.");
matrixOperations(2);
break;
}
} catch (Exception e) {
System.out.println("Ensure that you enter a number.");
matrixOperations(2);
break;
}
System.out.println(matrixOne.subtract(matrixTwo));
break;
case 3:
// Transpose matrix of any size
try {
System.out.print("Enter rows: ");
rows = input.nextInt();
System.out.print("Enter cols: ");
cols = input.nextInt();
matrixOne = new Matrix(rows, cols);
System.out.println("Matrix 1");
matrixOne.fill();
if (rows < 2 || cols < 2) {
System.out.println("Ensure that you enter a number greater than 1.");
matrixOperations(3);
break;
}
} catch (Exception e) {
System.out.println("Ensure that you enter a number.");
matrixOperations(3);
break;
}
System.out.println(matrixOne.transpose());
break;
case 4:
// Multiplication of 2 matrix
System.out.println("Matrix 1");
try {
System.out.print("Enter rows: ");
rows = input.nextInt();
System.out.print("Enter cols: ");
cols = input.nextInt();
if (rows < 2 || cols < 2) {
System.out.println("Invalid rows or cols");
matrixOperations(4);
break;
}
} catch (Exception e) {
System.out.println("Ensure that you enter a number.");
matrixOperations(4);
break;
}
matrixOne = new Matrix(rows, cols);
matrixOne.fill();
try {
System.out.println("Matrix 2");
rows = cols;
System.out.print("Enter cols: ");
cols = input.nextInt();
} catch (Exception e) {
System.out.println("Ensure that you enter a number.");
matrixOperations(4);
break;
}
matrixTwo = new Matrix(rows, cols);
matrixTwo.fill();
System.out.println(matrixOne.multiply(matrixTwo));
break;
case 5:
// return
break;
}
}
public static void otherOperations(int option) {
int number;
switch (option) {
case 1:
// Order N prime numbers
int count = 0;
int i = 1;
try {
System.out.print("N numberes of primes: ");
number = input.nextInt();
System.out.println("Wait a moment...");
System.out.println("Primes:");
while (count < number) {
if (methods.esPrime(i)) {
System.out.println(i);
count++;
}
i++;
}
} catch (Exception e) {
System.out.println("Ensure that you enter a number greater or equal than 1.");
// return to option 1 from otherOperations
otherOperations(1);
}
break;
case 2:
// Order N numbers with Fibonacci sequence
System.out.print("Number: ");
try {
number = input.nextInt();
for (int j = 2; j < number + 2; j++) {
System.out.println(methods.fibonacci(j));
}
} catch (Exception e) {
System.out.println("Ensure that you enter a number greater or equal than 1.");
otherOperations(2);
break;
}
break;
case 3:
// Age of a person
Period time;
String birthdayDate;
LocalDate birthday;
LocalDate last;
System.out.print("Birthday (dd/mm/yyyy): ");
try {
birthdayDate = input.next();
DateTimeFormatter formattert = DateTimeFormatter.ofPattern("dd/MM/yyyy");
birthday = LocalDate.parse(birthdayDate, formattert);
String checkDate = birthday.format(formattert);
if (!birthdayDate.equals(checkDate)) {
System.out.println("Invalid date, last date is February 28");
otherOperations(3);
}
} catch (DateTimeParseException e) {
System.out.println("Invalid date");
otherOperations(3);
break;
}
try {
System.out.print("Last date (dd/mm/yyyy): ");
String lastDate = input.next();
DateTimeFormatter formattert = DateTimeFormatter.ofPattern("dd/MM/yyyy");
last = LocalDate.parse(lastDate, formattert);
String checkDate = last.format(formattert);
if (!lastDate.equals(checkDate)) {
System.out.println("Invalid date, last date is February 28");
otherOperations(3);
}
} catch (Exception e) {
System.out.println("Invalid date");
otherOperations(3);
break;
}
try {
time = Period.between(birthday, last);
if (birthday.isAfter(last)) {
System.out.println("Invalid date, birthday is after last date)");
otherOperations(3);
break;
}
} catch (Exception e) {
System.out.println("Invalid date");
otherOperations(3);
break;
}
System.out.printf("Tu edad es: %s años, %s meses y %s días", time.getYears(), time.getMonths(), time.getDays(),
"\n");
break;
case 4:
// Obtain the IVA in a product
System.out.print("Product price: ");
try {
number = input.nextInt();
if (number < 0) {
System.out.println("Invalid price");
otherOperations(4);
break;
}
} catch (Exception e) {
System.out.println("Ensure that you enter a number.");
otherOperations(4);
break;
}
System.out.println("Price: " + (number - (float) (number * 0.137931035)));
System.out.println("IVA: " + (float) (number * 0.137931035));
break;
case 5:
// return
break;
}
}
}
class methods {
public static Scanner input = new Scanner(System.in);
public static void exit() {
System.exit(0);
}
public static int fibonacci(int number) {
if (number <= 1) {
return number;
}
return fibonacci(number
- 1) + fibonacci(number - 2);
}
public static boolean esPrime(int numero) {
int counter = 2;
boolean prime = true;
while ((prime) && (counter != numero)) {
if (numero % counter == 0)
prime = false;
counter++;
}
return prime;
}
}
class validationes {
public static boolean validHexadecimal(String hexadecimal) {
Pattern pattern = Pattern.compile("[0-9A-Fa-f]+");
Matcher matcher = pattern.matcher(hexadecimal);
return matcher.matches();
}
}
class Matrix {
public static Scanner input = new Scanner(System.in);
private int rows;
private int cols;
private int[][] matrix;
public Matrix(int rows, int cols) {
this.rows = rows;
this.cols = cols;
this.matrix = new int[rows][cols];
}
public void setElement(int row, int col, int value) {
this.matrix[row][col] = value;
}
public int getElement(int row, int col) {
return this.matrix[row][col];
}
public void setRow(int row, int[] values) {
this.matrix[row] = values;
}
public String toString() {
String result = "";
for (int[] row : this.matrix) {
for (int col : row) {
result += col + " ";
}
result += "\n";
}
return result;
}
public void fill() {
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
System.out.print("Enter value for row " + i + " and col " + j + ": ");
int value = input.nextInt();
this.matrix[i][j] = value;
}
}
}
public Matrix transpose() {
Matrix transposedMatrix = new Matrix(this.cols, this.rows);
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
transposedMatrix.setElement(j, i, this.matrix[i][j]);
}
}
return transposedMatrix;
}
public Matrix add(Matrix matrix) {
Matrix result = new Matrix(this.rows, this.cols);
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
result.setElement(i, j, this.matrix[i][j] + matrix.getElement(i, j));
}
}
return result;
}
public Matrix multiply(Matrix matrix) {
Matrix result = new Matrix(this.rows, matrix.cols);
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < matrix.cols; j++) {
int sum = 0;
for (int k = 0; k < this.cols; k++) {
sum += this.matrix[i][k] * matrix.getElement(k, j);
}
result.setElement(i, j, sum);
}
}
return result;
}
public Matrix subtract(Matrix matrix) {
Matrix result = new Matrix(this.rows, this.cols);
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
result.setElement(i, j, this.getElement(i, j) - matrix.getElement(i, j));
}
}
return result;
}
}