sources:
BasicClass.h (1.4k)
CashOffice.cpp (3.7k)
CashOffice.h (1.7k)
Date.cpp (6.1k)
Date.h (2.9k)
House.cpp (6.1k)
House.h (2.3k)
O3_1.cpp (737 bytes)
Room.cpp (2.9k)
Room.h (1.5k)


binaries:
Release/O3_1.exe (124.0k)


website:
more info here
studies/bauelemente/Softwarebauelemente-CodeO3-1/Date.cpp
download file

  1 ///////////////////////////////////////////////////////////
  2 // Softwarebauelemente II, Aufgabe O3.1
  3 //
  4 // author: Stephan Brumme
  5 // last changes: May 15, 2001
  6
  7
  8 #include "Date.h"
  9
 10
 11 // we are using OS-specific date/time operations
 12 #include <time.h>
 13 #include <iomanip>
 14 #include <iostream>
 15 using namespace std;
 16
 17
 18
 19
 20 // constructor, default value is current date and time
 21 CDate::CDate()
 22 {
 23     GetToday(m_nDay, m_nMonth, m_nYear, m_nHour, m_nMinute, m_nSecond);
 24 }
 25
 26
 27 // copy constructor
 28 CDate::CDate(const CDate &date)
 29 {
 30     operator=(date);
 31 }
 32
 33
 34 // set user defined date at construction time
 35 CDate::CDate(unsigned int nDay, unsigned int nMonth, unsigned int nYear,
 36              unsigned int nHour, unsigned int nMinute, unsigned int nSecond)

 37 {
 38     // store date
 39     m_nDay    = nDay;
 40     m_nMonth  = nMonth;
 41     m_nYear   = nYear;
 42
 43     // store time
 44     m_nHour   = nHour;
 45     m_nMinute = nMinute;
 46     m_nSecond = nSecond;
 47 }
 48
 49
 50 // show attributes
 51 string CDate::Show() const {
 52     ostringstream strOutput;
 53
 54     strOutput << setw(2) << setfill('0') << m_nDay << "."
 55               << setw(2) << setfill('0') << m_nMonth << "."
 56               << m_nYear << " - "
 57               << setw(2) << setfill('0') << m_nHour<< ":"
 58               << setw(2) << setfill('0') << m_nMinute << ":"
 59               << setw(2) << setfill('0') << m_nSecond;
 60
 61     return strOutput.str();
 62 }
 63
 64
 65 // shows all internal attributes
 66 string CDate::ShowDebug() const {
 67     ostringstream strOutput;
 68
 69     strOutput << "DEBUG info for 'CDate'" << endl
 70               << " m_nDay = " << m_nDay    << endl
 71               << " m_nMonth = " << m_nMonth  << endl
 72               << " m_nYear = " << m_nYear   << endl
 73               << " m_nHour = " << m_nHour   << endl
 74               << " m_nMinute = " << m_nMinute << endl
 75               << " m_nSecond = " << m_nSecond << endl;
 76
 77     return strOutput.str();
 78 }
 79
 80
 81 // return current date
 82 void CDate::GetToday(unsigned int &nDay,  unsigned int &nMonth,  unsigned int &nYear,
 83                      unsigned int &nHour, unsigned int &nMinute, unsigned int &nSecond)

 84 {
 85
 86     // the following code is basically taken from MSDN
 87
 88     // use system functions to get the current date as UTC
 89     time_t secondsSince1970;
 90     time(&secondsSince1970);
 91
 92     // convert UTC to local time zone
 93     struct tm *localTime;
 94     localTime = localtime(&secondsSince1970);
 95
 96     // store retrieved date
 97     nDay    = localTime->tm_mday;
 98     nMonth  = localTime->tm_mon + 1;
 99     nYear   = localTime->tm_year + 1900;
100     // store time
101     nHour   = localTime->tm_hour;
102     nMinute = localTime->tm_min;
103     nSecond = localTime->tm_sec;
104 }
105
106
107 // validate a date
108 bool CDate::ClassInvariant() const {
109     // validate month
110     if (m_nMonth < JANUARY || m_nMonth > DECEMBER)
111         return false;
112
113     // days per month, february may vary, note that array starts with 0, not JANUARY
114     unsigned int nDaysPerMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
115
116     // adjust days of february
117     if (IsLeapYear(m_nYear))
118         nDaysPerMonth[FEBRUARY]++;
119
120     // validate day
121     if (m_nDay < 1 || m_nDay > nDaysPerMonth[m_nMonth])
122         return false;
123     // day and month are valid
124
125     // now check the time
126     if (m_nHour < 0 || m_nHour > 23)
127         return false;
128     if (m_nMinute < 0 || m_nMinute > 59)
129         return false;
130     if (m_nSecond < 0 || m_nSecond > 59)
131         return false;
132
133     // instance must be valid now
134     return true;
135 }
136
137
138 // determine whether it is a leap year
139 bool CDate::IsLeapYear(unsigned int nYear)
140 {
141     // used to speed up code, may be deleted
142     if (nYear % 4   != 0)
143         return false;
144
145     // algorithm taken from MSDN, just converted from VBA to C++
146     if (nYear % 400 == 0)
147         return true;
148     if (nYear % 100 == 0)
149         return false;
150     if (nYear % 4   == 0)
151         return true;
152
153     // this line won't be executed because of optimization (see above)
154     return false;
155 }
156
157
158 // copy constructor
159 CDate& CDate::operator =(const CDate &date)
160 {
161     // date
162     m_nDay    = date.m_nDay;
163     m_nMonth  = date.m_nMonth;
164     m_nYear   = date.m_nYear;
165     // time
166     m_nHour   = date.m_nHour;
167     m_nMinute = date.m_nMinute;
168     m_nSecond = date.m_nSecond;
169
170     return *this;
171 }
172
173
174 // virtual, see operator=
175 bool CDate::Copy(const CBasicClass *pClass)
176 {
177     // cast to CDate
178     CBasicClass *basic;
179     CDate *date;
180     basic = const_cast<CBasicClass*>(pClass);
181     date = dynamic_cast<CDate*>(basic);
182
183     // invalid class (is NULL when pClass is not a CDate)
184     if (date == NULL || date == this)
185         return false;
186
187     // use non virtual reference based copy
188     // return value isn't needed
189     operator=(*date);
190
191     // we're done
192     return true;
193 }
194
195
196 // compare two dates
197 bool CDate::operator ==(const CDate &date) const {
198     // compare date and time attributes
199     return (m_nDay    == date.m_nDay    &&
200             m_nMonth  == date.m_nMonth  &&
201             m_nYear   == date.m_nYear   &&
202             m_nHour   == date.m_nHour   &&
203             m_nMinute == date.m_nMinute &&
204             m_nSecond == date.m_nSecond)
;
205 }
206
207
208 // virtual, see operator==
209 bool CDate::EqualValue(const CBasicClass *pClass) const {
210     // cast to CDate
211     CBasicClass *basic;
212     CDate *date;
213     basic = const_cast<CBasicClass*>(pClass);
214     date = dynamic_cast<CDate*>(basic);
215
216     // invalid class (is NULL when pClass is not a CDate)
217     if (date == NULL)
218         return false;
219
220     // use non virtual reference based copy
221     return operator==(*date);
222 }
223
224
225 // set attributes, returns validility of date
226 void CDate::SetDay(unsigned int nDay)
227 {
228     m_nDay = nDay;
229 }
230 void CDate::SetMonth(unsigned int nMonth)
231 {
232     m_nMonth = nMonth;
233 }
234 void CDate::SetYear(unsigned int nYear)
235 {
236     m_nYear = nYear;
237 }
238 void CDate::SetHour(unsigned int nHour)
239 {
240     m_nHour = nHour;
241 }
242 void CDate::SetMinute(unsigned int nMinute)
243 {
244     m_nMinute = nMinute;
245 }
246 void CDate::SetSecond(unsigned int nSecond)
247 {
248     m_nSecond = nSecond;
249 }
250
251 // return attributes
252 unsigned int CDate::GetDay() const {
253     return m_nDay;
254 }
255 unsigned int CDate::GetMonth() const {
256     return m_nMonth;
257 }
258 unsigned int CDate::GetYear() const {
259     return m_nYear;
260 }
261 unsigned int CDate::GetHour() const {
262     return m_nHour;
263 }
264 unsigned int CDate::GetMinute() const {
265     return m_nMinute;
266 }
267 unsigned int CDate::GetSecond() const {
268     return m_nSecond;
269 }
270
271