-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWithdrawCommand.java
More file actions
54 lines (45 loc) · 2.52 KB
/
WithdrawCommand.java
File metadata and controls
54 lines (45 loc) · 2.52 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
package com.javarush.task.task26.task2613.command;
import com.javarush.task.task26.task2613.ConsoleHelper;
import com.javarush.task.task26.task2613.CurrencyManipulator;
import com.javarush.task.task26.task2613.CurrencyManipulatorFactory;
import com.javarush.task.task26.task2613.exception.InterruptOperationException;
import com.javarush.task.task26.task2613.exception.NotEnoughMoneyException;
import java.util.ConcurrentModificationException;
import java.util.Map;
class WithdrawCommand implements Command {
@Override
public void execute() throws InterruptOperationException {
String curencyCod = ConsoleHelper.askCurrencyCode();
while (!CurrencyManipulatorFactory.isCurrencyCodeExist(curencyCod)){
curencyCod=ConsoleHelper.askCurrencyCode();
}
CurrencyManipulator manipulator = CurrencyManipulatorFactory.getManipulatorByCurrencyCode(curencyCod);
while (true) {
ConsoleHelper.writeMessage("Введите сумму которую хотите снять");
try {
int summ = Integer.parseInt(ConsoleHelper.readString());
if (summ == 0) {
ConsoleHelper.writeMessage("Неверный ввод, введите сумму которую хотите снять ");
continue;
}
if (manipulator.isAmountAvailable(summ)) {
Map<Integer, Integer> map = manipulator.withdrawAmount(summ);
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
ConsoleHelper.writeMessage("\t" + entry.getKey() + " - " + entry.getValue());
}
ConsoleHelper.writeMessage("Транзакция прошла успешно");
return;
} else {
ConsoleHelper.writeMessage("на ашем счету недостаточно денег");
throw new NotEnoughMoneyException();
}
} catch (IllegalArgumentException e) {
ConsoleHelper.writeMessage("Неверный ввод, введите сумму которую хотите снять ");
} catch (NotEnoughMoneyException e) {
ConsoleHelper.writeMessage("Невозможно выдать запрашиваемую сумму");
} catch (ConcurrentModificationException e){
e.printStackTrace();
}
}
}
}