Skip to content

Commit 3a5fa25

Browse files
feat: added comments
1 parent a171d96 commit 3a5fa25

File tree

11 files changed

+191
-126
lines changed

11 files changed

+191
-126
lines changed

Clock.cpp

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,111 @@
1-
#include "Clock.h"
2-
#include "Display.h"
3-
#include "Utility.h"
4-
#include <iostream>
5-
#include <ctime>
6-
#include <thread>
7-
#include <chrono>
8-
#include <cstdio> // for printf
1+
#include "Clock.h" // Include the Clock class definition.
2+
#include "Display.h" // Include the Display class (used to show digits, colon, AM/PM).
3+
#include "Utility.h" // Include helper functions (for time zones, weather, and logging).
4+
#include <iostream> // For input and output.
5+
#include <ctime> // For working with time.
6+
#include <thread> // For sleep functionality.
7+
#include <chrono> // For time durations (e.g., seconds).
8+
#include <cstdio> // For printf (printing formatted text).
99

1010
using namespace std;
1111

12+
// Constructor for the Clock class.
13+
// It initializes user preferences and calculates the time offsets based on location.
1214
Clock::Clock(const std::string &uName,
1315
const std::string &loc,
1416
int format,
1517
bool dispSec)
16-
: userName(uName),
17-
location(loc),
18-
timeFormat(format),
19-
displaySeconds(dispSec)
18+
: userName(uName), // Initialize the user name.
19+
location(loc), // Initialize the location.
20+
timeFormat(format), // Set the time format (12-hour or 24-hour).
21+
displaySeconds(dispSec) // Determine if seconds should be displayed.
2022
{
21-
// 1. Calculate offsets
23+
// 1. Calculate offsets for time zone and minutes.
2224
timeZoneOffset = Utility::getTimeZoneOffset(location);
2325
minuteOffset = Utility::getMinutesOffset(location);
2426

25-
// 2. Log user preferences
27+
// 2. Log the user’s preferences to a file.
2628
Utility::logUserChoice(userName, location, timeFormat, displaySeconds);
2729

28-
// 3. Fetch weather once and store in weatherInfo
30+
// 3. Fetch the weather information for the location (this happens once).
2931
weatherInfo = Utility::getWeatherInfo(location);
3032
}
3133

34+
// The start() function contains an infinite loop that updates the clock every second.
3235
void Clock::start()
3336
{
34-
while (true)
37+
while (true) // Loop forever until the program is stopped.
3538
{
36-
// Clear screen
39+
// Clear the console screen before re-drawing the time.
3740
Display::clearScreen();
3841

39-
// Get current UTC time
42+
// Get the current UTC time.
4043
time_t now = time(NULL);
41-
tm *t = gmtime(&now);
44+
tm *t = gmtime(&now); // Convert to a UTC time structure.
4245

43-
// Apply offset
46+
// Apply the time zone and minute offsets (to convert UTC to local time).
4447
t->tm_hour += timeZoneOffset;
4548
t->tm_min += minuteOffset;
4649

47-
// Let mktime() fix any out-of-range values
50+
// mktime() fixes any values that might be out of normal range (like minutes over 59).
4851
mktime(t);
4952

53+
// Extract the hour, minute, and second from the time structure.
5054
int hour = t->tm_hour;
5155
int minute = t->tm_min;
5256
int second = t->tm_sec;
5357

58+
// For 12-hour format, determine if it is AM or PM.
5459
bool isPm = false;
5560
if (timeFormat == 12)
5661
{
5762
isPm = (hour >= 12);
58-
hour = hour % 12;
59-
if (hour == 0) hour = 12;
63+
hour = hour % 12; // Convert to 12-hour format.
64+
if (hour == 0) hour = 12; // In a 12-hour clock, 0 is represented as 12.
6065
}
6166

6267
// -------------------- DISPLAY TIME --------------------
63-
Display::displayDigit(hour / 10, 10, 5);
64-
Display::displayDigit(hour % 10, 18, 5);
68+
// Display the hour digits:
69+
Display::displayDigit(hour / 10, 10, 5); // Tens place of the hour.
70+
Display::displayDigit(hour % 10, 18, 5); // Ones place of the hour.
6571

66-
// Colon
72+
// Display a colon (:) between hours and minutes.
6773
Display::displayColon(26, 5);
6874

69-
Display::displayDigit(minute / 10, 34, 5);
70-
Display::displayDigit(minute % 10, 42, 5);
75+
// Display the minute digits:
76+
Display::displayDigit(minute / 10, 34, 5); // Tens place of the minute.
77+
Display::displayDigit(minute % 10, 42, 5); // Ones place of the minute.
7178

79+
// Optionally display seconds if the user chose to.
7280
if (displaySeconds)
7381
{
7482
Display::displayColon(50, 5);
7583
Display::displayDigit(second / 10, 58, 5);
7684
Display::displayDigit(second % 10, 66, 5);
7785
}
7886

79-
// AM/PM label if 12-hour
87+
// If 12-hour format is selected, display the AM/PM indicator.
8088
if (timeFormat == 12)
8189
{
8290
Display::displayAmPm(isPm, 80, 5);
8391
}
8492

8593
// -------------------- DISPLAY DATE --------------------
86-
// Get local date for the chosen location
94+
// Get the local date (formatted according to location).
8795
std::string localDate = Utility::getLocalDate(location);
8896

89-
// Print the date in, say, row=15, col=10
97+
// Move the cursor to a specific position (row 15, column 10) and print the date.
9098
printf("\033[%d;%dH", 15, 10);
9199
printf("\033[1;33mToday in %s: %s\033[0m\n", location.c_str(), localDate.c_str());
92100

93101
// -------------------- DISPLAY WEATHER --------------------
94-
// Print the stored weather info on row=17, col=10
102+
// Move the cursor to row 17, column 10 and display the weather information.
95103
printf("\033[%d;%dH", 17, 10);
96104
printf("\033[1;32mWeather: %s\033[0m\n", weatherInfo.c_str());
97105

98-
fflush(stdout);
106+
fflush(stdout); // Ensure all output is printed immediately.
99107

100-
// Sleep for 1 second
108+
// Pause the program for 1 second before updating the display.
101109
this_thread::sleep_for(chrono::seconds(1));
102110
}
103111
}

Clock.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,28 @@
33

44
#include <string>
55

6+
// The Clock class handles user settings, time calculation, and the display loop.
67
class Clock
78
{
89
private:
9-
std::string userName;
10-
std::string location;
11-
int timeFormat; // 12 or 24
12-
bool displaySeconds;
13-
int timeZoneOffset;
14-
int minuteOffset;
10+
std::string userName; // User’s name.
11+
std::string location; // Location (e.g., London, Tokyo).
12+
int timeFormat; // Time format: 12 or 24 hours.
13+
bool displaySeconds; // Whether to show seconds.
14+
int timeZoneOffset; // Hour offset based on location.
15+
int minuteOffset; // Minute offset (for non-whole hour differences).
1516

16-
// Store fetched weather info
17+
// Weather information fetched from the internet.
1718
std::string weatherInfo;
1819

1920
public:
20-
// Constructor
21+
// Constructor: Initializes the clock with the user’s preferences.
2122
Clock(const std::string &userName,
2223
const std::string &location,
2324
int timeFormat,
2425
bool displaySeconds);
2526

26-
// Starts the clock loop (displays time continuously)
27+
// Starts the continuous clock loop (updating the display every second).
2728
void start();
2829
};
2930

DigitalClock

0 Bytes
Binary file not shown.

Display.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
#include "Display.h"
2-
#include <iostream> // for std::cout
3-
#include <cstdio> // for printf
1+
#include "Display.h" // Include the Display class definition.
2+
#include <iostream> // For standard I/O operations.
3+
#include <cstdio> // For printf.
44
using namespace std;
55

6-
// Initialize static members:
6+
// -------------------- DIGIT PATTERNS --------------------
7+
// Each digit (0-9) is represented by a 7-line pattern of characters.
78
const std::string Display::digits[10][HEIGHT] = {
89
{
910
" ██████╗ ",
@@ -97,6 +98,8 @@ const std::string Display::digits[10][HEIGHT] = {
9798
}
9899
};
99100

101+
// -------------------- COLON AND AM/PM PATTERNS --------------------
102+
// Pattern for the colon ":" used between hour and minute.
100103
const std::string Display::colon[HEIGHT] = {
101104
" ",
102105
"",
@@ -107,6 +110,7 @@ const std::string Display::colon[HEIGHT] = {
107110
" "
108111
};
109112

113+
// Pattern for "AM" displayed in large text.
110114
const std::string Display::am[HEIGHT] = {
111115
" █████╗ ███╗ ███╗",
112116
"██╔══██╗████╗ ████║",
@@ -117,6 +121,7 @@ const std::string Display::am[HEIGHT] = {
117121
" "
118122
};
119123

124+
// Pattern for "PM" displayed in large text.
120125
const std::string Display::pm[HEIGHT] = {
121126
"██████╗ ███╗ ███╗",
122127
"██╔══██╗████╗ ████║",
@@ -127,23 +132,27 @@ const std::string Display::pm[HEIGHT] = {
127132
" "
128133
};
129134

135+
// clearScreen() clears the console using ANSI escape codes.
130136
void Display::clearScreen()
131137
{
132-
// ANSI escape codes to clear screen and move cursor to (0,0)
133-
printf("\033[2J");
134-
printf("\033[H");
138+
printf("\033[2J"); // Clears the screen.
139+
printf("\033[H"); // Moves the cursor to the top-left corner.
135140
}
136141

142+
// displayDigit() prints a single digit (using its ASCII art) at the given x and y coordinates.
137143
void Display::displayDigit(int digit, int x, int y)
138144
{
139-
// Print each line of the digit at the specified (x, y)
145+
// Loop through each line of the digit pattern.
140146
for (int i = 0; i < HEIGHT; i++)
141147
{
148+
// Move the cursor to the correct position.
142149
printf("\033[%d;%dH", y + i, x);
150+
// Print the line with a set foreground color and then reset the color.
143151
printf("%s%s%s", FG_COLOR, digits[digit][i].c_str(), RESET_COLOR);
144152
}
145153
}
146154

155+
// displayColon() prints the colon pattern at the specified coordinates.
147156
void Display::displayColon(int x, int y)
148157
{
149158
for (int i = 0; i < HEIGHT; i++)
@@ -153,9 +162,10 @@ void Display::displayColon(int x, int y)
153162
}
154163
}
155164

165+
// displayAmPm() prints either "AM" or "PM" at the given coordinates based on the isPm flag.
156166
void Display::displayAmPm(bool isPm, int x, int y)
157167
{
158-
const std::string* pattern = isPm ? pm : am;
168+
const std::string* pattern = isPm ? pm : am; // Choose PM if true, else AM.
159169
for (int i = 0; i < HEIGHT; i++)
160170
{
161171
printf("\033[%d;%dH", y + i, x);

Display.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,35 @@
33

44
#include <string>
55

6+
// The Display class handles drawing the clock’s digits and symbols on the console.
67
class Display
78
{
89
private:
9-
static const int HEIGHT = 7;
10+
static const int HEIGHT = 7; // Each digit or symbol is 7 lines tall.
1011

11-
// Console color escape sequences
12-
static constexpr const char* FG_COLOR = "\033[1;32m"; // Bold Green
13-
static constexpr const char* RESET_COLOR = "\033[0m"; // Reset
12+
// ANSI escape codes for colored output.
13+
static constexpr const char* FG_COLOR = "\033[1;32m"; // Bold Green text.
14+
static constexpr const char* RESET_COLOR = "\033[0m"; // Reset to default color.
1415

15-
// Digit patterns
16+
// ASCII art patterns for digits 0-9.
1617
static const std::string digits[10][HEIGHT];
17-
// Colon pattern
18+
// Pattern for the colon.
1819
static const std::string colon[HEIGHT];
19-
// AM/PM patterns
20+
// Patterns for AM and PM.
2021
static const std::string am[HEIGHT];
2122
static const std::string pm[HEIGHT];
2223

2324
public:
24-
// Clears the screen
25+
// Clears the console screen.
2526
static void clearScreen();
2627

27-
// Displays a single digit at x, y
28+
// Displays a single digit at the (x, y) position.
2829
static void displayDigit(int digit, int x, int y);
2930

30-
// Displays colon ':' at x, y
31+
// Displays a colon at the (x, y) position.
3132
static void displayColon(int x, int y);
3233

33-
// Displays either AM or PM at x, y
34+
// Displays either "AM" or "PM" at the (x, y) position.
3435
static void displayAmPm(bool isPm, int x, int y);
3536
};
3637

Input.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
#include "Input.h"
2-
#include "Utility.h"
3-
#include <iostream>
4-
#include <limits>
1+
#include "Input.h" // Include the Input class declaration.
2+
#include "Utility.h" // Include Utility functions (for validating location).
3+
#include <iostream> // For input/output operations.
4+
#include <limits> // For clearing the input buffer.
55
using namespace std;
66

7+
// getUserInput() prompts the user and reads an integer.
8+
// It ensures that the user only enters one of the allowed values (minVal or maxVal).
79
int Input::getUserInput(const std::string &prompt, int minVal, int maxVal)
810
{
911
int input;
@@ -12,24 +14,27 @@ int Input::getUserInput(const std::string &prompt, int minVal, int maxVal)
1214
cout << prompt;
1315
cin >> input;
1416

15-
// Validate input
17+
// Validate that input is correct (not a failure) and matches one of the allowed values.
1618
if (!cin.fail() && (input == minVal || input == maxVal))
1719
{
18-
// Clear the buffer
20+
// Clear any extra characters from the input.
1921
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
2022
break;
2123
}
2224
else
2325
{
26+
// Tell the user the input was invalid and prompt again.
2427
cout << "Invalid input. You can only select " << minVal << " or " << maxVal << "." << endl;
25-
// Clear error flags and discard invalid input
28+
// Clear the error state and discard the bad input.
2629
cin.clear();
2730
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
2831
}
2932
}
3033
return input;
3134
}
3235

36+
// getLocationInput() prompts the user to enter their location.
37+
// It uses Utility::getTimeZoneOffset to check if the location is valid.
3338
std::string Input::getLocationInput()
3439
{
3540
while (true)
@@ -38,7 +43,7 @@ std::string Input::getLocationInput()
3843
string location;
3944
cin >> location;
4045

41-
// Check if location is valid
46+
// If the location returns a valid time zone offset, accept it.
4247
if (Utility::getTimeZoneOffset(location) != -9999)
4348
{
4449
return location;

Input.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33

44
#include <string>
55

6+
// The Input class provides functions to obtain and validate input from the user.
67
class Input
78
{
89
public:
9-
// Prompts the user and reads an integer that must be either minVal or maxVal
10+
// Prompts the user with a message and reads an integer.
11+
// The valid input must be either minVal or maxVal.
1012
static int getUserInput(const std::string &prompt, int minVal, int maxVal);
1113

12-
// Prompts for a location and validates it
14+
// Prompts the user to enter their location and validates it.
1315
static std::string getLocationInput();
1416
};
1517

0 commit comments

Comments
 (0)