-
Notifications
You must be signed in to change notification settings - Fork 0
/
AttendEase.cpp
230 lines (205 loc) · 5.9 KB
/
AttendEase.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
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
#include <iostream>
#include <string>
#include <map>
#include <memory>
// Abstract Base class for entities with an ID and a name
class Entity
{
public:
Entity(int id, const std::string &name) : id(id), name(name) {}
virtual ~Entity() = default; // Virtual destructor for proper cleanup of derived classes
int getId() const { return id; }
std::string getName() const { return name; }
protected:
void setName(const std::string &newName) { name = newName; }
private:
int id;
std::string name;
};
// Abstract base class for attendance entities
class AttendanceEntity : public Entity
{
public:
AttendanceEntity(int id, const std::string &name) : Entity(id, name) {}
virtual void markAttendance() = 0; // Pure virtual function
virtual int getAttendance() const = 0; // Pure virtual function
};
// Subject class inheriting from AttendanceEntity
class Subject : public AttendanceEntity
{
public:
Subject(const std::string &name) : AttendanceEntity(-1, name), attendanceCount(0) {}
void markAttendance() override
{
++attendanceCount;
}
int getAttendance() const override
{
return attendanceCount;
}
private:
int attendanceCount;
};
// Student class inheriting from Entity
class Student : public Entity
{
public:
Student(int id, const std::string &name) : Entity(id, name) {}
void addSubject(const std::string &subjectName)
{
subjects.emplace(subjectName, Subject(subjectName));
}
void markAttendance(const std::string &subjectName)
{
auto it = subjects.find(subjectName);
if (it != subjects.end())
{
it->second.markAttendance();
}
}
int getAttendance(const std::string &subjectName) const
{
auto it = subjects.find(subjectName);
if (it != subjects.end())
{
return it->second.getAttendance();
}
return 0;
}
void displayDetails() const
{
std::cout << "Student ID: " << getId() << ", Name: " << getName() << std::endl;
for (const auto &subject : subjects)
{
std::cout << " Subject: " << subject.first
<< ", Attendance: " << subject.second.getAttendance() << std::endl;
}
}
private:
std::map<std::string, Subject> subjects;
};
// AttendanceManager class
class AttendanceManager
{
public:
void addStudent(int id, const std::string &name)
{
students.emplace(id, Student(id, name));
}
void addSubjectToStudent(int id, const std::string &subjectName)
{
auto it = students.find(id);
if (it != students.end())
{
it->second.addSubject(subjectName);
}
}
void markStudentAttendance(int id, const std::string &subjectName)
{
auto it = students.find(id);
if (it != students.end())
{
it->second.markAttendance(subjectName);
}
}
int getStudentAttendance(int id, const std::string &subjectName) const
{
auto it = students.find(id);
if (it != students.end())
{
return it->second.getAttendance(subjectName);
}
return 0;
}
void displayAllStudents() const
{
for (const auto &student : students)
{
student.second.displayDetails();
}
}
private:
std::map<int, Student> students;
};
// Main function
int main()
{
AttendanceManager manager;
int choice;
do
{
std::cout << "\n===== Attendance Management System =====\n";
std::cout << "1. Add a new student\n";
std::cout << "2. Add a subject to a student\n";
std::cout << "3. Mark attendance\n";
std::cout << "4. Get attendance for a student\n";
std::cout << "5. Display all students\n";
std::cout << "0. Exit\n";
std::cout << "Enter your choice: ";
std::cin >> choice;
switch (choice)
{
case 1:
{
int id;
std::string name;
std::cout << "Enter student ID: ";
std::cin >> id;
std::cout << "Enter student name: ";
std::cin.ignore();
std::getline(std::cin, name);
manager.addStudent(id, name);
std::cout << "Student added successfully.\n";
break;
}
case 2:
{
int id;
std::string subject;
std::cout << "Enter student ID: ";
std::cin >> id;
std::cout << "Enter subject name: ";
std::cin.ignore();
std::getline(std::cin, subject);
manager.addSubjectToStudent(id, subject);
std::cout << "Subject added to student successfully.\n";
break;
}
case 3:
{
int id;
std::string subject;
std::cout << "Enter student ID: ";
std::cin >> id;
std::cout << "Enter subject name: ";
std::cin.ignore();
std::getline(std::cin, subject);
manager.markStudentAttendance(id, subject);
std::cout << "Attendance marked successfully.\n";
break;
}
case 4:
{
int id;
std::string subject;
std::cout << "Enter student ID: ";
std::cin >> id;
std::cout << "Enter subject name: ";
std::cin.ignore();
std::getline(std::cin, subject);
int attendance = manager.getStudentAttendance(id, subject);
std::cout << "Attendance for student " << id << " in " << subject << ": " << attendance << "\n";
break;
}
case 5:
manager.displayAllStudents();
break;
case 0:
std::cout << "Exiting program. Goodbye!\n";
break;
default:
std::cout << "Invalid choice. Please try again.\n";
}
} while (choice != 0);
return 0;
}