-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccount.cpp
More file actions
33 lines (28 loc) · 907 Bytes
/
Account.cpp
File metadata and controls
33 lines (28 loc) · 907 Bytes
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
#include "Account.h"
#include <stdexcept>
// Initializes account with holder name and starting balance
Account::Account(const std::string& name, double balance)
: name(name), balance(balance) {}
// Adds funds after validating positive deposit amount
void Account::deposit(double amount) {
if (amount <= 0) {
throw std::invalid_argument("Deposit must be positive.");
}
balance += amount;
}
// Removes funds only if sufficient balance exists
void Account::withdraw(double amount) {
if (amount > balance) {
throw std::runtime_error("Insufficient balance.");
}
balance -= amount;
}
// Outputs account details to standard output
void Account::display() const {
std::cout << "Name: " << name
<< ", Balance: " << balance << '\n';
}
// Returns current account balance without modification
double Account::getBalance() const {
return balance;
}