-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from jundl77/julian-hw-01
Julian hw 01 (date class)
- Loading branch information
Showing
9 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
cmake-build-debug |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
cmake_minimum_required(VERSION 3.5.1) | ||
|
||
project(cpp_class_hw1_julianbrendl LANGUAGES CXX) | ||
|
||
set(CMAKE_CXX_STANDARD 14) | ||
|
||
add_subdirectory(src) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
set(LIBRARY_NAME hw1) | ||
|
||
add_executable(${LIBRARY_NAME} date.h date.cc test.cc) | ||
|
||
target_include_directories(${LIBRARY_NAME} PUBLIC .) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#include "date.h" | ||
#include <stdexcept> | ||
|
||
namespace CppCourse { namespace Homework1 { | ||
|
||
Date::Date(int year, int month, int day) | ||
: mYear(year), | ||
mMonth(month), | ||
mDay(day) | ||
{ | ||
if (mYear < 0) | ||
throw std::runtime_error("Year is not valid."); | ||
|
||
if (mMonth < 0 || mMonth > 12) | ||
throw std::runtime_error("Month is not valid."); | ||
|
||
// Ignoring special cases like leap years, or 30 vs 31 etc. | ||
if (mDay < 0 || mDay > 31) | ||
throw std::runtime_error("Day is not valid."); | ||
} | ||
|
||
int Date::GetYear() const | ||
{ | ||
return mYear; | ||
} | ||
|
||
int Date::GetMonth() const | ||
{ | ||
return mMonth; | ||
} | ||
|
||
int Date::GetDay() const | ||
{ | ||
return mDay; | ||
} | ||
|
||
std::string Date::ToString() const | ||
{ | ||
return std::to_string(mYear) + "-" + std::to_string(mMonth) + "-" + std::to_string(mDay); | ||
} | ||
|
||
bool Date::operator== (const Date& other) const | ||
{ | ||
return mYear == other.mYear && mMonth == other.mMonth && mDay == other.mDay; | ||
} | ||
|
||
bool Date::operator!= (const Date& other) const | ||
{ | ||
return !(*this == other); | ||
} | ||
|
||
bool Date::operator< (const Date& other) const | ||
{ | ||
return *this != other && !(*this > other); | ||
} | ||
|
||
bool Date::operator> (const Date& other) const | ||
{ | ||
if (mYear == other.mYear) | ||
{ | ||
if (mMonth == other.mMonth) | ||
{ | ||
return mDay > other.mDay; | ||
} | ||
else | ||
{ | ||
return mMonth > other.mMonth; | ||
} | ||
} | ||
else | ||
{ | ||
return mYear > other.mYear; | ||
} | ||
} | ||
|
||
bool Date::operator<= (const Date& other) const | ||
{ | ||
return !(*this > other); | ||
} | ||
|
||
bool Date::operator>= (const Date& other) const | ||
{ | ||
return !(*this < other); | ||
} | ||
|
||
}}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
namespace CppCourse { namespace Homework1 { | ||
|
||
class Date | ||
{ | ||
public: | ||
Date(int year, int month, int day); | ||
|
||
int GetYear() const; | ||
int GetMonth() const; | ||
int GetDay() const; | ||
|
||
std::string ToString() const; | ||
|
||
bool operator== (const Date& other) const; | ||
bool operator!= (const Date& other) const; | ||
bool operator< (const Date& other) const; | ||
bool operator> (const Date& other) const; | ||
bool operator<= (const Date& other) const; | ||
bool operator>= (const Date& other) const; | ||
|
||
private: | ||
int mYear; | ||
int mMonth; | ||
int mDay; | ||
}; | ||
|
||
}}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
#include "date.h" | ||
#include <iostream> | ||
|
||
typedef CppCourse::Homework1::Date Date; | ||
|
||
void TestEqual(const Date& date1, const Date& date2) | ||
{ | ||
std::cout << "Test equal: "; | ||
bool result = date1 == date2; | ||
|
||
std::cout << date1.ToString() << " == " << date2.ToString() << ": " << std::to_string(result) << std::endl; | ||
} | ||
|
||
void TestNotEqual(const Date& date1, const Date& date2) | ||
{ | ||
std::cout << "Test not equal: "; | ||
bool result = date1 != date2; | ||
|
||
std::cout << date1.ToString() << " != " << date2.ToString() << ": " << std::to_string(result) << std::endl; | ||
} | ||
|
||
void TestLessThan(const Date& date1, const Date& date2) | ||
{ | ||
std::cout << "Test less than: "; | ||
bool result = date1 < date2; | ||
|
||
std::cout << date1.ToString() << " < " << date2.ToString() << ": " << std::to_string(result) << std::endl; | ||
} | ||
|
||
void TestGreaterThan(const Date& date1, const Date& date2) | ||
{ | ||
std::cout << "Test greater than: "; | ||
bool result = date1 > date2; | ||
|
||
std::cout << date1.ToString() << " > " << date2.ToString() << ": " << std::to_string(result) << std::endl; | ||
} | ||
|
||
void TestLessThanEqual(const Date& date1, const Date& date2) | ||
{ | ||
std::cout << "Test <=: "; | ||
bool result = date1 <= date2; | ||
|
||
std::cout << date1.ToString() << " <= " << date2.ToString() << ": " << std::to_string(result) << std::endl; | ||
} | ||
|
||
void TestGreaterThanEqual(const Date& date1, const Date& date2) | ||
{ | ||
std::cout << "Test >=: "; | ||
bool result = date1 >= date2; | ||
|
||
std::cout << date1.ToString() << " >= " << date2.ToString() << ": " << std::to_string(result) << std::endl; | ||
} | ||
|
||
void TestCompareDates(const Date& base, const Date& lessThanBase, const Date& greaterThanBase) | ||
{ | ||
TestEqual(base, base); | ||
TestEqual(base, lessThanBase); | ||
TestEqual(base, greaterThanBase); | ||
|
||
std::cout << std::endl; | ||
|
||
TestNotEqual(base, base); | ||
TestNotEqual(base, lessThanBase); | ||
TestNotEqual(base, greaterThanBase); | ||
|
||
std::cout << std::endl; | ||
|
||
TestLessThan(base, base); | ||
TestLessThan(base, lessThanBase); | ||
TestLessThan(base, greaterThanBase); | ||
|
||
std::cout << std::endl; | ||
|
||
TestGreaterThan(base, base); | ||
TestGreaterThan(base, lessThanBase); | ||
TestGreaterThan(base, greaterThanBase); | ||
|
||
std::cout << std::endl; | ||
|
||
TestLessThanEqual(base, base); | ||
TestLessThanEqual(base, lessThanBase); | ||
TestLessThanEqual(base, greaterThanBase); | ||
|
||
std::cout << std::endl; | ||
|
||
TestGreaterThanEqual(base, base); | ||
TestGreaterThanEqual(base, lessThanBase); | ||
TestGreaterThanEqual(base, greaterThanBase); | ||
} | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
std::cout << "Starting tests" << std::endl; | ||
|
||
Date baseDay {2019, 01, 02}; | ||
Date lessThanBaseDay {2019, 01, 01}; | ||
Date greaterThanBaseDay {2019, 01, 03}; | ||
|
||
Date baseMonth {2019, 02, 01}; | ||
Date lessThanBaseMonth {2019, 01, 01}; | ||
Date greaterThanBaseMonth {2019, 03, 01}; | ||
|
||
Date baseDayYear {2020, 01, 01}; | ||
Date lessThanBaseDayYear {2019, 01, 01}; | ||
Date greaterThanBaseDayYear {2021, 01, 01}; | ||
|
||
std::cout << std::endl << "COMPARE DATES DAY" << std::endl; | ||
TestCompareDates(baseDay, lessThanBaseDay, greaterThanBaseDay); | ||
std::cout << std::endl << "COMPARE DATES MONTH" << std::endl; | ||
TestCompareDates(baseMonth, lessThanBaseMonth, greaterThanBaseMonth); | ||
std::cout << std::endl << "COMPARE DATES YEAR" << std::endl; | ||
TestCompareDates(baseDayYear, lessThanBaseDayYear, greaterThanBaseDayYear); | ||
} |