/* 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 #include #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(); }