-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.cpp
204 lines (161 loc) · 5.66 KB
/
interface.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Si klase skirta saveikauti su vartotoju per terminalo sąsają.
* Veikia kaip gidas kuris nusako, kas kaip veiks programuoje.
*/
#include <iostream>
#include "interface_.h"
#include "login_.h"
#include "obtainUser_.h"
#include "bankAccount_.h"
#include "transactions_.h"
#include "user_.h"
#include "reader_from_csv_.h"
#include "writer_to_csv_.h"
using namespace std;
/*
* Class responsible for managing the user interface and coordinating interactions between different classes.
*/
Interface::Interface() {}
/*
* Method for displaying the initial interface screen.
* Prints a welcome message centered on the screen.
*/
void Interface::interfaceLoad() {
int terminalWidth = 80;
int terminalHeight = 5;
// Positioning calculation
int messageWidth = 27; // Length of the message
int padding = (terminalWidth - messageWidth) / 2;
// Printing empty lines
for (int i = 0; i < (terminalHeight / 2) - 1; i++) {
cout << endl;
}
// Positioning before the text
for (int i = 0; i < padding; i++) {
cout << " ";
}
cout << "Hello, welcome to your terminal" << endl;
}
/*
* Method for guiding the user through the interface.
* Prompts the user to choose an action: login, create an account, or exit the program.
* Based on the choice, it calls the respective interface method.
* Parameters:
* - users: Reference to the vector of User objects.
* - accounts: Reference to the vector of BankAccount objects.
*/
void Interface::interfaceGuide(vector<User>&users, vector<BankAccount>&accounts) {
int choice;
cout << endl << "Do you have an accout?" << endl;
cout << endl << "1. Login in" << endl;
cout << endl << "2. Create account" << endl;
cout << endl << "3. Exit program" << endl << endl;
cin >> choice;
// Clearing the terminal
cout << "\033[2J\033[H";
if (choice == 1) {
// Create a Login object and initiate the login process
Interface::interfaceLogin(users, accounts);
}
else if (choice == 2) {
// Create a new user account
Interface::interfaceCreateUser(users, accounts);
}
else if (choice == 3) {
// Exit the program
}
}
/*
* Method for handling the user login process.
* Creates a Login object, prompts the user for login information, and checks if the login matches any user.
* If the login is successful, calls the interfaceTransfer method to perform account transactions.
*/
void Interface::interfaceLogin(vector<User>&users, vector<BankAccount>&accounts) {
Login login;
login.ioCreateLogin();
int i = login.isItMatches(users);
if (i >= 0) {
i--;
if (i < accounts.size()) {
cout << "Login succesful. " << i;
Interface::interfaceTransfer(accounts[i], accounts);
}
else {
cout << "Error: Invalid index for accounts vector. ";
}
}
else {
cout << "Login failed, there is no such user.";
}
}
/*
* Method for handling the account transactions interface.
* Allows the user to perform various transactions: deposit, withdrawal, transfer, or view balance.
* Continuously prompts the user for a choice until they choose to exit.
* Parameters:
* - account: Reference to the BankAccount object representing the logged-in account.
* - accounts: Reference to the vector of BankAccount objects.
*/
void Interface::interfaceTransfer(BankAccount account, vector<BankAccount>& accounts) {
Transactions transfer(account);
account.disp();
int recipientID;
int choice = 0;
while (choice != 5) {
cout << "\033[2J\033[H";
cout << " Your balance is: " << account.getBalance();
transfer.printOptions();
cin >> choice;
if (choice == 1) {
transfer.deposit();
cout << transfer.getBalance();
}
else if (choice == 2) {
transfer.withdrawal();
cout << transfer.getBalance();
}
else if (choice == 3) {
transfer.transferTo(account, accounts);
cout << transfer.getBalance();
}
else if (choice == 4) {
cout << account.getBalance();
}
}
}
/*
* Method for handling the user account creation process.
* Creates an ObtainingUser object, prompts the user for account information, creates a new User object,
* and adds it to the users vector. Also creates a corresponding BankAccount object and adds it to the accounts vector.
* Writes the account information to a CSV file using the Writer class.
* After successful account creation, calls the interfaceLogin method to proceed with login.
* Parameters:
* - users: Reference to the vector of User objects.
* - accounts: Reference to the vector of BankAccount objects.
*/
void Interface::interfaceCreateUser(vector<User>& users, vector<BankAccount>& accounts) {
ObtainingUser obtainingUser;
obtainingUser.ioCreateUser();
User a = obtainingUser.createUserObject();
cout << "User created " << endl;
BankAccount c(a);
const User* retrieveUser = c.getUser();
cout << "User name :" << retrieveUser->getName() << endl;
cout << "User id: " << retrieveUser->getId() << endl;
users.push_back(a);
accounts.push_back(c);
Writer write("Data_.csv");
write.writeFile(accounts);
Interface::interfaceLogin(users, accounts);
}
/*
* Method for reading user information from a CSV file.
* Creates a Reader object, opens the file, reads the lines, closes the file, and returns the vector of User objects.
*/
vector<User>Interface::readUsers() {
Reader read("data.csv");
read.openReadFile();
vector<User>list = read.readLines();
read.closeReadFile();
return list;
}