-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUbahnRami.txt
258 lines (195 loc) · 4.34 KB
/
UbahnRami.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
*-----------------------------Ubahn--------------------*
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: UBahn.cpp
* Author: Rami Chaari
*
* Created on 27. Januar 2020, 10:40
*/
#define _CRT_SECURE_NO_WARNINGS
#include "UBahn.h"
#include <iostream>
#include <time.h>
using namespace std;
UBahn::UBahn() {
}
UBahn::~UBahn() {
}
void UBahn::drive(){
cout << "U" << line << " driving" << endl;
}
void UBahn::stop() {
cout << "U" << line << " stopping" << endl;
}
void UBahn::arrive() {
time_t timer;
tm *nun;
timer = time(NULL);
nun = localtime(&timer);
cout <<"U"<< line << " arrived at " << nun->tm_mday << "/" <<nun->tm_mon +1<<"/"<<nun->tm_year + 1900 << " "<<nun->tm_hour<<":"<<nun->tm_min<<":"<<nun->tm_sec<<endl; ;
}
void UBahn::enter() {
cout << "Der Passagier steigt ein" << endl;
}
void UBahn::leave() {
cout << "Der Passagier steigt aus" << endl;
}
*------------------------- Ubahn.h -------------------------*
#ifndef UBAHN_H
#define UBAHN_H
#include "IZug.h"
class UBahn: public IZug {
private:
int line;
public:
UBahn();
~UBahn();
void drive();
void stop();
void arrive();
void enter();
void leave();
};
#endif /* UBAHN_H */
*------------------------- UBahnFSM.cpp ------------------------*
#include "UBahnFSM.h"
#include <iostream>
using namespace std;
UBahnFSM::UBahnFSM(UBahn ubahn) {
this->state = STOPPED;
this->ubahn = ubahn;
}
UBahnFSM::~UBahnFSM() {
}
void UBahnFSM::evalEvents(UBahnEvent event) {
UBahnState nextState;
switch (state) {
case STOPPED:
if (event==START_DRIVING){
nextState = DRIVING;
} else if((event == PASSENGER_ENTER) || (event == PASSENGER_LEAVE)){
nextState = STOPPED;
}
break;
case CLEAN_UP:
if (event == CLEAN_UP_DONE) {
nextState = STOPPED;
}
break;
case DRIVING:
if (event == STOP) {
nextState = STOPPED;
}
else if (event == PASSENGER_ENTER) {
nextState = CLEAN_UP;
}
else if (event == PASSENGER_LEAVE) {
nextState = ERROR;
}
break;
case ERROR:
break;
}
state = nextState;
}
void UBahnFSM::evalState(){
switch (state) {
case ERROR:
cout << "Error!" << endl;
break;
case DRIVING:
ubahn.drive();
break;
case CLEAN_UP:
cout << "cleaning up" << endl;
break;
case STOPPED:
ubahn.stop();
ubahn.arrive();
break;
}
}
UBahnState UBahnFSM::getState() {
return state;
}
*--------------------- UBahnFSM.h -------------------------*
#include "UBahn.h"
enum UBahnState {DRIVING, STOPPED, ERROR, CLEAN_UP};
enum UBahnEvent {START_DRIVING, STOP, PASSENGER_ENTER, PASSENGER_LEAVE, CLEAN_UP_DONE};
class UBahnFSM {
private:
UBahnState state;
UBahn ubahn;
public:
UBahnFSM(UBahn);
~UBahnFSM();
void evalEvents(UBahnEvent);
void evalState();
UBahnState getState();
};
*----------------------------------- Person.h ---------------*
#ifndef PERSON_H
#define PERSON_H
#include "IZug.h"
class Person {
public:
Person();
void enterTrain(IZug* zug);
void leaveTrain();
private:
IZug* zug;
};
#endif /* PERSON_H */
*----------------------- Person.cpp -----------------------*
#include "Person.h"
Person::Person() {
}
void Person::enterTrain(IZug* zug){
this->zug = zug; zug->enter();
}
void Person::leaveTrain(){
zug->leave();
}
*-------------------------- IZug --------------------------*
#ifndef IZUG_H
#define IZUG_H
enum ZugTyp {
PUBLIC,
TRANSPORT
};
class IZug {
public:
virtual void enter() = 0;
virtual void leave() = 0;
virtual ~IZug() {};
protected:
int passenger_count;
ZugTyp type;
};
#endif /* IZUG_H */
*------------------------- main von Ubahn ------------------*
/*
* File: main.cpp
* Author: Rami Chaari
*
* Created on 27. Januar 2020, 10:35
*/
#include <cstdlib>
#include "UBahn.h"
#include "IZug.h"
#include "Person.h"
using namespace std;
int main(int argc, char** argv) {
UBahn ubahn;
ubahn.arrive();
Person person;
IZug *zug;
zug = &ubahn;
person.enterTrain(zug);
person.leaveTrain();
return 0;
}