-
Notifications
You must be signed in to change notification settings - Fork 0
/
atm.cpp
159 lines (150 loc) · 3.73 KB
/
atm.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "atm.h"
#include <string>
using namespace daniel;
Atm::Atm(){}
Atm::Atm(const char* name, double balance){
if (name != nullptr && name[0] != '\0' && strlen(name) > 0){
m_name = new char[strlen(name) + 1];
strcpy(m_name, name);
m_balance = balance;
}
}
Atm::Atm(const Atm& other){
*this = other;
}
Atm& Atm::operator=(const Atm& other){
if (this != &other){
if (m_name != nullptr){
delete[] m_name;
}
if (other.m_name != nullptr){
m_name = new char[strlen(other.m_name) + 1];
strcpy(m_name, other.m_name);
}
m_balance = other.m_balance;
}
return *this;
}
Atm::~Atm() {
delete[] m_name;
m_name = nullptr;
}
void Atm::run(){
std::cout << "Press enter to continue..." << std::endl;
std::cin.get();
int choice = menu();
while (choice != 6) {
if(choice == 1){
std::cout << "Current Balance: $" << getBalance() << std::endl;
}
else if(choice == 2){
double amount{0};
std::cout << "How much would you like to deposit: ";
std::cin >> amount;
deposit(amount);
}
else if(choice == 3){
double amount{0};
std::cout << "How much would you like to withdraw: ";
std::cin >> amount;
withdraw(amount);
}
else if(choice == 4){
std::string name{};
std::cout << "Enter the name of the person you would like to transfer to: ";
std::cin >> name;
Atm* recipent = new Atm(name.c_str());
transfer(*recipent);
}
else if(choice == 5){
std::string name;
std::cout << "Enter the name of the account holder: ";
getline(std::cin, name);
setName(name.c_str());
}
choice = menu();
}
std::cout << "Thank you for using the ATM!" << std::endl;
}
void Atm::deposit(double amount){
if (amount > 0){
m_balance += amount;
}
}
void Atm::withdraw(double amount){
if (amount > 0){
if(amount <= m_balance){
m_balance -= amount;
std::cout << "Withdrawal successful" << std::endl;
}
else {
std::cout << "you don't have enough money" << std::endl;
}
}
}
int Atm::menu()const {
int choice{0};
bool valid{false};
std::cout << "========================================" << std::endl;
std::cout << "Welcome to the ATM of " << m_name << std::endl;
std::cout << "========================================" << std::endl << std::endl;
std::cout << "1. Check balance" << std::endl;
std::cout << "2. Deposit" << std::endl;
std::cout << "3. Withdraw" << std::endl;
std::cout << "4. Transfer Fund" << std::endl;
std::cout << "5. Edit name" << std::endl;
std::cout << "6. Exit" << std::endl << std::endl;
std::cout << "Enter your choice: ";
std::cin >> choice;
while (!valid){
if (std::cin.fail()) {
std::cin.clear();
std::cin.ignore(1000, '\n');
std::cout << "Invalid input. Please enter a number between 1 and 6." << std::endl;
std::cout << "Enter your choice: ";
std::cin >> choice;
}
else if (choice < 1 || choice > 6){
std::cout << "Invalid input. Please enter a number between 1 and 6." << std::endl;
std::cout << "Enter your choice: ";
std::cin >> choice;
}
else{
valid = true;
}
}
return choice;
}
void Atm::setName(const char* name){
if (name != nullptr && name[0] != '\0' && strlen(name) > 0){
delete[] m_name;
m_name = new char[strlen(name) + 1];
strcpy(m_name, name);
}
}
void Atm::transfer(Atm& other){
double amount{0};
std::cout << "Enter the amount to transfer: ";
std::cin >> amount;
if (amount > 0){
if (amount <= m_balance){
m_balance -= amount;
other.m_balance += amount;
std::cout << "Transfer successful" << std::endl;
}
else {
std::cout << "you don't have enough money" << std::endl;
}
}
}
const char* Atm::getName()const{
return m_name;
}
double Atm::getBalance()const{
return m_balance;
}
void Atm::setBalance(double amount) {
m_balance = amount;
}