Implement Create Customer functionality

<UserStory> CUS004: Register Customer Account </UserStory>

<Changes>
    1. Added customer registration flow in UserInterface to collect username, name, email, password, and phone inputs.
    2. Added validation for email, password, and phone number before proceeding with registration.
    3. Updated Controller::createCustomer() and UserManagementService::createUser() to support customer registration with full user details including name.
    4. Added duplicate username validation in UserManagementService using map predicate search.
    5. Added user creation and insertion logic in UserManagementService to store newly registered customer records in DataStore.
    6. Added observer attachment for newly registered users to inventory(only for admins), payment, and service notification services.
    7. Added registration confirmation message display after successful customer creation.
</Changes>

<Test>

  Precondition:
  1. Application is running and user is on the main login/register menu.
  2. DataStore is initialized and available for storing user records.
  3. Existing customer records may already be present in the system.

  Steps:
  1. Select the Register Customer option.
  2. Enter valid username, name, email, password, and phone number.
  3. Submit the registration request.
     - Verify that the system accepts valid inputs and creates the customer account.
  4. Attempt registration using an already existing username.
     - Verify that the duplicate registration is rejected.
  5. Register again with valid unique details.
     - Verify that the registration confirmation message is displayed.
  6. Re-access application data after registration.
     - Verify that the customer data is stored persistently.

</Test>

<Review>
Sreeja Reghukumar, please review
</Review>
This commit is contained in:
2026-05-19 20:11:19 +05:30
parent 56c5c2dc70
commit 9533a74d87
5 changed files with 75 additions and 4 deletions
@@ -1,4 +1,5 @@
#include "Controller.h"
#include "Enums.h"
bool Controller::login(const std::string& username, const std::string& password)
{
@@ -13,8 +14,9 @@ void Controller::changePassword(const std::string& 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()
@@ -2,6 +2,7 @@
#include "Map.h"
#include <string>
#include "Enums.h"
#include "UserManagementService.h"
class Service;
class ComboPackage;
@@ -14,11 +15,13 @@ class Notification;
class Controller
{
private:
UserManagementService m_userManagementService;
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);
@@ -1 +1,34 @@
#include <stdexcept>
#include "UserManagementService.h"
#include "ServiceManagementService.h"
#include "PaymentManagementService.h"
#include "InventoryManagementService.h"
#include "User.h"
#include "Factory.h"
#include "Enums.h"
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<User>(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);
}
}
@@ -13,7 +13,7 @@ private:
DataStore& m_dataStore;
public:
UserManagementService() : m_dataStore(DataStore::getInstance()) {}
void createUser(const std::string& username, const std::string& password, const std::string& email, const std::string& phone, util::UserType type);
void createUser(const std::string& username, const std::string& name, const std::string& password, const std::string& email, const std::string& phone, util::UserType type);
void updateUserDetails(const std::string& userID, const std::string& email, const std::string& phone);
util::Map<std::string, User*> getUsers();
util::Map<std::string, User*> getUsers(util::UserType type);
@@ -1,6 +1,7 @@
#include "UserInterface.h"
#include "InputHelper.h"
#include "OutputHelper.h"
#include "Validator.h"
void UserInterface::run()
{
@@ -53,5 +54,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();
}