https://github.com/maximilianfeldthusen/simpleusermanagementsystem
https://github.com/maximilianfeldthusen/simpleusermanagementsystem
Last synced: 6 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/maximilianfeldthusen/simpleusermanagementsystem
- Owner: maximilianfeldthusen
- License: bsd-3-clause
- Created: 2025-03-19T02:05:03.000Z (7 months ago)
- Default Branch: TFD
- Last Pushed: 2025-03-19T02:10:00.000Z (7 months ago)
- Last Synced: 2025-03-19T03:22:15.876Z (7 months ago)
- Language: C++
- Size: 5.86 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Documentation
### SimpleUserManagementSystem
This C++ code implements a simple user management system using classes, exception handling, and smart pointers for memory management. Let's break down the code step-by-step:
### 1. Includes and Namespace
```cpp
#include
#include
#include
#include
#include
```
These include directives allow the program to use various standard library features:
- `` for input and output (using `std::cout` and `std::cerr`).
- `` for using the `std::string` class.
- `` for using the `std::vector` container.
- `` for smart pointers (specifically `std::unique_ptr`).
- `` for standard exception classes like `std::invalid_argument`.### 2. User Class
```cpp
class User {
public:
User(const std::string& name, int age) : name(name), age(age) {
if (age < 0) {
throw std::invalid_argument("Age cannot be negative");
}
}void display() const {
std::cout << "User: " << name << ", Age: " << age << std::endl;
}private:
std::string name;
int age;
};
```
- **Constructor**: Takes a `name` and `age` as parameters. If the `age` is negative, it throws an `std::invalid_argument` exception.
- **display()**: A member function that prints out the user's name and age.
- **Private Members**: `name` and `age` are private, ensuring they can only be accessed through the class's public methods.### 3. UserManager Class
```cpp
class UserManager {
public:
void addUser(const std::string& name, int age) {
// Validate input
if (name.empty() || name.length() > 50) {
throw std::invalid_argument("Name must be between 1 and 50 characters");
}
users.emplace_back(std::make_unique(name, age));
}void displayUsers() const {
for (const auto& user : users) {
user->display();
}
}private:
std::vector> users; // Use smart pointers for memory management
};
```
- **addUser()**: This method takes a `name` and `age` and validates them. If the name is empty or longer than 50 characters, it throws an exception. If the input is valid, it creates a new `User` object using `std::make_unique` (which returns a `std::unique_ptr` to handle dynamic memory safely) and adds it to the `users` vector.
- **displayUsers()**: This method iterates through the `users` vector and calls the `display()` method of each `User` object to output their information.
- **Private Member**: `users` is a vector of `std::unique_ptr`, ensuring that the memory for `User` objects is managed automatically (no need to manually delete them).### 4. Main Function
```cpp
int main() {
UserManager userManager;// Try to add users
try {
userManager.addUser("Alice", 30);
userManager.addUser("Bob", 25);
} catch (const std::exception& e) {
std::cerr << "Error adding user: " << e.what() << std::endl;
}// Attempt to add a user with invalid age
try {
userManager.addUser("Charlie", -5); // This will throw an exception
} catch (const std::exception& e) {
std::cerr << "Error adding user: " << e.what() << std::endl; // Handle exceptions
}// Display users
userManager.displayUsers();return 0;
}
```
- **Creating UserManager**: An instance of `UserManager` is created.
- **Adding Users**: The program tries to add two users, "Alice" and "Bob". If any exceptions occur during this process, they are caught, and an error message is printed to the standard error stream.
- **Invalid User Attempt**: The program attempts to add a user "Charlie" with a negative age, which will cause an exception to be thrown and caught, with an error message printed.
- **Displaying Users**: Finally, the program calls `displayUsers()` to print the details of all valid users that were added.### Summary
The code demonstrates:
- Class design and encapsulation by organizing user-related functionality into `User` and `UserManager` classes.
- Exception handling to manage invalid inputs gracefully.
- Usage of smart pointers for automatic memory management, preventing memory leaks.
- Basic input validation to ensure data integrity.