diff --git a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/controllers/Controller.cpp b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/controllers/Controller.cpp index b9da8b1..e13a879 100644 --- a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/controllers/Controller.cpp +++ b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/controllers/Controller.cpp @@ -1,4 +1,5 @@ #include "Controller.h" +#include "Enums.h" bool Controller::login(const std::string& username, const std::string& password) { @@ -15,8 +16,9 @@ void Controller::changePassword(const std::string& newPassword) m_authenticationManagementService.changePassword(newPassword); } -void Controller::createCustomer(const std::string& username, const std::string& password, const std::string& email, const std::string& phone) +void Controller::createCustomer(const std::string& username, const std::string& name, const std::string& password, const std::string& email, const std::string& phone) { + m_userManagementService.createUser(username, name, password, email, phone, util::UserType::CUSTOMER); } const User* Controller::getAuthenticatedUser() diff --git a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/controllers/Controller.h b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/controllers/Controller.h index e1ecc49..9939284 100644 --- a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/controllers/Controller.h +++ b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/controllers/Controller.h @@ -23,7 +23,7 @@ public: bool login(const std::string& username, const std::string& password); void logout(); void changePassword(const std::string& newPassword); - void createCustomer(const std::string& username, const std::string& password, const std::string& email, const std::string& phone); + void createCustomer(const std::string& username, const std::string& name, const std::string& password, const std::string& email, const std::string& phone); const User* getAuthenticatedUser(); void createTechnician(const std::string& username, const std::string& password, const std::string& email, const std::string& phone); void updateUserDetails(const std::string& email, const std::string& phone); diff --git a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/services/UserManagementService.cpp b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/services/UserManagementService.cpp index 2c2e156..8939f8b 100644 --- a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/services/UserManagementService.cpp +++ b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/services/UserManagementService.cpp @@ -1,7 +1,12 @@ -#include "UserManagementService.h" +#include #include "User.h" #include "Enums.h" #include "Config.h" +#include "UserManagementService.h" +#include "ServiceManagementService.h" +#include "PaymentManagementService.h" +#include "InventoryManagementService.h" +#include "Factory.h" void UserManagementService::ensureAdminExists() { @@ -28,3 +33,29 @@ void UserManagementService::ensureAdminExists() util::UserType::ADMIN); } } + +void UserManagementService::createUser(const std::string& username, const std::string& name, const std::string& password, const std::string& email, const std::string& phone, util::UserType type) +{ + InventoryManagementService inventoryManagementService; + PaymentManagementService paymentManagementService; + ServiceManagementService serviceManagementService; + auto& usersMap = m_dataStore.getUsers(); + int index = usersMap.findIf( + [&](const std::string&, User* user) + { + return user->getUserName() == username; + } + ); + if (index != -1) + { + throw std::runtime_error("Username already exists"); + } + User* newUser = Factory::getObject(username, password, name, phone, email, type); + usersMap.insert(newUser->getId(), newUser); + paymentManagementService.attach(newUser); + serviceManagementService.attach(newUser); + if (newUser->getUserType() == util::UserType::ADMIN) + { + inventoryManagementService.attach(newUser); + } +} diff --git a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/views/UserInterface.cpp b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/views/UserInterface.cpp index b03defd..6e4e26f 100644 --- a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/views/UserInterface.cpp +++ b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/views/UserInterface.cpp @@ -3,6 +3,7 @@ #include "OutputHelper.h" #include "Enums.h" #include "User.h" +#include "Validator.h" void UserInterface::run() { @@ -86,5 +87,37 @@ void UserInterface::login() void UserInterface::registerCustomer() { - + std::string username, name, email, phone, password; + util::clear(); + std::cout << "Enter username: "; + util::read(username); + std::cout << "Enter name: "; + util::read(name); + std::cout << "Enter email: "; + util::read(email); + if (!util::isEmailValid(email)) + { + std::cout << "Error: Email is invalid!"; + util::pressEnter(); + return; + } + std::cout << "Enter password: "; + util::read(password); + if (!util::isPasswordValid(password)) + { + std::cout << "Error: Password is invalid!"; + util::pressEnter(); + return; + } + std::cout << "Enter phone: "; + util::read(phone); + if (!util::isPhoneNumberValid(phone)) + { + std::cout << "Error: Phone number is invalid!"; + util::pressEnter(); + return; + } + m_controller.createCustomer(username, name, password, email, phone); + std::cout << "Registration is successful"; + util::pressEnter(); }