-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject.cpp
More file actions
371 lines (318 loc) · 7.52 KB
/
Copy pathProject.cpp
File metadata and controls
371 lines (318 loc) · 7.52 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include <bits/stdc++.h>
using namespace std;
void toLowerCase(string &str)
{
transform(str.begin(), str.end(), str.begin(), ::tolower);
}
void toUpperCase(string &str)
{
transform(str.begin(), str.end(), str.begin(), ::toupper);
}
void write(const string &strrr)
{
ofstream outputfile;
string fileName = "fileName.txt";
outputfile.open(fileName, ::ios::app);
if (!outputfile.is_open())
{
cout << "Failed to open " << fileName << "." << endl;
return;
}
outputfile << strrr << endl;
outputfile.close();
cout << "User data saved successfully." << endl;
}
vector<string> vec;
template <typename T>
void user_in(const T &inputt)
{
stringstream ss;
ss << inputt;
vec.push_back(ss.str());
}
void finalise_user_in()
{
stringstream vecContent;
for (const auto &s : vec)
{
vecContent << s;
}
write(vecContent.str());
vec.clear();
}
bool isValidName(const string &name)
{
if (name.empty()) return false;
for (char c : name)
if (!isalpha(c)) return false;
return true;
}
bool isValidPassword(const string &password)
{
return password.length() >= 6;
}
bool isValidFilmIndustry(const string &industry)
{
string upper = industry;
toUpperCase(upper);
return (upper == "HOLLYWOOD" || upper == "BOLLYWOOD");
}
int login_cred_new()
{
string name;
string password;
cout << "Hey there! Welcome!" << endl;
// Validate name
while (true)
{
cout << "Enter name (letters only): ";
cin >> name;
if (isValidName(name))
break;
cout << "Invalid name. Please use letters only (no spaces or numbers)." << endl;
}
toUpperCase(name);
user_in(name);
// Validate password
while (true)
{
cout << "Enter Password (min 6 characters): ";
cin >> password;
if (isValidPassword(password))
break;
cout << "Password too short. Must be at least 6 characters." << endl;
}
user_in(password);
finalise_user_in();
return 0;
}
int searchLineByData(const string &fileName, const string &searchString, const string &name)
{
ifstream file(fileName);
string line;
int lineNumber = 0;
bool found = false;
if (!file.is_open())
{
cout << "User doesn't exist. Would you like to register? (Y/N): ";
char ch;
// Validate Y/N input
while (true)
{
cin >> ch;
ch = toupper(ch);
if (ch == 'Y' || ch == 'N') break;
cout << "Invalid input. Please enter Y or N: ";
}
if (ch == 'Y')
{
login_cred_new();
}
return -1;
}
while (getline(file, line))
{
lineNumber++;
if (line.find(searchString) != string::npos)
{
if (!found)
{
found = true;
cout << "Welcome Again, " << name << "!" << endl;
}
else
{
cout << line << endl;
}
}
}
file.close();
if (!found)
{
cout << "Data not found in the file." << endl;
return -1;
}
return lineNumber;
}
int login_cred_existing()
{
string name;
string password;
// Validate name
while (true)
{
cout << "Enter name (letters only): ";
cin >> name;
if (isValidName(name))
break;
cout << "Invalid name. Please use letters only (no spaces or numbers)." << endl;
}
// Validate password
while (true)
{
cout << "Enter Password (min 6 characters): ";
cin >> password;
if (isValidPassword(password))
break;
cout << "Password too short. Must be at least 6 characters." << endl;
}
toUpperCase(name);
string fileName = "fileName.txt";
searchLineByData(fileName, name + password, name);
return 0;
}
class User
{
public:
virtual void login() = 0;
virtual void input() = 0;
};
int open(int choice, string filmindustry)
{
ifstream inputfile;
ifstream inputfile2;
string filename;
switch (choice)
{
case 1:
filename = "GENRE";
break;
case 2:
filename = "ACTOR";
break;
case 3:
filename = "DIRECTOR";
break;
case 4:
filename = "RELEASEYEAR";
break;
default:
cout << "Invalid option." << endl;
return 1;
}
string filename1 = filename + filmindustry + ".txt";
inputfile.open(filename1);
if (!inputfile.is_open())
{
cout << "Failed to open " << filename << endl;
return 1;
}
cout << "Available options are:" << endl;
string ch;
while (getline(inputfile, ch))
{
cout << ch << endl;
}
inputfile.close();
string choiceStr;
// Validate that the user entered something non-empty
while (true)
{
cout << "Enter your choice: ";
cin >> choiceStr;
if (!choiceStr.empty()) break;
cout << "Input cannot be empty. Please try again." << endl;
}
user_in(choiceStr);
finalise_user_in();
toUpperCase(choiceStr);
string movieFilename = choiceStr + filmindustry + ".txt";
inputfile2.open(movieFilename);
if (!inputfile2.is_open())
{
cout << "Failed to open movie file: " << movieFilename << endl;
return 1;
}
string movie;
cout << "Movies for the selected option:" << endl;
while (getline(inputfile2, movie))
{
cout << movie << endl;
}
inputfile2.close();
return 0;
}
void getUserInput()
{
string filmindustry;
// Validate film industry input
while (true)
{
cout << "Choose the film industry you want movies of (Hollywood / Bollywood): ";
cin >> filmindustry;
if (isValidFilmIndustry(filmindustry))
break;
cout << "Invalid choice. Please enter 'Hollywood' or 'Bollywood'." << endl;
}
user_in(filmindustry);
toUpperCase(filmindustry);
int a;
// Validate preference menu choice (1-4)
while (true)
{
cout << "Enter Preference Number from below list:" << endl;
cout << "1. Genre\n2. Actor\n3. Director\n4. Releaseyear" << endl;
if (cin >> a && a >= 1 && a <= 4)
break;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid choice. Please enter a number between 1 and 4." << endl;
}
user_in(a);
open(a, filmindustry);
}
class ExistingUser : public User
{
public:
void login() override
{
login_cred_existing();
}
void input() override
{
getUserInput();
}
};
class NewUser : public User
{
public:
void login() override
{
login_cred_new();
}
void input() override
{
getUserInput();
}
};
int main()
{
int ch;
// Validate main menu choice (1-2)
while (true)
{
cout << "Are you a:" << endl;
cout << "1. Existing User?" << endl;
cout << "2. New User?" << endl;
cout << "Type '1' for Existing user and '2' for New user: ";
if (cin >> ch && (ch == 1 || ch == 2))
break;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid option. Please enter 1 or 2." << endl;
}
User *user = nullptr;
switch (ch)
{
case 1:
user = new ExistingUser();
break;
case 2:
user = new NewUser();
break;
}
user->login();
user->input();
delete user;
return 0;
}