sources:
BasicClass.cpp (550 bytes)
BasicClass.h (1.4k)
Date.cpp (3.8k)
Date.h (2.2k)
House.cpp (4.9k)
House.h (2.4k)
O2_1.cpp (619 bytes)
Room.cpp (2.3k)
Room.h (1.4k)


website:
more info here
studies/bauelemente/Softwarebauelemente-CodeO2-1/House.cpp
download file

  1 ///////////////////////////////////////////////////////////
  2 // Softwarebauelemente II, Aufgabe O2.1
  3 //
  4 // author: Stephan Brumme
  5 // last changes: February 26, 2001
  6
  7
  8 #include "House.h"
  9
 10 // needed for std::find
 11 #include <algorithm>
 12 #include <strstream>
 13
 14
 15 // constructs a new house using its date of foundation (default: today)
 16 CHouse::CHouse(const CDate& dtDateOfFoundation)
 17 {
 18     m_dtDateOfFoundation = dtDateOfFoundation;
 19     m_Set.empty();
 20     m_itCursor = m_Set.begin();
 21 }
 22
 23
 24 ostream& operator<<(ostream& mystream, const CHouse& house)
 25 {
 26     return house.OutStream(mystream);
 27 }
 28
 29
 30 // used to perform proper display in derived classes
 31 ostream& CHouse::OutStream(ostream& mystream, bool bDebug) const
 32 {
 33     if (!bDebug)
 34     {
 35         // some general information
 36         mystream << "The house was founded on " << m_dtDateOfFoundation
 37                  << " and consists of "
 38                  << m_Set.size() << " rooms"  << endl;
 39     }
 40     else
 41     {
 42         // print any information about this class
 43         mystream << "DEBUG info for '" << ClassnameOf() << "'" << endl
 44                  << " m_dtDateOfFoundation=" << m_dtDateOfFoundation
 45                   << " rooms="   << m_Set.size() << endl;
 46     }
 47
 48     // stream all rooms
 49     if (m_Set.size() > 0)
 50     {
 51         // this iterator needs to be const ...
 52         TSetConstCursor itCursor;
 53         for (itCursor = m_Set.begin(); itCursor != m_Set.end(); itCursor++)
 54             mystream << " " << *itCursor << endl;
 55     }
 56
 57     return mystream;
 58 }
 59
 60
 61 // copy constructor
 62 CHouse& CHouse::operator =(const CHouse &house)
 63 {
 64     m_dtDateOfFoundation = house.m_dtDateOfFoundation;
 65     m_itCursor = house.m_itCursor;
 66     // container performs the whole copy internally
 67     m_Set      = house.m_Set;
 68
 69     return *this;
 70 }
 71
 72
 73 // virtual, see operator=
 74 bool CHouse::Copy(const CHouse *house)
 75 {
 76     // invalid class
 77     if (house == NULL || house == this)
 78         return false;
 79
 80     // use non virtual reference based copy
 81     // return value isn't needed
 82     operator=(*house);
 83
 84     // we're done
 85     return true;
 86 }
 87
 88
 89 // compare two houses
 90 bool CHouse::operator ==(const CHouse &house) const
 91 {
 92     // compare date of foundation
 93     if (!(m_dtDateOfFoundation == house.m_dtDateOfFoundation))
 94         return false;
 95
 96     // find each room of the given house in our house
 97     TSetConstCursor itCursor;
 98     for (itCursor = house.m_Set.begin(); itCursor != house.m_Set.end(); itCursor++)
 99         // I re-implement the find method because of keeping this method const
100         if (std::find(m_Set.begin(), m_Set.end(), *itCursor) == m_Set.end())
101             return false;
102
103     // all rooms were found
104     return true;
105 }
106
107
108 // virtual, see operator==
109 bool CHouse::EqualValue(const CHouse *house) const
110 {
111     // invalid class
112     if (house == NULL)
113         return false;
114
115     // use non virtual reference based copy
116     return operator==(*house);
117 }
118
119
120 // virtual, just compares the pointers
121 bool CHouse::Equal(const CHouse *house) const
122 {
123     return (house == this);
124 }
125
126
127 // return date of foundation
128 CDate CHouse::GetDateOfFoundation() const
129 {
130     return m_dtDateOfFoundation;
131 }
132
133
134 // return number of stored rooms
135 int CHouse::Card() const
136 {
137     return m_Set.size();
138 }
139
140
141 // insert a new room into the set, TRUE if successful
142 // will fail if room already exists
143 bool CHouse::Insert(const CRoom &room)
144 {
145     // there must be some memory to grow the set
146     // and the room must not be part of the list
147     if (m_Set.size() < m_Set.max_size() && !Find(room))
148     {
149         // insert at the tail of the list
150         m_Set.push_back(room);
151         return true;
152     }
153     else
154         return false;
155 }
156
157
158 // return first stored room, TRUE if successful
159 bool CHouse::GetFirst(CRoom &room)
160 {
161     // set must not be empty
162     if (m_Set.empty())
163         return false;
164
165     // set cursor to first room
166     m_itCursor = m_Set.begin();
167     // get this room
168     room = *m_itCursor;
169
170     return true;
171 }
172
173
174 // return next room, TRUE if successful
175 bool CHouse::GetNext(CRoom &room)
176 {
177     // set must not be empty, end must not be reached
178     if (m_Set.empty() && m_itCursor != m_Set.end())
179         return false;
180    
181     // iterate to next object
182     m_itCursor++;
183     // get this object
184     room = *m_itCursor;
185
186     return true;
187 }
188
189
190 // look for a room and set cursor, TRUE if successful
191 bool CHouse::Find(const CRoom &room)
192 {
193     // set must not be empty
194     if (m_Set.empty())
195         return false;
196
197     // create new iterator
198     TSetCursor itCursor;
199
200     // use STL's find
201     itCursor = std::find(m_Set.begin(), m_Set.end(), room);
202
203     // change m_itCursor if room was found
204     if (itCursor != m_Set.end())
205     {
206         m_itCursor = itCursor;
207         return true;
208     }
209     else
210         return false;
211 }
212
213
214 // return the room the cursor points to, TRUE if successful
215 bool CHouse::GetCurrent(CRoom &room) const
216 {
217     // set must not be empty
218     if (m_Set.empty())
219         return false;
220
221     // return current room
222     room = *m_itCursor;
223     return true;
224 }
225
226
227 // erase current room, TRUE if successful
228 bool CHouse::Scratch()
229 {
230     // set must not be empty
231     if (m_Set.empty())
232         return false;
233
234     // erase room, set cursor to next room
235     m_itCursor = m_Set.erase(m_itCursor);
236     return true;
237 }
238