-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaixaDialogoTipoConversor.java
More file actions
122 lines (95 loc) · 3.21 KB
/
CaixaDialogoTipoConversor.java
File metadata and controls
122 lines (95 loc) · 3.21 KB
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
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class CaixaDialogoTipoConversor {
public static void main(String[] args) {
conversorMoedas();
desejaContinuar();
}
private static void desejaContinuar() {
int resposta = JOptionPane.showConfirmDialog(null, "Deseja continuar?", "information", JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE);
if(resposta == 0) {
conversorMoedas();
desejaContinuar();
}if(resposta == 1) {
JOptionPane.showMessageDialog(null, "Programa finalizado.");
} if(resposta == 2) {
JOptionPane.showMessageDialog(null, "Programa concluído.");
}
}
public static void converteRealDolar(double valor) {
double valorConvertido = valor / 5.25;
String cifra = "$ ";
mostraMensagem(valorConvertido, cifra);
}
public static void converteDolarReal(double valor) {
double valorConvertido = valor * 5.25;
String cifra = "R$";
mostraMensagem(valorConvertido, cifra);
}
public static void converteRealEuro(double valor) {
double valorConvertido = valor / 5.45;
String cifra = "¢ ";
mostraMensagem(valorConvertido, cifra);
}
public static void converteEuroReal(double valor) {
double valorConvertido = valor * 5.45;
String cifra = "R$ ";
mostraMensagem(valorConvertido, cifra);
}
public static void converteRealLibra(double valor) {
double valorConvertido = valor / 6.32;
String cifra = "£ ";
mostraMensagem(valorConvertido, cifra);
}
public static void converteLibraReal(double valor) {
double valorConvertido = valor * 6.32;
String cifra = "R$";
mostraMensagem(valorConvertido, cifra);
}
public static void mostraMensagem(double valorConvertido, String cifra) {
String val = new DecimalFormat("#,##0.00").format(valorConvertido);
String msg = cifra;
JOptionPane.showMessageDialog(null, "O valor convertido é "+ msg + val);
}
public static boolean validaInput(String input) {
if (input.matches("[+-]?\\d*(\\.\\d+)?")&& !(input == null))
return true;
return false;
}
public static void conversorMoedas() {
Object moeda = JOptionPane.showInputDialog(null,
"Escolha a moeda para a qual você deseja converter seu dinheiro", "Moedas",
JOptionPane.QUESTION_MESSAGE, null, new Object[] { "De Reais a Dólares", "De Reais a Euros",
"De Reais a Libras", "De Dólares a Reais", "De Euros a Reais", "De Libras a Reais" },
"Escolha");
System.out.println(moeda);
String input = JOptionPane.showInputDialog("Insira um valor");
boolean inputValido = validaInput(input);
if (!inputValido) {
input = JOptionPane.showInputDialog("Por favor, digite um valor válido!");
}
Double valor = Double.parseDouble(input);
System.out.println(moeda);
if (moeda.equals("De Reais a Dólares")) {
converteRealDolar(valor);
}
if (moeda.equals("De Dólares a Reais")) {
converteDolarReal(valor);
}
if (moeda.equals("De Reais a Euros")) {
converteRealEuro(valor);
}
if (moeda.equals("De Euros a Reais")) {
converteEuroReal(valor);
}
if (moeda.equals("De Reais a Libras")) {
converteRealLibra(valor);
}
if (moeda.equals("De Libras a Reais")) {
converteLibraReal(valor);
}if(moeda.equals(null)) {
JOptionPane.showMessageDialog(null, "Programa finalizado.");
}
}
}