This repository has been archived by the owner on Jul 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Event.cpp
148 lines (125 loc) · 2.35 KB
/
Event.cpp
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
/* Nama File : Event.cpp */
/* NIM - Nama : 13512003 - Fauzan Hilmi Ramadhian */
/* : 13512019 - Christ Angga Saputra */
#include "Event.h"
#include <cstdlib>
Event::Event() {}
Event::Event(DateTime TMax, int teller) {
Q = new Queue[teller];
this->TMax = TMax;
this->teller = teller;
customer = 0;
}
Event::~Event() {
delete[] Q;
}
ostream& operator<<(ostream& os, Event& E) {
bool enter = false;
for (int i=0; i<E.getTeller(); ++i) {
if (E.getQueue(i).size() > 0) {
if (enter) {
os << endl;
}
os << "Q[" << i << "] = {" << E.getQueue(i) << "}";
enter = true;
}
}
return os;
}
Queue Event::getQueue(int i) {
return Q[i];
}
DateTime Event::getTMax() {
return TMax;
}
int Event::getCustomer() {
return customer;
}
int Event::getTeller() {
return teller;
}
void Event::setTMax(DateTime TMax) {
this->TMax = TMax;
}
void Event::setTeller(int teller) {
this->teller = teller;
}
int Event::jockeying(int iOrigin) {
int min = 0;
for (int i=1; i<teller; ++i) {
if (Q[i].size() < Q[min].size()) {
min = i;
}
}
if (abs(Q[iOrigin].size() - Q[min].size()) <= 2) {
return -1;
}
list<int> L;
for (int i=0; i<teller; ++i) {
if (Q[i].size() == Q[min].size() && i != iOrigin) {
L.push_back(i);
}
}
min = L.front();
for (list<int>::iterator it=L.begin(); it!=L.end(); ++it) {
if (it != L.begin() && abs(iOrigin - *it) < abs(iOrigin - min)) {
min = *it;
}
}
return min;
}
void Event::process(DateTime DT, char code) {
int n;
if (code == 'D') {
cin >> n;
}
if (DT.compare(TMax) <= 0) {
if (code == 'A') {
arrival();
} else if (code =='D') {
departure(n);
}
}
}
void Event::arrival() {
int min = 0;
for (int i=1; i<teller; ++i) {
if (Q[i].size() < Q[min].size()) {
min = i;
}
}
++customer;
Q[min].add(customer);
}
void Event::departure(int n) {
for (int i=0; i<teller; ++i) {
if (Q[i].getHead() == n) {
Q[i].del();
cout << "masuk"<< endl;
int jockey = jockeying(i);
cout << "jockey " << jockey << endl;
if (jockey >= 0) {
Q[jockey].add(Q[i].move());
}
break;
}
}
}
void Event::closing() {
while (!isEmpty()) {
for (int i=0; i<teller; ++i) {
if (!Q[i].isEmpty()) {
cout << "Departure " << Q[i].getHead() << endl;
Q[i].del();
}
}
}
}
bool Event::isEmpty() {
for (int i=0; i<teller; ++i) {
if (!Q[i].isEmpty()) {
return false;
}
}
return true;
}