sources:
BasicClass.h (1.4k)
CashOffice.cpp (3.6k)
CashOffice.h (1.7k)
Date.cpp (6.0k)
Date.h (2.9k)
Exception.cpp (2.5k)
Exception.h (1.4k)
House.cpp (7.5k)
House.h (2.5k)
O3_5.cpp (577 bytes)
Room.cpp (2.9k)
Room.h (1.5k)


binaries:
Release/O3_5.exe (132.0k)


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

  1 ///////////////////////////////////////////////////////////
  2 // Softwarebauelemente II, Aufgabe O3.2
  3 //
  4 // author: Stephan Brumme
  5 // last changes: May 30, 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     // check invariance
 54     if (!ClassInvariant())
 55         return "";
 56
 57     ostringstream strOutput;
 58
 59     strOutput << setw(2) << setfill('0') << m_nDay << "."
 60               << setw(2) << setfill('0') << m_nMonth << "."
 61               << m_nYear << " - "
 62               << setw(2) << setfill('0') << m_nHour<< ":"
 63               << setw(2) << setfill('0') << m_nMinute << ":"
 64               << setw(2) << setfill('0') << m_nSecond;
 65
 66     return strOutput.str();
 67 }
 68
 69
 70 // shows all internal attributes
 71 string CDate::ShowDebug() const
 72 {
 73     ostringstream strOutput;
 74
 75     strOutput << "DEBUG info for 'CDate'" << endl
 76               << " m_nDay = " << m_nDay    << endl
 77               << " m_nMonth = " << m_nMonth  << endl
 78               << " m_nYear = " << m_nYear   << endl
 79               << " m_nHour = " << m_nHour   << endl
 80               << " m_nMinute = " << m_nMinute << endl
 81               << " m_nSecond = " << m_nSecond << endl;
 82
 83     return strOutput.str();
 84 }
 85
 86
 87 // return current date
 88 void CDate::GetToday(unsigned int &nDay,  unsigned int &nMonth,  unsigned int &nYear,
 89                      unsigned int &nHour, unsigned int &nMinute, unsigned int &nSecond)

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