-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
69 lines (57 loc) · 1.38 KB
/
main.cpp
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
#include <exception>
#include <iostream>
class NegativeDeposit : public std::exception {
public:
const char *what() const noexcept override {
return "값이 0또는 음수가 될 수 없습니다.";
}
};
class InsufficientFunds : public std::exception {
public:
const char *what() const noexcept override {
return "자금이 불충분합니다.";
}
};
class Account {
private:
double balance;
public:
Account() {
balance = 0;
}
Account(double initialDeposit) {
balance = initialDeposit;
}
double getBalance() const {
return balance;
}
// returns new balance or -1 if error
double deposit(double amount) {
if (amount <= 0)
throw NegativeDeposit();
balance += amount;
return balance;
}
// returns new balance or -1 if invalid amount
double withdraw(double amount) {
if ((amount > balance) || (amount < 0))
throw InsufficientFunds();
balance -= amount;
return balance;
}
};
int main(){
auto a = Account{10};
try {
a.deposit(-10);
}
catch (NegativeDeposit& e) {
std::cout << "NegativeDeposit Exception: " << e.what() << '\n';
}
try {
a.withdraw(100);
}
catch (InsufficientFunds& e) {
std::cout << "InsufficientFunds Exception: " << e.what() << '\n';
}
}