89fc662181
<UserStory> 1960: Service Refactoring </UserStory> UserStory #1960 <Changes> 1. Replaced the Controller load/save workflow with initialize() and shutdown() methods. 2. Moved startup checks such as admin verification, low stock alerts, and payment reminders into Controller::initialize(). 3. Updated UserInterface to use the new Controller initialization and shutdown flow. 4. Updated DataStore::getUsers() and DataStore::getNotifications() to load data directly from shared memory. 5. Added logic to rebuild user notification mappings when user data is loaded from shared memory. 6. Implemented user and notification persistence through DataStore::saveUsers() and DataStore::saveNotifications(). 7. Updated UserManagementService to use TrackedRecord-based shared memory records. 8. Added DataStore locking and unlocking to user management operations for synchronization. 9. Updated createUser(), updateUserDetails(), removeUser(), and deleteNotification() to persist changes through shared memory. 10. Removed legacy loadUsers() and saveUsers() implementations from UserManagementService. 11. Added required header dependencies for shared memory support. </Changes> <Test> N/A </Test> <Review> Sreeja Reghukumar, please review </Review>
191 lines
4.5 KiB
C++
191 lines
4.5 KiB
C++
/*
|
|
File: UserInterface.cpp
|
|
Description: Implementation file containing the method definitions of the
|
|
UserInterface class, including system run loop, login handling,
|
|
and customer registration logic.
|
|
Author: Trenser
|
|
Date:19-May-2026
|
|
*/
|
|
|
|
#include <iostream>
|
|
#include <stdexcept>
|
|
#include "Enums.h"
|
|
#include "InputHelper.h"
|
|
#include "OutputHelper.h"
|
|
#include "User.h"
|
|
#include "UserInterface.h"
|
|
#include "Validator.h"
|
|
|
|
/*
|
|
Function: run
|
|
Description: Runs the main system loop, displaying the initial menu for login,
|
|
customer registration, or exit. Handles exceptions gracefully.
|
|
Parameter: None
|
|
Return type: void
|
|
*/
|
|
void UserInterface::run()
|
|
{
|
|
try
|
|
{
|
|
if (!m_controller.initialize())
|
|
{
|
|
std::cout << "Error: Failed to initialize the system!";
|
|
return;
|
|
}
|
|
bool isMenuActive = true;
|
|
while (isMenuActive)
|
|
{
|
|
try
|
|
{
|
|
int choice;
|
|
util::clear();
|
|
std::cout << "Vehicle Service System\n1. Login\n2. Register Customer\n3. Exit\nEnter your Choice: ";
|
|
util::read(choice);
|
|
if (!handleOperation(choice))
|
|
{
|
|
isMenuActive = false;
|
|
}
|
|
}
|
|
catch (const std::exception& e)
|
|
{
|
|
std::cout << "Exception: " << e.what() << std::endl;
|
|
util::pressEnter();
|
|
}
|
|
}
|
|
m_controller.shutdown();
|
|
}
|
|
catch (const std::invalid_argument& exception)
|
|
{
|
|
std::cout << "Exception: Invalid Argument: " << exception.what() << std::endl;
|
|
}
|
|
catch (const std::exception& exception)
|
|
{
|
|
std::cout << "Exception: " << exception.what() << std::endl;
|
|
}
|
|
catch (...)
|
|
{
|
|
std::cout << "Unknown error occurred." << std::endl;
|
|
}
|
|
}
|
|
|
|
/*
|
|
Function: handleOperation
|
|
Description: Executes the corresponding system operation based on the selected menu choice.
|
|
Parameter: int choice - selected menu option
|
|
Return type: bool - true if menu continues, false if exit
|
|
*/
|
|
bool UserInterface::handleOperation(int choice)
|
|
{
|
|
switch (choice)
|
|
{
|
|
case 1:
|
|
login();
|
|
break;
|
|
case 2:
|
|
registerCustomer();
|
|
break;
|
|
case 3:
|
|
std::cout << "Exiting..." << std::endl;
|
|
return false;
|
|
default:
|
|
std::cout << "Enter a valid choice!" << std::endl;
|
|
util::pressEnter();
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/*
|
|
Function: login
|
|
Description: Handles user login by validating credentials. Based on the authenticated
|
|
user type, navigates to the appropriate menu (Admin, Technician, Customer).
|
|
Parameter: None
|
|
Return type: void
|
|
*/
|
|
void UserInterface::login()
|
|
{
|
|
std::string username, password;
|
|
util::clear();
|
|
std::cout << "Login\n";
|
|
std::cout << "Enter username: ";
|
|
util::read(username);
|
|
std::cout << "Enter password: ";
|
|
util::readPassword(password);
|
|
if (m_controller.login(username, password))
|
|
{
|
|
const User* authenticatedUser = m_controller.getAuthenticatedUser();
|
|
if (authenticatedUser && authenticatedUser->getState() != util::State::INACTIVE)
|
|
{
|
|
switch (authenticatedUser->getUserType())
|
|
{
|
|
case util::UserType::ADMIN:
|
|
m_adminMenu.showMenu();
|
|
break;
|
|
case util::UserType::TECHNICIAN:
|
|
m_technicianMenu.showMenu();
|
|
break;
|
|
case util::UserType::CUSTOMER:
|
|
m_customerMenu.showMenu();
|
|
break;
|
|
default:
|
|
std::cout << "\nError: Unknown user type";
|
|
break;
|
|
}
|
|
}
|
|
else if (authenticatedUser && authenticatedUser->getState() == util::State::INACTIVE)
|
|
{
|
|
std::cout << "\nError: Your account has been disabled. Please contact your Administrator.";
|
|
util::pressEnter();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
std::cout << "\nError: Invalid Username or Password";
|
|
util::pressEnter();
|
|
}
|
|
}
|
|
|
|
/*
|
|
Function: registerCustomer
|
|
Description: Registers a new customer by collecting and validating details such as
|
|
username, name, email, password, and phone number. Delegates creation
|
|
to the controller.
|
|
Parameter: None
|
|
Return type: void
|
|
*/
|
|
void UserInterface::registerCustomer()
|
|
{
|
|
std::string username, name, email, phone, password;
|
|
util::clear();
|
|
std::cout << "Register Customer\n";
|
|
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::readPassword(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();
|
|
} |