164 lines
3.7 KiB
C++
164 lines
3.7 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 "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()
|
|
{
|
|
m_controller.loadSystemData();
|
|
m_controller.runSystemChecks();
|
|
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.saveSystemData();
|
|
}
|
|
|
|
/*
|
|
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 << "Enter username: ";
|
|
util::read(username);
|
|
std::cout << "Enter password: ";
|
|
util::read(password);
|
|
if (m_controller.login(username, password))
|
|
{
|
|
const User* authenticatedUser = m_controller.getAuthenticatedUser();
|
|
if (authenticatedUser != nullptr)
|
|
{
|
|
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
|
|
{
|
|
std::cout << "\nError: Invalid Username or Password";
|
|
}
|
|
}
|
|
|
|
/*
|
|
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 << "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();
|
|
}
|