-
Notifications
You must be signed in to change notification settings - Fork 3
/
atmMachine.cpp
103 lines (90 loc) · 2.67 KB
/
atmMachine.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
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>
using namespace std;
// Class definition for the ATM machine
class ATM {
private:
// Private member to hold the user's balance
double balance;
public:
// Constructor initializes the balance with a default value of 1000
ATM(){
balance=(1000.0);
}
// Member function to display the current balance
void checkBalance()
{
cout << "Your current balance is: $" << balance << endl;
}
// Member function to handle depositing money
void deposit() {
double amount;
cout << "Enter the amount to deposit: $";
cin >> amount;
// Check if the deposit amount is valid
if (amount < 0)
{
cout << "Invalid amount." << endl;
return;
}
// Update the balance
balance += amount;
cout << "Successfully deposited $" << amount << "." << endl;
}
// Member function to handle withdrawing money
void withdraw()
{
double amount;
cout << "Enter the amount to withdraw: $";
cin >> amount;
// Check if the withdrawal amount is valid and if there are sufficient funds
if (amount < 0 || amount > balance)
{
cout << "Invalid amount or insufficient funds." << endl;
return;
}
// Update the balance
balance -= amount;
cout << "Successfully withdrew $" << amount << "." << endl;
}
};
// Main function
int main() {
// Create an instance of the ATM class
ATM atm;
int choice;
// Main loop for the ATM menu
do {
// Display the menu to the user
cout << "\n------ ATM Menu ------" << endl;
cout << "1. Check Balance" << endl;
cout << "2. Deposit" << endl;
cout << "3. Withdraw" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
// Use switch statement to handle user's choice
switch (choice) {
case 1:
// Call the function to check balance
atm.checkBalance();
break;
case 2:
// Call the function to deposit money
atm.deposit();
break;
case 3:
// Call the function to withdraw money
atm.withdraw();
break;
case 4:
// Exit the program
cout << "Thank you for using our ATM. Goodbye!" << endl;
break;
default:
// Handle invalid choices
cout << "Invalid choice. Please choose a number between 1 and 4." << endl;
break;
}
} while (choice != 4); // Loop until user chooses to exit
return 0;
}