|
| 1 | +#ifndef UTILS_H |
| 2 | +#define UTILS_H |
| 3 | + |
| 4 | +#include "timetable.h" |
| 5 | +#include <regex> |
| 6 | +#include <iostream> |
| 7 | +#include <string> |
| 8 | + |
| 9 | +constexpr int MAX_DAYS = 7; |
| 10 | +constexpr int MAX_SLOTS = 24; |
| 11 | +constexpr int MAX_ROOMS = 50; |
| 12 | +constexpr int MAX_COURSES = 100; |
| 13 | + |
| 14 | +// Robust numeric input |
| 15 | +inline int getSafeInt(const std::string &prompt, int minVal, int maxVal) { |
| 16 | + while (true) { |
| 17 | + std::cout << prompt; |
| 18 | + std::string line; |
| 19 | + std::getline(std::cin, line); |
| 20 | + |
| 21 | + line.erase(0, line.find_first_not_of(" \t\n\r")); |
| 22 | + line.erase(line.find_last_not_of(" \t\n\r") + 1); |
| 23 | + |
| 24 | + if (line.empty()) { |
| 25 | + std::cout << "Input cannot be empty! Please enter a number.\n"; |
| 26 | + continue; |
| 27 | + } |
| 28 | + |
| 29 | + bool allDigits = true; |
| 30 | + for (char c : line) { |
| 31 | + if (!isdigit(c)) { allDigits = false; break; } |
| 32 | + } |
| 33 | + if (!allDigits) { |
| 34 | + std::cout << "Invalid input! Enter a numeric integer.\n"; |
| 35 | + continue; |
| 36 | + } |
| 37 | + |
| 38 | + try { |
| 39 | + int val = std::stoi(line); |
| 40 | + if (val < minVal || val > maxVal) { |
| 41 | + std::cout << "Invalid input! Enter integer between " << minVal << " and " << maxVal << ".\n"; |
| 42 | + continue; |
| 43 | + } |
| 44 | + return val; |
| 45 | + } catch (...) { |
| 46 | + std::cout << "Invalid input! Could not convert to number.\n"; |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// Non-empty string input |
| 52 | +inline std::string getNonEmptyString(const std::string &prompt) { |
| 53 | + std::string s; |
| 54 | + while (true) { |
| 55 | + std::cout << prompt; |
| 56 | + std::getline(std::cin, s); |
| 57 | + if (!s.empty()) return s; |
| 58 | + std::cout << "Input cannot be empty! Please enter a valid value.\n"; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// Validate slot label (HH:MM-HH:MM or single uppercase letter) |
| 63 | +inline bool isValidSlotLabel(const std::string &s) { |
| 64 | + std::regex timePattern(R"(\d{1,2}:\d{2}-\d{1,2}:\d{2})"); |
| 65 | + std::regex letterPattern(R"([A-Z])"); |
| 66 | + return std::regex_match(s, timePattern) || std::regex_match(s, letterPattern); |
| 67 | +} |
| 68 | + |
| 69 | +inline std::string getSlotLabel(int i) { |
| 70 | + std::string label; |
| 71 | + while (true) { |
| 72 | + std::cout << "Slot " << i + 1 << " label (HH:MM-HH:MM or A-Z): "; |
| 73 | + std::getline(std::cin, label); |
| 74 | + if (label.empty()) label = "Slot " + std::to_string(i + 1); |
| 75 | + if (isValidSlotLabel(label)) break; |
| 76 | + std::cout << "Invalid format! Use HH:MM-HH:MM (09:00-10:00) or single letter (A-Z).\n"; |
| 77 | + } |
| 78 | + return label; |
| 79 | +} |
| 80 | + |
| 81 | +inline Timetable getUserInput() { |
| 82 | + int days = getSafeInt("Enter number of days in week (1-7): ", 1, MAX_DAYS); |
| 83 | + int slots = getSafeInt("Enter slots per day (1-24): ", 1, MAX_SLOTS); |
| 84 | + |
| 85 | + std::vector<std::string> slotLabels(slots); |
| 86 | + std::cout << "\nEnter label or time for each slot:\n"; |
| 87 | + for (int i = 0; i < slots; ++i) slotLabels[i] = getSlotLabel(i); |
| 88 | + |
| 89 | + int numRooms = getSafeInt("\nEnter number of rooms (1-50): ", 1, MAX_ROOMS); |
| 90 | + std::vector<Room> rooms(numRooms); |
| 91 | + for (int i = 0; i < numRooms; ++i) rooms[i].name = getNonEmptyString("Room " + std::to_string(i + 1) + " name: "); |
| 92 | + |
| 93 | + int numCourses = getSafeInt("\nEnter number of courses (1-100): ", 1, MAX_COURSES); |
| 94 | + std::vector<Course> courses(numCourses); |
| 95 | + |
| 96 | + int totalAvailableSlots = days * slots * numRooms; |
| 97 | + |
| 98 | + for (int i = 0; i < numCourses; ++i) { |
| 99 | + std::cout << "\nCourse " << i + 1 << " details:\n"; |
| 100 | + courses[i].name = getNonEmptyString("Course name: "); |
| 101 | + courses[i].instructor = getNonEmptyString("Instructor name: "); |
| 102 | + courses[i].sessionsPerWeek = getSafeInt("Sessions per week: ", 1, totalAvailableSlots); |
| 103 | + |
| 104 | + int prefCount = getSafeInt("Number of preferred slots (0 for none): ", 0, slots); |
| 105 | + courses[i].allowedSlots.clear(); |
| 106 | + for (int j = 0; j < prefCount; ++j) { |
| 107 | + int slot = getSafeInt("Preferred slot index (1-" + std::to_string(slots) + "): ", 1, slots); |
| 108 | + courses[i].allowedSlots.push_back(slot - 1); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return Timetable(days, slots, rooms, courses, slotLabels); |
| 113 | +} |
| 114 | + |
| 115 | +#endif |
0 commit comments