-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleHelper.java
More file actions
81 lines (67 loc) · 3.14 KB
/
ConsoleHelper.java
File metadata and controls
81 lines (67 loc) · 3.14 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
package com.javarush.task.task26.task2613;
import com.javarush.task.task26.task2613.exception.InterruptOperationException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ConsoleHelper {
private static BufferedReader bis = new BufferedReader(new InputStreamReader(System.in));
public static void writeMessage(String message) {
System.out.println(message);
}
public static String readString() throws InterruptOperationException {
try {
String text = bis.readLine();
if(text.toUpperCase().equals("EXIT")){
throw new InterruptOperationException();
}
return text;
} catch (IOException ignored) {
/* NOP */
}
return null;
}
public static Operation askOperation() throws InterruptOperationException{//просим пользователя ввести номер операции, если ввод не корректный, обрабатываем исключение
// исключение и просим повторить ввод
while (true){
try{
ConsoleHelper.writeMessage("введите номер операции 1- INFO, 2 - DEPOSIT, 3 - WITHDRAW, 4 - EXIT");
String operationNumber = ConsoleHelper.readString();
return Operation.getAllowableOperationByOrdinal(Integer.parseInt(operationNumber.trim()));
}catch (IllegalArgumentException e){
ConsoleHelper.writeMessage("неверный номер операции");
}
}
}
public static String askCurrencyCode() throws InterruptOperationException{
while (true) {
ConsoleHelper.writeMessage("Please choose a currency code, for example USD");
String currencyCode = ConsoleHelper.readString();
if (currencyCode == null || currencyCode.trim().length() != 3) {
ConsoleHelper.writeMessage("Please specify valid data.");
continue;
}
return currencyCode.trim().toUpperCase();
}
}
public static String[] getValidTwoDigits(String currencyCode) throws InterruptOperationException{
while (true) {
ConsoleHelper.writeMessage(String.format("Please specify integer denomination and integer count." +
" For example '10 3' means 30 %s", currencyCode));
String s = ConsoleHelper.readString();
String[] split = null;
if (s == null || (split = s.split(" ")).length != 2) {
ConsoleHelper.writeMessage("Please specify valid data.");
} else {
try {
if (Integer.parseInt(split[0]) <= 0 || Integer.parseInt(split[1]) <= 0)
{
ConsoleHelper.writeMessage("Please specify valid data.");
}else {return split;}
} catch (NumberFormatException e) {
ConsoleHelper.writeMessage("Please specify valid data.");
continue;
}
}
}
}
}