-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
52 lines (52 loc) · 1.81 KB
/
Main.java
File metadata and controls
52 lines (52 loc) · 1.81 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
import java.util.regex.*;
public class Main {
public static void main(String[] args) {
money("$45.55"); // вводим число
}
public static boolean money(String money){
try{
Pattern P = Pattern.compile("\\$|€");
Matcher M = P.matcher(money);
boolean b = false;
double dol = 31.7326;
double eur = 42.9501;
if(check(money)){// проверяем на правильно ли ввели
if(M.find()){
char c = M.group().charAt(0);
money = M.replaceAll("");
money = money.replaceAll(",", ".");
double res;
if (c == '$'){
res = Double.parseDouble(money) * dol;
}else{
res = Double.parseDouble(money) * eur;
}
System.out.printf("Р" + "%8.2f", res);
b = true;
}else{
System.out.println(0);
}
}
return b;
}catch(PatternSyntaxException e){
System.out.println("Wrong regexp pattern");
return false;
}
}
public static boolean check(String money){// проверяем на правильно ли ввели
try{
Pattern P = Pattern.compile("(\\$|€)(\\d*|(\\d*(,|\\.)\\d\\d?))");
Matcher M = P.matcher(money);
boolean b = false;
if(M.matches()){
b = true;
}else{
System.out.println(0);
}
return b;
}catch(PatternSyntaxException e){
System.out.println("Wrong regexp pattern");
return false;
}
}
}