README.md

Download
markdown 218 lines 5.2 KB
  1# Student Management System
  2
  3A comprehensive C++17 educational example demonstrating modern C++ features, STL algorithms, smart pointers, file I/O, and exception handling.
  4
  5## Features
  6
  7- **CRUD Operations**: Add, remove, find, and list students
  8- **Smart Pointers**: Uses `std::shared_ptr` for automatic memory management
  9- **STL Algorithms**: Demonstrates `find_if`, `sort`, `accumulate`, `count_if`, `copy_if`
 10- **File Persistence**: Save/load database to/from CSV files
 11- **Exception Handling**: Custom exceptions and error handling
 12- **Interactive CLI**: Menu-driven user interface
 13
 14## File Structure
 15
 16```
 17student_management/
 18├── student.h           # Student class declaration
 19├── student.cpp         # Student class implementation
 20├── database.h          # StudentDatabase class declaration
 21├── database.cpp        # StudentDatabase implementation
 22├── main.cpp            # Interactive CLI application
 23├── Makefile            # Build configuration
 24└── README.md           # This file
 25```
 26
 27## Building
 28
 29### Requirements
 30- C++17 compatible compiler (g++ 7.0+, clang++ 5.0+)
 31- Make
 32
 33### Compile
 34```bash
 35make
 36```
 37
 38### Clean Build
 39```bash
 40make clean
 41make
 42```
 43
 44## Running
 45
 46```bash
 47./student_manager
 48```
 49
 50Or use the Makefile:
 51```bash
 52make run
 53```
 54
 55## Usage Examples
 56
 57### 1. Add Students
 58```
 59Choice: 1
 60Student ID: 1001
 61Name: Alice Johnson
 62Major: Computer Science
 63GPA: 3.85
 64Student added successfully!
 65```
 66
 67### 2. List All Students
 68```
 69Choice: 5
 70================================================================================
 71STUDENT DATABASE (3 students)
 72================================================================================
 73ID:  1001 | Name: Alice Johnson        | Major: Computer Science | GPA: 3.85
 74ID:  1002 | Name: Bob Smith            | Major: Mathematics      | GPA: 3.92
 75ID:  1003 | Name: Carol Williams       | Major: Computer Science | GPA: 3.67
 76================================================================================
 77```
 78
 79### 3. Sort by GPA (Descending)
 80```
 81Choice: 6
 82Sort By:
 831. ID
 842. Name
 853. GPA (descending)
 86Choice: 3
 87Sorted by GPA (descending).
 88```
 89
 90### 4. Statistics
 91```
 92Choice: 7
 93==================================================
 94STATISTICS
 95==================================================
 96Total Students: 3
 97Average GPA: 3.81
 98
 99Students by Major:
100  Computer Science    : 2 student(s)
101  Mathematics         : 1 student(s)
102==================================================
103```
104
105### 5. Save to File
106```
107Choice: 8
108Enter filename (e.g., students.csv): data.csv
109Database saved to data.csv (3 students)
110```
111
112### 6. Load from File
113```
114Choice: 9
115Enter filename: data.csv
116Database loaded from data.csv (3 students)
117```
118
119## C++17 Features Demonstrated
120
121### Smart Pointers
122```cpp
123std::shared_ptr<Student> student = std::make_shared<Student>(id, name, major, gpa);
124std::vector<std::shared_ptr<Student>> students;
125```
126
127### Lambda Expressions
128```cpp
129auto it = std::find_if(students.begin(), students.end(),
130    [id](const std::shared_ptr<Student>& s) {
131        return s->getId() == id;
132    });
133```
134
135### STL Algorithms
136```cpp
137// Sort by GPA (descending)
138std::sort(students.begin(), students.end(),
139    [](const auto& a, const auto& b) {
140        return a->getGpa() > b->getGpa();
141    });
142
143// Calculate average GPA
144double sum = std::accumulate(students.begin(), students.end(), 0.0,
145    [](double total, const auto& s) {
146        return total + s->getGpa();
147    });
148```
149
150### Operator Overloading
151```cpp
152// Stream insertion operator for pretty printing
153friend std::ostream& operator<<(std::ostream& os, const Student& student);
154
155// Comparison operators
156bool operator<(const Student& other) const;
157bool operator==(const Student& other) const;
158```
159
160### Custom Exceptions
161```cpp
162class StudentNotFoundException : public std::exception {
163    const char* what() const noexcept override;
164};
165```
166
167### File I/O with Exception Safety
168```cpp
169void saveToFile(const std::string& filename) const {
170    std::ofstream file(filename);
171    if (!file.is_open()) {
172        throw std::runtime_error("Failed to open file");
173    }
174    // Write data...
175}
176```
177
178## Design Patterns
179
180- **Encapsulation**: Private members with public getters/setters
181- **RAII**: Automatic resource management via smart pointers and RAII objects
182- **Exception Safety**: Proper error handling throughout
183- **Separation of Concerns**: Student, Database, and UI are separate modules
184
185## CSV File Format
186
187```csv
188id,name,major,gpa
1891001,Alice Johnson,Computer Science,3.85
1901002,Bob Smith,Mathematics,3.92
1911003,Carol Williams,Computer Science,3.67
192```
193
194## Error Handling
195
196The system handles various error conditions:
197- Duplicate student IDs
198- Invalid GPA values (must be 0.0-4.0)
199- Student not found errors
200- File I/O errors
201- Invalid CSV format
202
203## Educational Goals
204
205This project is designed to teach:
206
2071. **Modern C++ (C++17)**: Smart pointers, lambdas, auto keyword
2082. **STL Containers**: `std::vector`, `std::map`
2093. **STL Algorithms**: `find_if`, `sort`, `accumulate`, `copy_if`, `count_if`
2104. **Object-Oriented Design**: Classes, encapsulation, operator overloading
2115. **Exception Handling**: Custom exceptions, RAII
2126. **File I/O**: Reading/writing CSV files
2137. **Build Systems**: Makefiles with proper dependencies
214
215## License
216
217MIT License - Educational example for the 03_Study project.