-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATMmachine.cpp
More file actions
263 lines (224 loc) · 8.22 KB
/
ATMmachine.cpp
File metadata and controls
263 lines (224 loc) · 8.22 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <bits/stdc++.h>
using namespace std;
enum class CashType{BILL_2000 = 2000,BILL_500 = 500, BILL_200 = 200, BILL_100 = 100};
enum class TransactionType{WITHDRAWAL, DEPOSIT, BALANCE_INQUIRY};
class Account{
string name;
double balance;
string accountNumber;
public:
Account(string n, string accNum,int bal): name(n), balance(bal), accountNumber(accNum) {}
string getAccountNumber(){ return accountNumber; }
string getName(){ return name; }
double getBalance(){ return balance; }
void deposit(double amount){ balance += amount; }
bool withdraw(double amount){
if(amount > balance) return false;
balance -= amount;
return true;
}
};
class Card{
Account &account;
string cardNumber;
int PIN;
public:
Card(Account &acc,string cnum,int pin): account(acc), cardNumber(cnum), PIN(pin) {}
string getCardNumber(){ return cardNumber; }
Account& getAccount(){ return account; }
bool verifyPIN(int pin){ return PIN == pin; }
};
class ATMinventory{
map<CashType,int> cashInventory; // denomination -> count
public:
ATMinventory(){
initializeCash();
}
void initializeCash(){
cashInventory[CashType::BILL_2000] = 10;
cashInventory[CashType::BILL_500] = 20;
cashInventory[CashType::BILL_200] = 30;
cashInventory[CashType::BILL_100] = 50;
}
int getTotalCash(){
int total = 0;
for(auto &p : cashInventory){
CashType bill = p.first;
int count = p.second;
total += static_cast<int>(bill) * count;
}
return total;
}
bool hasSufficientCash(int amount){
return getTotalCash() >= amount;
}
map<CashType,int> dispenseCash(int amount){
if(!hasSufficientCash(amount)) return {};
map<CashType,int> toDispense;
int remaining = amount;
vector<CashType> denominations = {CashType::BILL_2000, CashType::BILL_500, CashType::BILL_200, CashType::BILL_100};
for(CashType denom : denominations){
int billValue = static_cast<int>(denom);
int available = cashInventory[denom];
int needed = remaining / billValue;
int toGive = min(available, needed);
if(toGive > 0){
toDispense[denom] = toGive;
remaining -= toGive * billValue;
cashInventory[denom] -= toGive;
}
}
if(remaining == 0) return toDispense;
// If we couldn't dispense the exact amount, rollback
for(auto &[bill,count] : toDispense){
cashInventory[bill] += count;
}
return {};
}
void addCash(CashType type, int count){
cashInventory[type] += count;
}
};
// Simple transaction record
struct Transaction{
TransactionType type;
double amount;
bool success;
std::string note;
std::time_t timestamp;
};
// ATM class -- handles card interaction + transactions
class ATM{
ATMinventory inventory;
Card *insertedCard = nullptr;
bool authenticated = false;
vector<Transaction> history;
public:
ATM() = default;
bool insertCard(Card &card){
if(insertedCard) return false; // already a card
insertedCard = &card;
authenticated = false;
return true;
}
void ejectCard(){ insertedCard = nullptr; authenticated = false; }
bool enterPIN(int pin){
if(!insertedCard) return false;
authenticated = insertedCard->verifyPIN(pin);
return authenticated;
}
// Withdraw: checks account balance and ATM denominations
bool withdraw(int amount){
Transaction tx{TransactionType::WITHDRAWAL, (double)amount, false, "", std::time(nullptr)};
if(!insertedCard || !authenticated){ tx.note = "No card or not authenticated"; history.push_back(tx); return false; }
if(amount <= 0){ tx.note = "Invalid amount"; history.push_back(tx); return false; }
if(amount % 100 != 0){ tx.note = "Amount must be multiple of 100"; history.push_back(tx); return false; }
Account &acc = insertedCard->getAccount();
if(acc.getBalance() < amount){ tx.note = "Insufficient funds in account"; history.push_back(tx); return false; }
// Ask inventory to dispense first
auto dispensed = inventory.dispenseCash(amount);
if(dispensed.empty()){ tx.note = "ATM cannot dispense exact denominations or insufficient ATM cash"; history.push_back(tx); return false; }
// Try to withdraw from account. If it fails, roll back inventory.
if(!acc.withdraw(amount)){
// rollback
for(auto &p : dispensed) inventory.addCash(p.first, p.second);
tx.note = "Account withdraw failed"; history.push_back(tx); return false;
}
tx.success = true;
tx.note = "Dispensed cash";
history.push_back(tx);
printDispensed(dispensed);
return true;
}
// Deposit: accept a map of denominations -> counts
bool deposit(const map<CashType,int> &depositBills){
Transaction tx{TransactionType::DEPOSIT, 0.0, false, "", std::time(nullptr)};
if(!insertedCard || !authenticated){ tx.note = "No card or not authenticated"; history.push_back(tx); return false; }
int total = 0;
for(auto &p : depositBills){
total += static_cast<int>(p.first) * p.second;
}
if(total <= 0){ tx.note = "No cash provided"; history.push_back(tx); return false; }
// add to ATM inventory
for(auto &p : depositBills) inventory.addCash(p.first, p.second);
// credit account
insertedCard->getAccount().deposit(total);
tx.type = TransactionType::DEPOSIT;
tx.amount = total;
tx.success = true;
tx.note = "Deposit accepted";
history.push_back(tx);
return true;
}
double balanceInquiry(){
Transaction tx{TransactionType::BALANCE_INQUIRY, 0.0, false, "", std::time(nullptr)};
if(!insertedCard || !authenticated){ tx.note = "No card or not authenticated"; history.push_back(tx); return -1; }
double bal = insertedCard->getAccount().getBalance();
tx.type = TransactionType::BALANCE_INQUIRY;
tx.amount = bal;
tx.success = true;
tx.note = "Balance returned";
history.push_back(tx);
return bal;
}
const vector<Transaction>& getHistory() const{ return history; }
void printDispensed(const map<CashType,int> &d){
std::cout<<"Dispensed:\n";
for(auto &p : d){
std::cout<<static_cast<int>(p.first)<<" x "<<p.second<<"\n";
}
}
void printATMStatus(){
std::cout<<"ATM total cash: "<<inventory.getTotalCash()<<"\n";
}
};
int main(){
cout << "Welcome to ATM" << endl;
Account acc("Nikhil", "12345", 25000);
Card card(acc, "98765", 4321);
ATM atm;
// Insert card
if (!atm.insertCard(card)) {
cout << "Failed to insert card." << endl;
return 1;
}
// Enter PIN
if (!atm.enterPIN(4321)) {
cout << "PIN incorrect." << endl;
return 1;
}
// Deposit cash
map<CashType, int> depositBills = {
{CashType::BILL_2000, 2}, // 4000
{CashType::BILL_500, 4} // 2000
};
if (atm.deposit(depositBills)) {
cout << "Deposit successful." << endl;
} else {
cout << "Deposit failed." << endl;
}
// Withdraw cash
int withdrawAmount = 5000;
if (atm.withdraw(withdrawAmount)) {
cout << "Withdraw successful." << endl;
} else {
cout << "Withdraw failed." << endl;
}
// Balance inquiry
double bal = atm.balanceInquiry();
// Print ATM status
atm.printATMStatus();
// Print transaction history
cout << "\nTransaction History:" << endl;
for (const auto& tx : atm.getHistory()) {
cout << "Type: ";
switch (tx.type) {
case TransactionType::WITHDRAWAL: cout << "Withdrawal"; break;
case TransactionType::DEPOSIT: cout << "Deposit"; break;
case TransactionType::BALANCE_INQUIRY: cout << "Balance Inquiry"; break;
}
cout << ", Amount: " << tx.amount << ", Success: " << (tx.success ? "Yes" : "No") << ", Note: " << tx.note << endl;
}
atm.ejectCard();
return 0;
}