-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
211 lines (178 loc) · 8 KB
/
main.cpp
File metadata and controls
211 lines (178 loc) · 8 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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
// Define a structure to represent a user
struct User {
string username;
string password;
};
int main() {
// ...
// Define a structure to represent a book
struct Book {
string title;
string author;
string description;
string owner;
};
// Function to list available books
void ListBooks(sql::Connection *con, const string ¤tUser) {
try {
sql::Statement *stmt;
sql::ResultSet *res;
stmt = con->createStatement();
res = stmt->executeQuery("SELECT * FROM books WHERE owner != '" + currentUser + "'");
cout << "Available Books:\n";
while (res->next()) {
cout << "Title: " << res->getString("title") << endl;
cout << "Author: " << res->getString("author") << endl;
cout << "Description: " << res->getString("description") << endl;
cout << "Owner: " << res->getString("owner") << endl;
cout << "-----------------------------\n";
}
delete res;
delete stmt;
} catch (sql::SQLException &e) {
cout << "MySQL error: " << e.what() << endl;
}
}
// Function to search for books
void SearchBooks(sql::Connection *con, const string &searchTerm) {
try {
sql::Statement *stmt;
sql::ResultSet *res;
stmt = con->createStatement();
res = stmt->executeQuery("SELECT * FROM books WHERE title LIKE '%" + searchTerm + "%'");
cout << "Search Results:\n";
while (res->next()) {
cout << "Title: " << res->getString("title") << endl;
cout << "Author: " << res->getString("author") << endl;
cout << "Description: " << res->getString("description") << endl;
cout << "Owner: " << res->getString("owner") << endl;
cout << "-----------------------------\n";
}
delete res;
delete stmt;
} catch (sql::SQLException &e) {
cout << "MySQL error: " << e.what() << endl;
}
}
// Function to propose a trade
void ProposeTrade(sql::Connection *con, const string ¤tUser, const string &bookTitle) {
// Implement trade proposal logic here
// You can update the database to reflect the trade proposal
cout << "Trade proposed for book: " << bookTitle << endl;
// Additional logic to update the database
}
// Modify your main code to call these functions based on user input
// ...
vector<User> users; // Store user accounts
cout << "Are you a USF student? (yes/no): ";
string answer;
cin >> answer;
if (answer == "yes" || answer == "Yes" || answer == "YES") {
string filename = "StudentID.txt"; // Replace with your file's name
ifstream file(filename); // Open the file for reading
if (!file.is_open()) {
std::cerr << "Failed to open the file: " << filename << std::endl;
return 1;
}
std::string numID;
std::cout << "What is your student ID? ";
std::cin >> numID; // Read the user's input as a string
std::string line;
while (std::getline(file, line)) {
// Convert the line to an integer using std::stoi()
int number = std::stoi(line);
// Convert numID to an integer for comparison
int input_to_check = std::stoi(numID);
if (number == input_to_check) {
cout << "Welcome USF Student " << std::endl;
while (true) {
cout << "\n------Go Bulls-----\n";
cout << "1. Log In\n";
cout << "2. Sign Up\n";
cout << "3. Exit\n";
int choice;
cin >> choice;
switch (choice) {
case 1: {
string username, password;
cout << "Enter your username: ";
cin >> username;
cout << "Enter your password: ";
cin >> password;
// Check if the user exists in the vector (simplified for this example)
bool userFound = false;
for (const User& user : users) {
if (user.username == username && user.password == password) {
cout << "Logged in successfully!\n";
userFound = true;
break;
}
}
if (!userFound) {
cout << "Invalid username or password. Please try again.\n";
}
break;
}
case 2: {
string username, password;
cout << "Enter a new username: ";
cin >> username;
do {
cout << "Enter your password: ";
std::cin >> password;
// Check password length
if (password.length() < 8) {
std::cout << "Password is too short. It should be at least 8 characters long.\n";
}
// Check for at least one letter and one digit
else if (!std::any_of(password.begin(), password.end(), ::isdigit) ||
!std::any_of(password.begin(), password.end(), ::isalpha)) {
std::cout << "Password should include both letters and digits.\n";
}
// If password meets criteria, break out of the loop
else {
std::cout << "Password accepted!\n";
break;
}
} while (true);
// Check if the username already exists (simplified for this example)
bool usernameExists = false;
for (const User& user : users) {
if (user.username == username) {
usernameExists = true;
break;
}
}
if (usernameExists) {
cout << "Username already exists. Please choose a different username.\n";
} else {
User newUser = {username, password};
users.push_back(newUser);
cout << "Sign up successful! You can now log in.\n";
}
break;
}
case 3:
cout << "Exiting the program.\n";
return 0;
default:
cout << "Invalid choice. Try again.\n";
}
}
}else {
cout << "You are not a USF student. This program is for USF students only.\n";
break;
}
}
file.close();
return 0;
}else{
cout << "This program is for USF students only, sorry.\n";
}
return 0;
}