-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03.C++
103 lines (88 loc) · 3.15 KB
/
03.C++
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream> // Include the standard input-output library
using namespace std; // Use the standard namespace to avoid prefixing std::
// Define a class named Account
class Account
{
private:
string id; // Holds the account ID
string name; // Holds the account holder's name
int balance = 0; // Holds the account balance, initialized to zero
public:
// Constructor that initializes an account with an ID and name, balance defaults to 0
Account(string i, string n)
{
id = i;
name = n;
}
// Overloaded constructor that initializes an account with an ID, name, and balance
Account(string i, string n, int b)
{
id = i;
name = n;
balance = b;
}
// Getter method to retrieve the account ID
string getID()
{
return id;
}
// Getter method to retrieve the account holder's name
string getName()
{
return name;
}
// Getter method to retrieve the current balance
int getBalance()
{
return balance;
}
// Method to add a specified amount to the balance (credit the account)
int credit(int amount)
{
balance += amount; // Increase the balance by the credited amount
return balance; // Return the updated balance
}
// Method to withdraw a specified amount from the balance (debit the account)
int debit(int amount)
{
if (amount <= balance) // Check if there are sufficient funds
{
balance -= amount; // Deduct the amount from the balance
}
else
{
cout << "Amount exceeded balance" << endl; // Print an error message if insufficient funds
}
return balance; // Return the updated balance
}
// Method to transfer a specified amount to another account
int transferTo(Account &account, int amount)
{
if (amount <= balance) // Check if sufficient balance is available
{
balance -= amount; // Deduct amount from the sender's account
account.credit(amount); // Add amount to the recipient's account
}
else
{
cout << "Amount exceeded balance" << endl; // Print error if insufficient balance
}
return balance; // Return the updated balance
}
};
int main() {
// Create an Account object for Mahmoud with an initial balance of 12,200
Account No1("001", "Mahmoud", 12200);
// Create another Account object for Mohamed with an initial balance of 10,000
Account No2("002", "Mohamed", 10000);
// Add 5623 to Mahmoud's balance
No1.credit(5623);
cout << "Balance after credit: " << No1.getBalance() << endl; // Display updated balance
// Withdraw 9000 from Mahmoud's balance
No1.debit(9000);
cout << "Balance after debit: " << No1.getBalance() << endl; // Display updated balance
// Transfer 5000 from Mahmoud's account to Mohamed's account
No1.transferTo(No2, 5000);
cout << "Balance after transfer: " << No1.getBalance() << endl; // Display Mahmoud's balance after transfer
return 0; // Indicate successful program termination
}