Assignment-1-26032022-095650am

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

NAMES: DANIEL

SARFRAZ
SOHAIB
AHMED
CLASS: BSCS-2B
ROLL NOS: 01-134212-
035
01-134212-
203

MR.BURHAN ABBASI
OOP THEORY

assignment-1
DIGITAL ALARM CLOCK
Object Oriented Programming
Course Code: CSC 210

Assignment 1
Submission Instructions:
- The assignment is to be submitted in group of 2 members.
- Individual submission is not allowed. For exceptions, please seek prior approval
- Submission deadline is 9 April 2022
- Late submission is not allowed
- Make sure to use this file to submit your solutions.

Submission by:
Name: DANIEL SARFARAZ Enrollment Number: 01-134212-035

Name: SOHAIB AHMED Enrollment Number: 01-134212-203

Problem Description
Design an Alarm clock application using your knowledge of object-oriented programming. User can set
an alarm for a specific Time (HH:MM:SS AM/PM). User can also set Title of the alarm. Implement your
designed Class in C++ and write down driver program in main function to show the functionality of
your work. All data members must be initialized through Constructor. Destructor should print your
enrollment number.
Solution should be divided into following parts.
Part a: Entity Name

Part b: List of Data Members

Part c: List of Member Functions

Part d: Complete implementation of class in C++

Part e: Driver Program (main function) to exhibit functionality of your class.

Part e: Screen Shots of output

*Remember to follow best practices, including comments.


 SOLUTION:
CODE:
#include <iostream>
#include <ctime>
#include <windows.h>
#include <cstring>
#include <string>
#include <iomanip>
#include <conio.h>
#include <thread>
#include <vector>

using namespace std;

//This function take an hour, min and sec as variables and sets it to the current time. It also sets a bool true or false to determine if
it's AM or PM
void setCurrentTimeVariables(int&, int&, int&, bool&);

class Alarm
{
private:
string title;
int alarmHour, alarmMinute, alarmSecond;
bool isPM;

public:
//Static int tells us how many Alarm objects exist currently
static int alarmCount;

//Constructor takes in title, hour, min, sec and AM/PM variables and sets Object variables to that defining the when the
Alarm should ring. It also increments the Alarm Count
Alarm(string inputTitle, int inputHour, int inputMinute, int inputSecond, char inputAmPm[])
{
title.assign(inputTitle);

alarmHour = inputHour;
alarmMinute = inputMinute;
alarmSecond = inputSecond;

if (!(strcmp(inputAmPm, "PM")))
{
isPM = true;
}
else
{
isPM = false;
}

alarmCount++;
}

//Returns the title of the Alarm


string getTitle()
{
return title;
}

//Function compares current time variables to Object variables to determine when the Alarm should ring. Returns true if
the Alarm should ring.
bool alarmCheck()
{
int currentHour, currentMinute, currentSecond;
bool isCurrentlyPM;

setCurrentTimeVariables(currentHour, currentMinute, currentSecond, isCurrentlyPM);

if (currentHour == alarmHour && currentMinute == alarmMinute && currentSecond == alarmSecond &&


isCurrentlyPM == isPM)
{
ringAlarm();
return true;
}
else
{
return false;
}
}

//Function rings the Alarm and waits for keypress to stop Alarm.
void ringAlarm()
{
system("CLS");

int i = 0;

cout << "Press any key to stop Alarm: " << title;

while (!_kbhit())
{
Beep(500 + (sin(i) * 200), 100);
i++;
}
}

//Destructor decrements the Alarm Count


~Alarm()
{
alarmCount--;

cout << "\n\nDaniel Sarfraz 01-134212-035\nSohaib Ahmed 01-134212-203";


}
};

int Alarm::alarmCount{ 0 };

void options(bool&, vector<Alarm*>&);

void newAlarm(vector<Alarm*>&);

void deleteAlarm(vector<Alarm*>&);

void alarmCheck(vector<Alarm*>&);

void shutdown(vector<Alarm*>&);

void main()
{
bool programRunning = true;

int currentHour, currentMinute, currentSecond;


bool isCurrentlyPM;
vector<Alarm*> alarms;

while (programRunning)
{
system("CLS");

setCurrentTimeVariables(currentHour, currentMinute, currentSecond, isCurrentlyPM);


cout << "The current time is " << setfill('0') << setw(2) << currentHour << ":" << setw(2) << currentMinute <<
":" << setw(2) << currentSecond << ":" << (isCurrentlyPM ? "PM" : "AM");

cout << "\n\nPress any key for Options";

cout << "\n\n\n" << Alarm::alarmCount << " Current Alarm(s):" << endl;

for (int i = 0; i < Alarm::alarmCount; i++)


{
cout << endl << "Alarm " << i + 1 << ": " << alarms[i]->getTitle() << endl;
}

alarmCheck(alarms);

if (_kbhit())
{
options(programRunning, alarms);
}

Sleep(500);
}

shutdown(alarms);

system("pause");
}

void options(bool& programRunning, vector<Alarm*>& currentAlarm)


{
system("CLS");

cout << "Press N for New Alarm" << endl << "Press D to Delete an Alarm" << endl << "Press Q to Quit Program" <<
endl << "Press Any Other to go back to Main Menu";

_getch();
char pressedKey = _getch();

if (pressedKey == 'N' || pressedKey == 'n')


{
newAlarm(currentAlarm);
}

if (pressedKey == 'D' || pressedKey == 'd')


{
deleteAlarm(currentAlarm);
}

if (pressedKey == 'Q' || pressedKey == 'q')


{
programRunning = false;
}

system("CLS");
}
void setCurrentTimeVariables(int& currentHour, int& currentMinute, int& currentSecond, bool& isCurrentlyPM)
{
struct tm newtime;
time_t now = time(0);
localtime_s(&newtime, &now);

currentHour = newtime.tm_hour % 12;


currentHour = currentHour == 0 ? 12 : currentHour;
currentMinute = newtime.tm_min;
currentSecond = newtime.tm_sec;
isCurrentlyPM = newtime.tm_hour >= 12 ? true : false;
}

void newAlarm(vector<Alarm*>& currentAlarms)


{
system("CLS");

int inputHour, inputMinute, inputSecond;


char inputAmPm[8];
string inputTitle;

cout << "Set title for alarm: ";

getline(cin, inputTitle);

cout << "Input time for Alarm (HH:MM:SS:AM/PM): ";

scanf_s("%d:%d:%d:%s", &inputHour, &inputMinute, &inputSecond, inputAmPm, 8);


cin.ignore();

currentAlarms.emplace_back(new Alarm(inputTitle, inputHour, inputMinute, inputSecond, inputAmPm));


}

void deleteAlarm(vector<Alarm*>& currentAlarms)


{
system("CLS");

string inputNameOfAlarmToDelete;

cout << "Please input the title of the Alarm you want to delete: ";

getline(cin, inputNameOfAlarmToDelete);

for (int i = 0; i < Alarm::alarmCount; i++)


{
if (currentAlarms[i]->getTitle() == inputNameOfAlarmToDelete)
{
delete currentAlarms[i];
currentAlarms.erase(currentAlarms.begin() + i);
}
}
}

void alarmCheck(vector<Alarm*>& currentAlarms)


{
for (int i = 0; i < Alarm::alarmCount; i++)
{
if (currentAlarms[i]->alarmCheck())
{
delete currentAlarms[i];
currentAlarms.erase(currentAlarms.begin() + i);
Sleep(1000);
}
}
}

void shutdown(vector<Alarm*>& currentAlarms)


{
for (int i = 0; i < Alarm::alarmCount; i++)
{
delete currentAlarms[i];
}

currentAlarms.clear();
}

 SCREENSHOTS:
THE CURRENT TIME :

MENU BAR:

TITLE SETUP :
TIME SETUP:

BACK TO HOMEPAGE AFTER SETUP:

ALARM RINGING:
DELETE MENU:

QUIT ALARM:

You might also like