Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
249 lines (217 loc) · 7.96 KB
/
Copy pathmain.cpp
File metadata and controls
249 lines (217 loc) · 7.96 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
// ============================================================
// main.cpp -- Freelance Marketplace System
// Entry point: main menu, registration, login
//
// Compile:
// g++ -std=c++11 -o marketplace main.cpp
//
// 100% C++11:
// string (not char[]) | nullptr (not NULL)
// stod / stoi | getline (not scanf)
// vector<> | auto keyword
// RAII file streams | range-based for loops
//
// Compatible: VS Code, Dev C++, Code::Blocks
// ============================================================
#include <iostream>
#include <string>
#include "utils.h"
#include "FileManager.h"
#include "User.h"
#include "Freelancer.h"
#include "Client.h"
#include "Admin.h"
#include "Exceptions.h"
using namespace std;
// Global FileManager used during registration and login
FileManager gFM;
// Forward declarations
void showMainMenu();
void handleRegister();
void handleLogin();
void registerAsFreelancer(const string& uid, const string& uname);
void registerAsClient(const string& uid, const string& uname);
// ============================================================
// ENTRY POINT
// ============================================================
int main() {
try {
showMainMenu();
}
catch (const FileException& e) {
cerr << "\n[FILE ERROR] " << e.what() << endl;
}
catch (const ValidationException& e) {
cerr << "\n[VALIDATION ERROR] " << e.what() << endl;
}
catch (const AuthenticationException& e) {
cerr << "\n[AUTH ERROR] " << e.what() << endl;
}
catch (const exception& e) {
cerr << "\n[UNEXPECTED ERROR] " << e.what() << endl;
}
return 0;
}
// ============================================================
// MAIN MENU
// ============================================================
void showMainMenu() {
while (true) {
clearScreen();
cout << "\n\n"
<< " +============================================+\n"
<< " | |\n"
<< " | FREELANCE MARKETPLACE SYSTEM |\n"
<< " | Console-Based Edition (C++) |\n"
<< " | |\n"
<< " +============================================+\n\n"
<< " [1] Register New Account\n"
<< " [2] Login\n"
<< " [3] Exit\n\n";
int choice = getChoice(1, 3);
switch (choice) {
case 1: handleRegister(); break;
case 2: handleLogin(); break;
case 3:
clearScreen();
cout << "\n Thank you for using Freelance Marketplace!\n\n";
return;
}
}
}
// ============================================================
// REGISTRATION
// ============================================================
void handleRegister() {
clearScreen();
printHeader("CREATE NEW ACCOUNT");
cout << " Select Account Type:\n\n"
<< " [1] Freelancer (I offer services)\n"
<< " [2] Client (I post projects)\n"
<< " [3] Back\n\n";
int typeChoice = getChoice(1, 3);
if (typeChoice == 3) return;
// ---- Common registration fields ----
cout << "\n Username: ";
string username;
getline(cin, username);
if(username.empty()) throw ValidationException("Username cannot be empty");
if (username.empty()) {
cout << " Username cannot be empty.\n";
pressEnterToContinue();
return;
}
if (gFM.usernameExists(username)) {
cout << " Username '" << username << "' is already taken.\n";
pressEnterToContinue();
return;
}
cout << " Password (min 6 chars): ";
string password;
getline(cin, password);
if (password.length() < 6) {
cout << " Password must be at least 6 characters.\n";
pressEnterToContinue();
return;
}
cout << " Email: ";
string email;
getline(cin, email);
if (email.empty() || email.find('@') == string::npos) {
cout << " Please enter a valid email address.\n";
pressEnterToContinue();
return;
}
string uid = generateID("U", FileManager::getUsersFile());
string type = (typeChoice == 1) ? "Freelancer" : "Client";
gFM.saveUser(uid, username, password, email, type);
if (typeChoice == 1)
registerAsFreelancer(uid, username);
else
registerAsClient(uid, username);
}
// Collect extra Freelancer info after basic account is created
void registerAsFreelancer(const string& uid, const string& uname) {
cout << "\n Your Skills (e.g. C++ Python Web): ";
string skills;
getline(cin, skills);
if (skills.empty()) skills = "General";
double rate = getDouble(" Hourly Rate (Rs.): ");
gFM.saveFreelancerProfile(uname, skills, rate);
cout << "\n ==========================================\n"
<< " Account created successfully!\n"
<< " User ID : " << uid << "\n"
<< " Username : " << uname << "\n"
<< " Type : Freelancer\n"
<< " Skills : " << skills << "\n"
<< " ==========================================\n";
pressEnterToContinue();
}
// Collect extra Client info after basic account is created
void registerAsClient(const string& uid, const string& uname) {
cout << "\n Company Name (press Enter to skip): ";
string company;
getline(cin, company);
cout << "\n ==========================================\n"
<< " Account created successfully!\n"
<< " User ID : " << uid << "\n"
<< " Username : " << uname << "\n"
<< " Type : Client\n";
if (!company.empty())
cout << " Company : " << company << "\n";
cout << " ==========================================\n";
pressEnterToContinue();
}
// ============================================================
// LOGIN -- demonstrates RUNTIME POLYMORPHISM
// ============================================================
void handleLogin() {
clearScreen();
printHeader("LOGIN");
cout << " Username: ";
string username;
getline(cin, username);
cout << " Password: ";
string password;
getline(cin, password);
// loginUser now returns userType and fills outUserID + outEmail
string outUserID, outEmail;
string userType = gFM.loginUser(username, password, outUserID, outEmail);
if (userType.empty()) {
cout << "\n Incorrect username or password. Please try again.\n";
pressEnterToContinue();
return;
}
cout << "\n Login successful! Welcome, " << username << "!\n";
// ================================================================
// POLYMORPHISM in action
//
// 'currentUser' is declared as User* (base-class pointer).
// But it actually POINTS TO a derived object:
// Freelancer OR Client OR Admin
//
// When displayDashboard() is called on it:
// - C++ looks up the virtual function table (vtable)
// - Calls the CORRECT overridden version at RUNTIME
//
// This means one line of code handles 3 different dashboards.
// Adding a new user type (e.g. Moderator) would need only:
// 1. A new class inheriting User
// 2. One more 'else if' below
// Nothing else changes.
//
// 'nullptr' is C++ (replaces C's NULL).
// ================================================================
User* currentUser = nullptr;
if (userType == "Freelancer")
currentUser = new Freelancer(outUserID, username, password, outEmail);
else if (userType == "Client")
currentUser = new Client(outUserID, username, password, outEmail, "");
else if (userType == "Admin")
currentUser = new Admin(outUserID, username, password, outEmail);
if (currentUser != nullptr) {
currentUser->displayDashboard(); // virtual dispatch
delete currentUser; // calls virtual destructor
currentUser = nullptr;
}
}