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 {
 53     ostringstream strOutput;
 54
 55     strOutput << setw(2) << setfill('0') << m_nDay << "."
 56               << setw(2) << setfill('0') << m_nMonth << "."
 57               << m_nYear << " - "
 58               << setw(2) << setfill('0') << m_nHour<< ":"
 59               << setw(2) << setfill('0') << m_nMinute << ":"
 60               << setw(2) << setfill('0') << m_nSecond;
 61
 62     return strOutput.str();
 63 }
 64
 65
 66 // shows all internal attributes
 67 string CDate::ShowDebug() const
 68 {
 69     ostringstream strOutput;
 70
 71     strOutput << "DEBUG info for 'CDate'" << endl
 72               << " m_nDay = " << m_nDay    << endl
 73               << " m_nMonth = " << m_nMonth  << endl
 74               << " m_nYear = " << m_nYear   << endl
 75               << " m_nHour = " << m_nHour   << endl
 76               << " m_nMinute = " << m_nMinute << endl
 77               << " m_nSecond = " << m_nSecond << endl;
 78
 79     return strOutput.str();
 80 }
 81
 82
 83 // return current date
 84 void CDate::GetToday(unsigned int &nDay,  unsigned int &nMonth,  unsigned int &nYear,
 85                      unsigned int &nHour, unsigned int &nMinute, unsigned int &nSecond)

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