Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
mihirp161 authored Aug 26, 2019
1 parent 50edd16 commit bf63e83
Show file tree
Hide file tree
Showing 11 changed files with 936 additions and 0 deletions.
179 changes: 179 additions & 0 deletions Calendar_IO.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/*
Author: Mihir K Patel
Purpose: Following is a implementation file for the CalendarIO.h. This contains function defintions
for those in its header file. Main properties of this file are:
1) To do computation on the Date.
2) To write a .csv format calendar to the folder, which is more readable than .txt file.
3) Most importantly, to transfer the control from Main() using istream.
*/
#include "Calendar_IO.h"
/*Counter*/
short int CalendarIO::cycle_counter = 0;
/*Defaults constructor of CalendarIO*/
CalendarIO::CalendarIO() {
month = 0;
day = 0;
}
/*Guesses the next period*/
void CalendarIO::guess_the_day() {
for (std::vector<int>::iterator itr = length_container.begin(); itr != length_container.end(); ++itr) {
next_cycle_guess = next_cycle_guess + *itr;
if (itr == length_container.end() - 1) {
next_cycle_guess = (static_cast<int>(next_cycle_guess / cycle_counter)) - OFFSET_DOS;
}
}
}
/*This function shifts the date accordingly to guess_the_day() helper function*/
void CalendarIO::adjust_day_month(int* days_arr, short int remaining_days) {
try {
if (days_arr != nullptr) {
for (int x = 0; x <= remaining_days; ++x) { /*For loop runs until the remaining days are added to the day*/
if (day < days_arr[month - 1]) {
++day;
}
else if ((day == days_arr[month - 1]) && (month < 12)) {
day = 1;
++month;
}
else if ((day == days_arr[month - 1]) && (month == 12)) {
day = 1;
month = 1;
}
else {
throw ("");
}
}
}
else { throw (""); }
}
catch (...) {
std::cerr << "Date shifting failed.\n" << std::endl;
}
}
/*Writes the .csv formatted calendar to a folder*/
void CalendarIO::write_calendar(CalendarIO & C_IO, std::string * months_arr) {
try {
srand((unsigned int)time(nullptr));
int rand_quote = 1 + rand() % 7;
std::ofstream outputFile("VaccationPlanner.csv");
if (outputFile.is_open() && (months_arr != nullptr)) {
/*2D vector creates a grid where months are in column and days in rows*/
std::vector<std::vector<std::string>> cal_mat(32, std::vector<std::string>(13));
/*This creates the rows and columns*/
for (size_t x = 0; x < 32; x++) { /*{Prints the rows*/
for (size_t y = 0; y < 13; y++) { /*Prints the columns*/
if ((x == 0) && (y > 0)) cal_mat[x][y] = " " + months_arr[y - 1]; /*Prints months*/
else if ((y == 0) && (x > 0)) cal_mat[x][y] = std::to_string(x); /*Prints the numbers*/
else cal_mat[x][y] = " ";
cal_mat[C_IO.day][C_IO.month] = " X,"; /*Marker to give visual representation of expected date*/
outputFile << cal_mat[x][y] + ","; /*Comma seperates the columns*/
}
outputFile << std::endl;
}
outputFile << C_IO.quotes_array[0] << std::endl << C_IO.quotes_array[rand_quote]; /*Prints random quote in the end*/
outputFile.close();
}
else { throw (""); }
}
catch (...) {
std::cerr << "File writing failed.\n" << std::endl;
}

}
/* Output the next date using ostream overridden virtual functions*/
std::ostream& CalendarIO::print_out(std::ostream & out) const {
out << KBCYN << "\nREMINDER!" << KNRM << std::endl << "->Next Period will be on: " << get_month() << "/" << get_day() << '\n';
out << "->On average, Menstrual Cycle would last " << (get_next_cycle_guess() + OFFSET_DOS) << " days." << '\n';
return out;
}
/*This istream takes in all of the necessary input from the user to give his closed-by/her next menstrual date*/
std::istream & operator>> (std::istream & in, CalendarIO & C_IO) {
char choice = ' ';
std::string day_s = "";
std::string cycle_len_s = "";
for (;;) {
try {
std::cout << "Enter first day of your or your relative's last Period (mm/dd) or (mm-dd): ";
std::getline(in, day_s);
/*Takes the string of date, do the operation in its substring*/
C_IO.month = std::stoi(day_s.substr(0, 2));
C_IO.day = std::stoi(day_s.substr(3, 2));
/*Input validation for the day and month*/
if ((C_IO.month >= 1 && C_IO.month <= 31) && !isdigit(C_IO.month)) {
if ((C_IO.day >= 1 && C_IO.day <= 31) && !isdigit(C_IO.day) && (C_IO.month == 1 || C_IO.month == 3 || C_IO.month == 5
|| C_IO.month == 7 || C_IO.month == 8 || C_IO.month == 10 || C_IO.month == 12)) {
break;
}
else if ((C_IO.day >= 1 && C_IO.day <= 30) && !isdigit(C_IO.day) && (C_IO.month == 4 || C_IO.month == 6 || C_IO.month == 9
|| C_IO.month == 11)) {
break;
}
else if ((C_IO.day >= 1 && C_IO.day <= 28) && !isdigit(C_IO.day) && (C_IO.month == 2)) { break; }
else if (C_IO.day == 29 && C_IO.month == 2 && !isdigit(C_IO.day)) { break; }
else { throw (""); }
}
else { throw (""); }
}
catch (...) {
std::cerr << KBRED << "Invalid Date entry. Kindly try again." << KNRM << std::endl;
in.clear();
}
}
/*Asks for the Menstrual Cycle days, with its normal input validation*/
for (;;) {
try {
std::cout << "What is the usual duration (in days) between monthly Periods: ";

while (true) {
std::getline(in, cycle_len_s);
C_IO.cycle_length = std::stoi(cycle_len_s.substr(0, 2));
if (!isdigit(C_IO.cycle_length) && !(C_IO.cycle_length >= 20 && C_IO.cycle_length <= 40)) { throw(""); }

C_IO.length_container.push_back(C_IO.cycle_length);
CalendarIO::cycle_counter++;

std::cout << "Would you like to give any more of Menstrual Cycles (days between periods)?";
choice = Period::check_choice();
if (tolower(choice) == 'y') {
std::cout << "Ok, how many days did the Cycle before previous Cycle lasted?: ";
}
if (tolower(choice) == 'n') { break; }
in.ignore();
}
break;
}
catch (...) {
in.clear();
std::cerr << KBRED << "An abnormal cycle given or entry is invalid. If former, contact your primary doctor; else re-try!" << KNRM << std::endl;
}
}
/*Leap year passes the array container which is used by the adjusted_day_month() function*/
std::cout << "Is this a Leap year?";
choice = Period::check_choice();

if (tolower(choice) == 'y') {
int leap_array[] = { 31,29,31,30,31,30,31,31,30,31,30,31 };
C_IO.guess_the_day();
short int days_adjustment = C_IO.get_next_cycle_guess() % 366; /*Adjust the limiting condition for a for-loop and passes it below*/
C_IO.adjust_day_month(leap_array, days_adjustment);
}
else if (tolower(choice) == 'n') {
int not_leap_array[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
C_IO.guess_the_day();
short int days_adjustment = C_IO.get_next_cycle_guess() % 365;
C_IO.adjust_day_month(not_leap_array, days_adjustment);
}
/*Self explanitpry, check choice, if y, write the calendar*/
std::cout << "Finally, would you like to get a Calendar with expected Period starting date?";
choice = Period::check_choice();
in.ignore();

if (tolower(choice) == 'y') {
CalendarIO::write_calendar(C_IO, C_IO.months_array);
std::cout << KBYEL << "\nA CSV file 'VacationPlanner' has been made in patel_mihir_FP2 folder."
<< "Don't forget to enable the Gridlines option in Page Layout Option when printing." << KNRM << std::endl;
}
return in;
}

CalendarIO::~CalendarIO() { }
52 changes: 52 additions & 0 deletions Calendar_IO.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Author: Mihir K Patel
Purpose: Following is a Header file for the CalendarIO.cpp. This inherits a parent class named Period.h
This contains function protypes. Few of the main prototypes are adjust_day_month(array, int), which
shifts the date to give an accurate result for the next Period. A static function
write_function(CalendarIO, array) which makes a CSV format calendar that reflects next Period with
a marker. A virtual function of overridden ostream<< that prints the results displaying date change and
expected date. Lastly, a friend function of istream>> that allows interaction with this class.
*/
#ifndef DOT_MATE_A_GUIDE_TO_MENSTRUATION_CALENDERIO
#define DOT_MATE_A_GUIDE_TO_MENSTRUATION_CALENDERIO
#define OFFSET_DOS 2
/*Class specific overhead*/
#include "Period.h"
#include <cstdlib>

class CalendarIO :public Period
{
private:
static short int cycle_counter; /*Counter for cycle input*/
short int month = 0; /*Holds the month*/
short int day = 0; /*Holds the day*/

std::string months_array[12]{ "January","February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December" };/*Months container*/
/*Prints quote in the VacationPlanner.csv (comma-delimited)*/
std::string quotes_array[8]{ "Today's, quotes:,",
"Last time, one man, said to, a woman, that she, can't fly, a plane, then she, flew one, around, the world.,",
"Men are, without, a vagina, therefore, there's, a $20B, market, for the, Sanitary, Pads.,",
"If you, want to, compare, genders, then let, me tell, you: Wo,men are, not only ,competin,g they're, beating, you!,",
"So you, were told, to shut u,p. To clos,e your eye,s & to po,p your ear,drums....,my questi,on is: Wi,ll you?,",
"My body,can be, put on, chains., But my, soul will, never be, able to, comply.,",
"God gave, us hands, to love, and to, work and, most im,portantly-, to slap, when the, need arise.,",
"Duty for, my count,ry is first, but duty, for you, comes later.," };

/*Pure function that guesses the next period days which would get added into date*/
void guess_the_day();
void adjust_day_month(int* days_arr, short int remaining_days); /*Offsets the date with next_cycle_guess*/

static void write_calendar(CalendarIO&, std::string* months_arr); /*Prototype for writing a calendar to folder*/
public:
CalendarIO();

/*Simple getters to get the day and month*/
int get_day() const { return day; }
int get_month() const { return month; }

virtual std::ostream& print_out(std::ostream& out) const override; /*Prototype for ostream virtual function*/
friend std::istream& operator>> (std::istream&, CalendarIO&); /*Prototype for istream friend function*/
~CalendarIO();
};
#endif // !DOT_MATE_A_GUIDE_TO_MENSTRUATION_CALENDERIO
134 changes: 134 additions & 0 deletions Log_IO.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
Author: Mihir K Patel
Purpose: Following is a implementation file for the LogIO.h. This contains function defintions
for those in its header file. Main properties of this file are:
1) To create a log file with users names.
2) Take user names.
3) To greet the users.
4) To make user-specific log file
*/
#include "Log_IO.h"
LogIO::LogIO() { /*Simple constructor*/
original_user_name = "";
log_file_name = "";
}
/*Simple setter*/
void LogIO::set_name(std::string name_para) { /*Set the name in correct format*/
name_para.erase(std::remove_if(name_para.begin(), name_para.end(), ::isspace), name_para.end()); /*Removes the spaces*/
transform(name_para.begin(), name_para.end(), name_para.begin(), ::tolower); /*Lowers the letters*/
*user_name = name_para;
set_file_name(user_name);
}
/*Writes the names to file*/
void LogIO::write_user_names(std::string user_name_para) {
std::ofstream name_file; ///--------->>>Want to make the users.txt, currently premade one exist
try {
/*t makes it refrence to current time, then *tm ptr get the time out of the system time.*/
std::time_t t = std::time(0);
std::tm* now = std::localtime(&t);

name_file.open("users.txt", std::ios::out | std::ios::app);
if (name_file.fail()) { throw ("Name write failed."); }
/*Prints the date::time, and then tab then name and then new line in a file*/
name_file << (now->tm_mon + 1) << '/' << (now->tm_mday) << '/' << (now->tm_year + 1900) << "::" << (now->tm_hour) << ':' << (now->tm_min) << ' ';
name_file << user_name_para << std::endl;

name_file.close();
}
catch (const std::exception & e) { std::cerr << e.what() << std::endl; } ///------>>Check these since these are not stl
}
/*Writes the question to the file*/
void LogIO::write_search(std::string search_para, std::string log_file_name_para) {
try {
std::time_t t = std::time(0);
std::tm* now = std::localtime(&t);
custom_file.open(log_file_name_para, std::ios::out | std::ios::app); //name_missing
if (custom_file.fail()) { throw ("Search write failed."); }
custom_file << (now->tm_mon + 1) << '/' << (now->tm_mday) << '/' << (now->tm_year + 1900) << "::" << (now->tm_hour);
for (int x = 0; x <= 80; ++x) { custom_file << '.'; }
search_para[0] = std::toupper(search_para[0]);
custom_file << '\n' << search_para << '?' << std::endl; //put time and dashes in another mini function before calling
custom_file.close();
}
catch (const std::exception & e) {
std::cerr << e.what() << std::endl;
}
}
/*Writes the the search result into the user file*/
void LogIO::write_queries(std::string queries_para, std::string log_file_name_para) {
try {
custom_file.open(log_file_name_para, std::ios::out | std::ios::app);
if (custom_file.fail()) { throw ("Query write failed."); }
custom_file << ' ' << ' ' << queries_para << std::endl;
custom_file << std::endl;
custom_file.close();
}
catch (const std::exception & e) {
std::cerr << e.what() << std::endl;
}
}

/*Sees if user names is in the file, if yes, return 1*/
bool LogIO::check_returned_user(std::string user_name_para) {
try {
std::ifstream name_file;
name_file.open("users.txt", std::ios::in);
if (name_file.is_open()) {

std::string line = "";
std::string dummy = "";
std::string name = "";
while (std::getline(name_file, line)) {
std::istringstream iss(line);
if (iss >> dummy >> name) { if (name == user_name_para) { return true; } }
}
name_file.close();
}
else { throw ("Name read failed."); }
}
catch (const std::exception & e) { std::cerr << e.what() << std::endl; }
return false;
}
/*istream take the input*/
std::istream& operator>> (std::istream & in, LogIO & L_IO) {

std::string local_user_name = "";
/*Draws/ slows the console output so the bar appears as if it is loading*/
std::cout << KBBLU << "|||" << KNRM << std::flush; L_IO.sleepcp(1000);
std::cout << KBBB << " " << KNRM << std::flush; L_IO.sleepcp(1000);
std::cout << KBBC << " " << KNRM << std::flush; L_IO.sleepcp(0100);
std::cout << KBBG << " " << KNRM << std::flush; L_IO.sleepcp(0100);
std::cout << KBBGR << " " << KNRM << std::flush; L_IO.sleepcp(0100);
std::cout << KBBR << " " << KNRM << std::flush; L_IO.sleepcp(0010);
std::cout << KBBY << " " << KNRM << std::flush; L_IO.sleepcp(0010);
std::cout << KBYEL << "|||" << KNRM << KNRM << std::flush << std::endl; L_IO.sleepcp(0010);

do {
try { /*Asking name with validation*/
std::cout << "\nTo begin, please type your name: ";
std::getline(in, local_user_name);
if (local_user_name.length() == 0) { throw std::invalid_argument("Type your name first."); }
else { break; }
}
catch (const std::exception & e) { in.clear(); std::cerr << e.what() << std::endl; }
} while (true);

L_IO.original_user_name = local_user_name;
L_IO.set_name(local_user_name); /*Sets the user_name to correct format*/

if (L_IO.check_returned_user(*(L_IO.user_name)) == true) { /*If the name match say Welcome*/
std::cout << "Welcome back " << local_user_name << '!' << std::endl;
L_IO.write_user_names(*(L_IO.user_name));
}
else {
L_IO.write_user_names(*(L_IO.user_name));
std::cout << "Hello! " << local_user_name << ". Glad you're here.\n";
}
return in;
}
/*ostream prints the good-bye message*/
std::ostream& operator<< (std::ostream & out, const LogIO & L_IO) {
out << KBCYN << "\nThank you, " << L_IO.original_user_name << " for using this program today!\n" << KNRM;
return out;
}
LogIO::~LogIO() { delete user_name; } //----->Do I need a copy constructor or copy assinment here???
Loading

0 comments on commit bf63e83

Please sign in to comment.