Time.h
#ifndef TIME_H_
#define TIME_H_
class Time
{
protected:
int hour;
int min;
int sec;
public:
// Default constructor
Time()
{
hour = 0;
min = 0;
sec = 0;
}
// Constructor
Time(int h, int m, int s)
{
hour = h;
min = m;
sec = s;
}
// Accessor functions
int getHour() const
{ return hour; }
int getMinute() const
{ return min; }
int getSecond() const
{ return sec; }
};
#endif
MilTime.h
#ifndef MILTIME_H_
#define MILTIME_H_
#include "Time.h"
#include "string"
class MilTime : public Time
{
protected:
int milHours;
int milSeconds;
public:
// Exception class
class BadHour
{
private:
int badHour;
public:
BadHour(int hourVal) : badHour(hourVal)
{ }
int getBadHour() const
{ return badHour; }
};
// Exception class
class BadSeconds
{
private:
int badSeconds;
public:
BadSeconds(int secondsVal) : badSeconds(secondsVal)
{ }
int getBadSeconds() const
{ return badSeconds; }
};
// Constructor
MilTime() : Time()
{
milHours = 0;
milSeconds = 0;
}
// Constructor
MilTime(int mHrs, int mSec)
{
setTime(mHrs, mSec);
}
// Mutator functions
void setTime(const int, const int);
void convertMilTime();
// Accessor functions
std::string getHour() const;
std::string getStandHr() const;
};
#endif
MilTime.cpp
#include "MilTime.h"
#include <iostream>
using std::cin;
using std::cout;
#include <sstream>
using std::stringstream;
#include <string>
using std::string;
/* **********************************************************
MilTime::setTime() : const int, const int
This function sets the values for the milHours and
milSeconds variables. If the hours value is invalid,
BadHour is thrown. Otherwise BadSeconds is thrown.
********************************************************** */
void MilTime::setTime(const int mHrs, const int mSec)
{
if (mHrs < 0 || mHrs > 2359 || mHrs % 100 >= 60)
{
throw BadHour(mHrs);
}
else if (mSec < 0 || mSec > 59)
{
throw BadSeconds(mSec);
}
else
{
milHours = mHrs;
milSeconds = mSec;
convertMilTime();
}
}
/* **********************************************************
MilTime::convertMilTime()
This function converts a military time to standard time,
and stored in the hour, min and sec variables of the Time
class.
********************************************************** */
void MilTime::convertMilTime()
{
if (milHours > 1259)
{
hour = (milHours - 1200) / 100;
}
else if (milHours <= 59)
{
hour = 12;
}
else
{
hour = (milHours / 100);
}
min = milHours % 100;
sec = milSeconds;
}
/* **********************************************************
MilTime::getHour()
This function formats and returns the hour in military
format.
********************************************************** */
string MilTime::getHour() const
{
stringstream ss;
ss.str("");
ss.clear(stringstream::goodbit);
milHours < 10 ? ss << "000" << milHours << ':' :
milHours < 100 ? ss << "00" << milHours << ':' :
milHours < 1000 ? ss << '0' << milHours << ':' : ss << milHours << ':';
milSeconds < 10 ? ss << '0' << milSeconds << "Z Hours" : ss << milSeconds << "Z Hours";
return ss.str();
}
/* **********************************************************
MilTime::getStandHr()
This function formats and returns the hour in standard
format.
********************************************************** */
string MilTime::getStandHr() const
{
stringstream ss;
ss.str("");
ss.clear(stringstream::goodbit);
hour < 10 ? ss << '0' << hour << ':' : ss << hour << ':';
min < 10 ? ss << '0' << min << ':' : ss << min << ':';
sec < 10 ? ss << '0' << sec : ss << sec;
milHours >= 1200 ? ss << " P.M." : ss << " A.M";
return ss.str();
}
TimeFormatException.cpp
#include "MilTime.h"
#include <iostream>
using std::cin;
using std::cout;
int main()
{
int mHrs = 0;
int mSecs = 0;
bool tryAgain = true;
cout << "MILITARY TIME CONVERTER\n\n";
cout << "This program lets you convert Military to 'Standard' time.\n"
<< "Here is an example:\n\n";
// Create a MilTime object and pass a military time to the constructor
MilTime milTime(045, 7);
cout << "MILITARY TIME\n";
cout << milTime.getHour() << "\n\n";
cout << "STANDARD TIME\n";
cout << milTime.getStandHr() << "\n\n";
// Create another MilTime object
MilTime milTimeOne;
// Ask the user to enter a military time
cout << "Please enter a military time\n";
cout << "Hour: ";
cin >> mHrs;
cout << "Second: ";
cin >> mSecs;
cout << "\n";
while (tryAgain)
{
try
{
milTimeOne.setTime(mHrs, mSecs);
tryAgain = false;
}
catch (MilTime::BadHour hourVal)
{
cout << "Error: " << hourVal.getBadHour() << " is no military hour.\n";
cout << "Hour: ";
cin >> mHrs;
}
catch (MilTime::BadSeconds secondsVal)
{
cout << "Error: " << secondsVal.getBadSeconds()
<< " is an invalid second value.\n";
cout << "Seconds: ";
cin >> mSecs;
}
}
cout << "\nMILITARY TIME\n";
cout << milTimeOne.getHour() << "\n";
cout << "\nSTANDARD TIME\n";
cout << milTimeOne.getStandHr() << "\n\n";
cout << "Thank you for trying this program. Have a nice day!";
cin.get();
cin.ignore();
return 0;
}
No comments:
Post a Comment