-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsavings.cpp
More file actions
53 lines (44 loc) · 1.9 KB
/
savings.cpp
File metadata and controls
53 lines (44 loc) · 1.9 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
// SavingsAccount.cpp
#include "Savings.h"
#include <stdexcept> // To handle errors
// Method to withdraw money from the account
// Withdrawing from a savings account incurs a $50 fee
void Savings::withdraw(double amount) {
if (balance < amount - 50) {
throw runtime_error("Not enough money to withdraw!"); // Handle error if balance is too low
}
balance -= amount; // Deduct the amount from the balance
}
// Method to transfer money to another account
void Savings::transfer(Account& toAccount, double amount) {
if (balance < amount) {
throw runtime_error("Not enough money to transfer!"); // Handle error if balance is too low
}
balance -= amount; // Deduct the amount from this account
toAccount.deposit(amount); // Add the amount to the other account
}
// Method to print the account details (ID and balance)
void Savings::printAccount() const {
cout << "Account ID: " << accountId << " | Balance: $" << balance << endl;
}
// Constructor for SavingsAccount class (inherits from Account)
Savings::Savings(long account_number, double balaance) : Account(account_number, balance) {}
// Overridden withdraw method for SavingsAccount
void Savings::withdraw(double amount) {
if (balance < amount) {
throw runtime_error("Not enough money to withdraw!"); // Handle error if balance is too low
}
// If balance after withdrawal is below $1,000, apply a $50 penalty
if (balance - amount < 1000) {
if (balance - amount < 50) {
throw runtime_error("Not enough money to cover the $50 penalty!");
}
balance -= 50; // Apply $50 penalty
}
balance -= amount; // Deduct the amount from the balance
}
// Method to print the savings account details
void SavingsAccount::printAccount() const {
Account::printAccount(); // Print the base account details
cout << "Interest Rate: 1.05% APY" << endl; // Print the interest rate
}