Files
Datastructs/Exam/IKT203Exam/Portfolio/Assignment-02/option1.h
2025-11-18 13:23:54 +01:00

118 lines
3.5 KiB
C++

// Option 1 (Standard): Cruise Ship Manifest.
// Uses linked lists, merge sort, quick sort, and binary search
// to manage guest and employee manifests from random_names.txt.
#pragma once
#ifndef OPTION1_H
#define OPTION1_H
#include "TLinkedList.h"
#include "TPerson.h"
// Global lists and counters used across the assignment:
// - 'e' stores EMPLOYEE records
// - 'g' stores GUEST records
// - guestCount / employCount track how many were loaded
inline TLinkedList g, e;
inline int guestCount = 0;
inline int employCount = 0;
int RunApp();
inline void printline()
{
std::cout << "----------------------------------------" << std::endl;
}
inline void pack(const std::string& line)
{
std::cout << "\n\n\n" << std::endl;
printline();
std::cout << line << std::endl;
printline();
std::cout << "\n\n\n" << std::endl;
}
/**
* @brief Callback function to process one name.
static bool NameReadCallback(const int aIndex, const int aTotalCount, const std::string& aFirstName, const std::string& aLastName)
{
std::cout << "Reading Name " << (aIndex + 1) << " of " << aTotalCount << ": "
<< aFirstName << " " << aLastName << "\n";
// We only want to read 10 names (index 0 through 9)
// Return false when aIndex is 9 to stop the loop after this one.
return (aIndex < 9);
}
*/
// Callback used by readNamesFromFile.
// - Creates a TPerson with status (EMPLOYEE or GUEST)
// - First 1500 entries are EMPLOYEE, the rest are GUEST
// - Appends each person to the appropriate linked list and updates counters
static bool onNameRead(const int aIndex, const int aTotalCount, const std::string& aFirstName, const std::string& aLastName)
{
// Determine status based on index: first 1500 are employees, rest are guests.
const ENumStatus status = (aIndex < 1500) ? EMPLOYEE : GUEST;
const TPerson p(aFirstName, aLastName, status);
if (status == EMPLOYEE) {
e.Append(p);
employCount++;
}
else {
g.Append(p);
guestCount++;
}
std::cout << "[" <<aIndex << "] " << aLastName << ", " << aFirstName << " | status: " << (status == 1 ? "Employee" : "Guest")
<< " | cabin size: " << p.cabinSize << std::endl;
return true;
}
// Binary-search helper:
// - Performs binary search on a sorted array of TPerson* (alphabetical by last name)
// - 'target' is the surname entered by the user
// - Expands left/right from the first match to find and print all matches
inline void SearchAndPrint(TPerson** targetArray, int arraySize, const std::string& target)
{
int index = Utils::BinarySearch(targetArray, 0, arraySize - 1, target);
if (index == -1) {
std::cout << "No match found" << std::endl;
return;
}
int left = index - 1;
int right = index + 1;
// Move left while neighbouring entries share the same first or last name
while (left >= 0 && (targetArray[left]->firstName == target || targetArray[left]->lastName == target)) {
--left;
}
// Move right while neighbouring entries share the same first or last name
while (right < arraySize && (targetArray[right]->firstName == target || targetArray[right]->lastName == target)) {
++right;
}
for (int i = left + 1; i < right; ++i) {
std::cout << "Match found: \nName: " << targetArray[i]->firstName << " "
<< targetArray[i]->lastName << " | status: " << (targetArray[i]->status == 0 ? "Guest" : "Employee")
<< " | cabinsize: " << targetArray[i]->cabinSize << "\n" << std::endl;
}
}
#endif // OPTION1_H