File tree 7 files changed +165
-0
lines changed
7 files changed +165
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include " Apartment.h"
2
+ #include < iostream>
3
+
4
+ void Apartment::addRoom (const Room& room) {
5
+ rooms.push_back (room);
6
+ }
7
+
8
+ void Apartment::lockAll () {
9
+ for (auto & room : rooms) {
10
+ for (auto & lock : room.getLocks ()) {
11
+ lock.lock ();
12
+ }
13
+ }
14
+ }
15
+
16
+ void Apartment::unlockAll () {
17
+ for (auto & room : rooms) {
18
+ for (auto & lock : room.getLocks ()) {
19
+ lock.unlock ();
20
+ }
21
+ }
22
+ }
23
+
24
+ void Apartment::generateStatistics () const {
25
+ for (const auto & room : rooms) {
26
+ std::cout << " Room: " << room.getName () << std::endl;
27
+ std::cout << " Entries: " << room.getEntryCount () << std::endl;
28
+ std::cout << " Exits: " << room.getExitCount () << std::endl;
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ #ifndef APARTMENT_H
2
+ #define APARTMENT_H
3
+
4
+ #include < vector>
5
+ #include " Room.h"
6
+
7
+ class Apartment {
8
+ public:
9
+ void addRoom (const Room& room);
10
+ void lockAll ();
11
+ void unlockAll ();
12
+ void generateStatistics () const ;
13
+
14
+ private:
15
+ std::vector<Room> rooms;
16
+ };
17
+
18
+ #endif // APARTMENT_H
Original file line number Diff line number Diff line change
1
+ #include " Lock.h"
2
+
3
+ Lock::Lock (const std::string& id) : id(id), locked(true ) {}
4
+
5
+ void Lock::lock () {
6
+ locked = true ;
7
+ }
8
+
9
+ void Lock::unlock () {
10
+ locked = false ;
11
+ }
12
+
13
+ bool Lock::isLocked () const {
14
+ return locked;
15
+ }
16
+
17
+ std::string Lock::getId () const {
18
+ return id;
19
+ }
Original file line number Diff line number Diff line change
1
+ #ifndef LOCK_H
2
+ #define LOCK_H
3
+
4
+ #include < string>
5
+
6
+ class Lock {
7
+ public:
8
+ Lock (const std::string& id);
9
+ void lock ();
10
+ void unlock ();
11
+ bool isLocked () const ;
12
+ std::string getId () const ;
13
+
14
+ private:
15
+ std::string id;
16
+ bool locked;
17
+ };
18
+
19
+ #endif // LOCK_H
Original file line number Diff line number Diff line change
1
+ #include " Room.h"
2
+
3
+ Room::Room (const std::string& name) : name(name), entryCount(0 ), exitCount(0 ) {}
4
+
5
+ void Room::addLock (const Lock& lock) {
6
+ locks.push_back (lock);
7
+ }
8
+
9
+ void Room::enter () {
10
+ entryCount++;
11
+ }
12
+
13
+ void Room::exit () {
14
+ exitCount++;
15
+ }
16
+
17
+ int Room::getEntryCount () const {
18
+ return entryCount;
19
+ }
20
+
21
+ int Room::getExitCount () const {
22
+ return exitCount;
23
+ }
24
+
25
+ std::string Room::getName () const {
26
+ return name;
27
+ }
Original file line number Diff line number Diff line change
1
+ #ifndef ROOM_H
2
+ #define ROOM_H
3
+
4
+ #include < string>
5
+ #include < vector>
6
+ #include " Lock.h"
7
+
8
+ class Room {
9
+ public:
10
+ Room (const std::string& name);
11
+ void addLock (const Lock& lock);
12
+ void enter ();
13
+ void exit ();
14
+ int getEntryCount () const ;
15
+ int getExitCount () const ;
16
+ std::string getName () const ;
17
+
18
+ private:
19
+ std::string name;
20
+ std::vector<Lock> locks;
21
+ int entryCount;
22
+ int exitCount;
23
+ };
24
+
25
+ #endif // ROOM_H
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include " Apartment.h"
3
+
4
+ int main () {
5
+ Apartment apartment;
6
+
7
+ Room livingRoom (" Living Room" );
8
+ livingRoom.addLock (Lock (" LivingRoomLock1" ));
9
+ livingRoom.addLock (Lock (" LivingRoomLock2" ));
10
+
11
+ Room bedroom (" Bedroom" );
12
+ bedroom.addLock (Lock (" BedroomLock1" ));
13
+
14
+ apartment.addRoom (livingRoom);
15
+ apartment.addRoom (bedroom);
16
+
17
+ livingRoom.enter ();
18
+ livingRoom.exit ();
19
+ bedroom.enter ();
20
+
21
+ apartment.generateStatistics ();
22
+
23
+ apartment.lockAll ();
24
+ apartment.unlockAll ();
25
+
26
+ return 0 ;
27
+ }
You can’t perform that action at this time.
0 commit comments