-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatm.cpp
181 lines (145 loc) · 6.53 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <limits>
#include <chrono>
#include <ctime>
#include <algorithm>
class Transaction {
public:
std::string date;
std::string type;
double amount;
Transaction(const std::string& date,const std::string& type, double transAmount)
: date(date), type(type), amount(transAmount) {} // Corrected constructor
};
class Account {
public:
int account_no;
std::string password;
double balance;
std::vector<Transaction> transactions;
static int next_account_no;
static std::string default_password;
Account(double initial_balance)
: account_no(next_account_no++), password(default_password), balance(initial_balance) {}
};
//Initialize account num and password
int Account::next_account_no = 1;
std::string Account::default_password = "123";
//clean the screen
void clearScreen() {
// Clear the screen,linux use "clear"
//system("clear") for linux
system("cls");
}
//Get current time
std::string ctime() {
std::time_t currentTime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::string time = std::ctime(¤tTime);
// Remove the trailing newline character
time.erase(std::remove(time.begin(), time.end(), '\n'), time.end());
return time;
}
int main(){
std::vector<Account> mainlist;
std::vector<Transaction> transHistory;
int account_no;
std::string password;
int option;
//create 10 accounts.
for(int i=0;i<10;i++){
mainlist.push_back(Account(5000));
}
//first page
std::cout<<"Login Window:\n"<<"=============\n"<<"Enter your Account no:\n"<<std::endl;
std::cin >> account_no;
std::cout<<"Enter your password\n"<<std::endl;
std::cin >> password;
//verify account number and password.
while(1>account_no || account_no<10 && password!="123"){
clearScreen();
std::cout<<"Login Window:\n"<<"=============\n"<<"Enter your Account no:\n"<<std::endl;
std::cin >> account_no;
std::cout<<"Enter your password\n"<<std::endl;
std::cin >> password;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
//second page, let user choose options
while(true){
clearScreen();
std::cout<<"Main Window\n"<<"=============\n"<<"Choose one of the following options:\n"<<
"(1)Show balance\n"<<"(2)Show Deposit\n"<<"(3)Withdrwaw\n"<<"(4)Show All Transactions\n"
<<"Enter your choice:\n"<<std::endl;
std::cin>> option;
//Show balance
if(option == 1){
clearScreen();
std::cout << "Main Window-->Show Balance\n"<<"==============================================="<<std::endl;
std::cout << "Your current Balance is: " << mainlist[account_no - 1].balance << "\n";
std::cout << "-----------------------------------------------";
std::cout << "Press Enter to continue...";
std::cin.get();
}
//Deposit
if(option == 2){
double deposit;
clearScreen();
std::cout << "Main Window-->Deposit(Enter the following information)\n"<< "===============================================\n"<<
"The amount you want to deposit: "<<std::endl;
std::cin >> deposit;
std::string currentTime = ctime();
std::cout << "-----------------------------------------------\n";
//Deposit must >0
if(deposit>0){
transHistory.push_back(Transaction(currentTime," deposit ", deposit));
mainlist[account_no-1].balance = mainlist[account_no-1].balance + deposit;
} else{
std::cout << "Invalid input, please try again." << std::endl;
}
std::cout << "Well Done. This was added to your balance successfully...Press Enter to go back to the Main Window";
std::cin.get();
}
//Withdraw
if(option == 3){
double withdrawNum;
clearScreen();
std::cout << "Main Window-->Wtihdraw(Enter the following information)\n"<< "===============================================\n"<<
"The amount you want to withdraw: "<<std::endl;
std::cin >> withdrawNum;
std::string currentTime = ctime();
std::cout << "-----------------------------------------------\n";
//Withdraw amount cannot exceed current balance and bigger than 0
if(withdrawNum>0 && withdrawNum <= mainlist[account_no-1].balance){
transHistory.push_back(Transaction(currentTime," withdraw ", withdrawNum));
mainlist[account_no-1].balance = mainlist[account_no-1].balance - withdrawNum;
std::cout << "Please take your money then...Press Enter to go back to the Main Window";
} else{
std::cout << "Invalid input, please try again." << std::endl;
std::cout << "Press Enter to go back to the Main Window";
}
std::cin.get();
}
//Show transaction
if(option == 4){
clearScreen();
std::cout << "Main Window-->Show All Transactions\n"<< "===============================================\n"
<< "Account no: "<< account_no << "\n" <<
"-----------------------------------------------\n"<< std::endl;
for(Transaction& trans : transHistory) { // Loop through each transaction
std::cout << "Date: " << trans.date << " |Type: " << trans.type << " |Amount: " << trans.amount << std::endl;
std::cout << "-----------------------------------------------\n";
}
std::cout << "Press Enter to go back to the Main Window";
std::cin.get();
}
//if user inputs string or other numbers, clear input and give them new chance.
if(std::cin.fail())
{
std::cin.clear();
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
return 0;
}