-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
206 lines (179 loc) · 6.47 KB
/
Copy pathmain.cpp
File metadata and controls
206 lines (179 loc) · 6.47 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
/**
* Student Record Management System
* Description: A console-based CRUD application built with C++ and MySQL.
* Employs Object-Oriented Programming principles to manage student data.
*/
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <mysql.h>
using namespace std;
/**
* @class Student
* @brief Represents a single student entity.
* Encapsulates student data to ensure safe data transfer between the UI and database.
*/
class Student {
private:
int id;
string firstName;
string lastName;
string department;
double cgpa;
public:
// Constructor to initialize a new student record
Student(string fName, string lName, string dept, double gpa) {
firstName = fName;
lastName = lName;
department = dept;
cgpa = gpa;
}
// Accessor methods (Getters)
string getFirstName() { return firstName; }
string getLastName() { return lastName; }
string getDepartment() { return department; }
double getCgpa() { return cgpa; }
};
/**
* @class DatabaseManager
* @brief Handles all interactions with the MySQL database.
* Abstracts the C API to provide clean CRUD operations for the main application.
*/
class DatabaseManager {
private:
MYSQL* conn;
/**
* Reads database credentials from an external configuration file.
* This prevents hardcoding sensitive passwords in the source code.
* @return string Password read from db_config.txt
*/
string getDatabasePassword() {
ifstream file("db_config.txt");
string password = "";
if (file.is_open()) {
getline(file, password);
file.close();
} else {
cout << "\n[WARNING] Configuration file 'db_config.txt' missing. Connection may fail." << endl;
}
return password;
}
public:
// Initializes the MySQL connection using external credentials
DatabaseManager() {
conn = mysql_init(0);
string dbPassword = getDatabasePassword();
// Establish connection to the local database server
conn = mysql_real_connect(conn, "127.0.0.1", "root", dbPassword.c_str(), "StudentRecordDB", 3306, NULL, 0);
if (!conn) {
cout << "\n[ERROR] Database Connection failed: " << mysql_error(conn) << endl;
}
}
// Destructor ensures the database connection is gracefully closed
~DatabaseManager() {
if (conn) {
mysql_close(conn);
}
}
/**
* Inserts a new Student object into the database.
* @param student The Student object containing the data to be inserted.
*/
void addStudent(Student student) {
string query = "INSERT INTO students(first_name, last_name, department, cgpa) VALUES ('"
+ student.getFirstName() + "', '"
+ student.getLastName() + "', '"
+ student.getDepartment() + "', "
+ to_string(student.getCgpa()) + ")";
if (mysql_query(conn, query.c_str()) == 0) {
cout << "\n[SUCCESS] Student record saved successfully." << endl;
} else {
cout << "\n[ERROR] Failed to save record: " << mysql_error(conn) << endl;
}
}
/**
* Retrieves and formats all student records from the database into a table.
*/
void viewAllStudents() {
if (mysql_query(conn, "SELECT * FROM students") == 0) {
MYSQL_RES* res = mysql_store_result(conn);
MYSQL_ROW row;
cout << "\n=======================================================" << endl;
cout << left << setw(5) << "ID" << setw(15) << "First Name" << setw(15) << "Last Name" << setw(20) << "Department" << "CGPA" << endl;
cout << "=======================================================" << endl;
while ((row = mysql_fetch_row(res))) {
cout << left << setw(5) << row[0]
<< setw(15) << row[1]
<< setw(15) << row[2]
<< setw(20) << row[3]
<< row[4] << endl;
}
cout << "=======================================================\n" << endl;
mysql_free_result(res);
} else {
cout << "\n[ERROR] Failed to retrieve records: " << mysql_error(conn) << endl;
}
}
/**
* Deletes a student record based on the primary key (ID).
* @param studentId The ID of the student to delete.
*/
void deleteStudent(int studentId) {
string query = "DELETE FROM students WHERE id = " + to_string(studentId);
if (mysql_query(conn, query.c_str()) == 0) {
if (mysql_affected_rows(conn) > 0) {
cout << "\n[SUCCESS] Student ID " << studentId << " has been deleted." << endl;
} else {
cout << "\n[ERROR] Student ID not found in the database." << endl;
}
} else {
cout << "\n[ERROR] Failed to delete record: " << mysql_error(conn) << endl;
}
}
};
/**
* Entry point of the application.
* Manages the main event loop and user interaction via CLI.
*/
int main() {
DatabaseManager db;
int choice;
while (true) {
cout << "\n=== STUDENT RECORD MANAGEMENT SYSTEM ===" << endl;
cout << "1. Add New Student" << endl;
cout << "2. View All Students" << endl;
cout << "3. Delete a Student" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1) {
string fName, lName, dept;
double gpa;
cout << "Enter First Name: ";
cin >> fName;
cout << "Enter Last Name: ";
cin >> lName;
cin.ignore();
cout << "Enter Department (e.g., CS, IT): ";
getline(cin, dept);
cout << "Enter CGPA: ";
cin >> gpa;
Student newStudent(fName, lName, dept, gpa);
db.addStudent(newStudent);
} else if (choice == 2) {
db.viewAllStudents();
} else if (choice == 3) {
int id;
cout << "Enter Student ID to delete: ";
cin >> id;
db.deleteStudent(id);
} else if (choice == 4) {
cout << "Exiting system. Goodbye!" << endl;
break;
} else {
cout << "Invalid choice. Please try again." << endl;
}
}
return 0;
}