Cant figure out error in Bank Simulation logic!

My problem is that when I run the simulation for the bank it keeps removing the wrong customers from the Queues at incorrect departure times. Also when it reaches the closing time I receive a nulltpr error when trying to finish departure events and customers left in the queue and list.


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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include "Bank.h"

Bank::Bank()
{
	totalCustomers = 0;
	totalTime = 0;
	currentTime = 0;
	closingTime = 60 * 60;//(60 * 60 * 8);
	list = new EventList();
	win1 = new Queue();
	win2 = new Queue();
	win3 = new Queue();
	win4 = new Queue();
}

Bank::Bank(Event* event)
{
	totalCustomers = 0;
	totalTime = 0;
	currentTime = event->occurTime;
	closingTime = (60 * 60 * 8);
	list = new EventList();
	list->insertEnd(event);
	win1 = new Queue();
	win2 = new Queue();
	win3 = new Queue();
	win4 = new Queue();
}

Bank::~Bank()
{
	win1->clear();
	win2->clear();
	win3->clear();
	win4->clear();
	list->clearList();
}

int Bank::avgTime()
{
	int avg = totalTime / totalCustomers;
	return avg;
}

int Bank::genService()
{
	srand(time(NULL));
	int time = rand() % 20 + 1;
	time *= 60;
	return time;
}

int Bank::genArrival(Customer* customer)
{
	srand(time(NULL));
	int time = rand() % 5 + 1;
	time *= 60;
	time += customer->arrivalTime;
	return time;
}

int Bank::departTime(Customer* tmp)
{
	int depart;
	depart = tmp->arrivalTime + tmp->serviceTime;
	return depart;
}

bool Bank::emptyWindows()
{
	if (win1->isEmpty() || win2->isEmpty() || win3->isEmpty() || win4->isEmpty())
		return true;
	return false;
}

int Bank::checkType(int eventType)
{
	switch (eventType)
	{
		case 1: return 1; break;
		case 2: return 2; break;
		case 3: return 3; break;
		case 4: return 4; break;
		default: return 0; break;
	}
}

void Bank::customerArrival(Event* event)
{
	Customer* customer = new Customer;
	customer->arrivalTime = event->occurTime;
	int time = genService();
	customer->serviceTime = time;
	customer->nextCustomer = NULL;
	int nxtArrival = genArrival(customer);

	if (nxtArrival < closingTime)
	{
		Event* arriveEvent = new Event;
		arriveEvent->occurTime = nxtArrival;
		arriveEvent->type = 0;
		arriveEvent->next = NULL;

		Event* p = list->getFront();
		if (list->isEmpty())
			list->frontInsert(arriveEvent);
		else {
			while (p->occurTime < nxtArrival && p->next != NULL)
				p = p->next;
			if (nxtArrival > p->occurTime)
				list->insertEnd(arriveEvent);
			else
				list->midInsert(arriveEvent, p);
		}
	}

	int num1, num2, num3, num4;
	if (!emptyWindows())
	{
		num1 = win1->getSize();
		num2 = win2->getSize();
		num3 = win3->getSize();
		num4 = win4->getSize();

		if (num1 < num2 && num1 < num3 && num1 < num4)
			win1->enQueue(customer);
		else if (num2 < num3 && num2 < num4)
			win2->enQueue(customer);
		else if (num3 < num4)
			win3->enQueue(customer);
		else
			win4->enQueue(customer);
	}
	if (win1->isEmpty())
	{
		Event* depart = new Event;
		depart->occurTime = customer->arrivalTime + customer->serviceTime;
		depart->type = 1;
		depart->next = NULL;
		win1->enQueue(customer);
		list->insertEnd(depart);
		list->sort(list->getFront());
	}
	else if (win2->isEmpty())
	{
		Event* depart = new Event;
		depart->occurTime = customer->arrivalTime + customer->serviceTime;
		depart->type = 2;
		depart->next = NULL;
		win2->enQueue(customer);
		list->insertEnd(depart);
		list->sort(list->getFront());
	}
	else if (win3->isEmpty())
	{
		Event* depart = new Event;
		depart->occurTime = departTime(customer);
		depart->type = 3;
		depart->next = NULL;
		win3->enQueue(customer);
		list->insertEnd(depart);
		list->sort(list->getFront());
	}
	else if (win4->isEmpty())
	{
		Event* depart = new Event;
		depart->occurTime = departTime(customer);
		depart->type = 4;
		depart->next = NULL;
		win4->enQueue(customer);
		list->insertEnd(depart);
		list->sort(list->getFront());
	}
}

int Bank::customerDepart(Event* event)
{
	int tmpType = checkType(event->type);
	Customer* nxt;
	int stayingTime = 0;

	if (tmpType == 1)
	{
		Customer* tmp = win1->getHead();
		stayingTime += event->occurTime - tmp->arrivalTime;
		win1->dlQueue();

		if (!win1->isEmpty())
		{
			nxt = win1->getHead();
			Event* depart = new Event;
			depart->occurTime = nxt->arrivalTime + nxt->serviceTime;
			depart->type = 1;
			depart->next = NULL;
		}
	}
	else if (tmpType == 2)
	{
		Customer* tmp = win2->getHead();
		stayingTime += event->occurTime - tmp->arrivalTime;
		win2->dlQueue();

		if (!win2->isEmpty())
		{
			nxt = win2->getHead();
			Event* depart = new Event;
			depart->occurTime = nxt->arrivalTime + nxt->serviceTime;
			depart->type = 2;
			depart->next = NULL;
		}
	}
	else if (tmpType == 3)
	{
		Customer* tmp = win3->getHead();
		stayingTime += event->occurTime - tmp->arrivalTime;
		win3->dlQueue();

		if (!win3->isEmpty())
		{
			nxt = win3->getHead();
			Event* depart = new Event;
			depart->occurTime = nxt->arrivalTime + nxt->serviceTime;
			depart->type = 3;
			depart->next = NULL;
		}
	}
	else if (tmpType == 4)
	{
		Customer* tmp = win4->getHead();
		stayingTime += event->occurTime - tmp->arrivalTime;
		win4->dlQueue();

		if (!win4->isEmpty())
		{
			nxt = win4->getHead();
			Event* depart = new Event;
			depart->occurTime = nxt->arrivalTime + nxt->serviceTime;
			depart->type = 4;
			depart->next = NULL;
		}
	}
	return stayingTime;
}

void Bank::runSimulation() //caught in endless loop
{
	if (currentTime == 0)
	{
		list->addNode(currentTime, 0);
	}
	Event* currentEvent;
	while (!list->isEmpty())
	{
		currentEvent = list->removeEvent();
		
		if (currentEvent->type == 0)
		{
			customerArrival(currentEvent);
			currentTime = currentEvent->occurTime;
		}
		else
		{
			customerDepart(currentEvent);
			currentTime = currentEvent->occurTime;
		}
		//currentEvent = currentEvent->next;
	}
	int avg = avgTime();
	printResults(avg);
}

void Bank::printResults(int avg)
{
	cout << "Total Customers:" << totalCustomers << endl;
	cout << "Total Time:" << totalTime << endl;
	cout << "Avg Staying Time:" << avg << endl;
}

#include "Queue.h"

Queue::Queue()
{
	front = nullptr;
	last = nullptr;
	numOfCustomers = 0;
}

Queue::~Queue()
{
	Customer* current = front;
	Customer* tmp;

	while (current != NULL)
	{
		tmp = current;
		current = current->nextCustomer;
		delete tmp;
	}
	numOfCustomers = 0;
}

bool Queue::isEmpty()
{
	if (front == NULL)
		return true;
	return false;
}

int Queue::getSize()
{
	return numOfCustomers;
}

Customer* Queue::getHead()
{
	return front;
}

const void Queue::print(Customer* customer)
{
	Customer* p = customer;
	if (p == NULL)
		cout << "NULL" << endl;
	else
	{
		cout << "Customer:" << p->arrivalTime << p->serviceTime << endl;
		print(p->nextCustomer);
	}
}

void Queue::clear()
{
	if (isEmpty())
		cout << "Empty window" << endl;
	Customer* del = front;
	Customer* p = front;

	while (p != last)
	{
		p = p->nextCustomer;
		delete del;
		del = p;
	}
}
void Queue::enQueue(Customer* customer)
{
	if (isEmpty())
	{
		front = customer;
		last = customer;
	}
	else
	{
		last->nextCustomer = customer;
		last = customer;
	}
	numOfCustomers++;
}

void Queue::dlQueue()
{
	if (isEmpty())
		cout << "cannot remove from an empty queue" << endl;
	else
	{
		Customer* tmp = front->nextCustomer;
		delete front;
		if (tmp == NULL)
			front = last = NULL;
		else
			front = tmp;
		numOfCustomers--;
	}
}


#
Last edited on
Here is my Event list class too



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
#include "EventList.h"
#include <stdlib.h>
#include <iostream>

using namespace std;

EventList::EventList()
{
	front = nullptr;
	last = nullptr;
	numOfEvents = 0;
}

EventList::~EventList()
{
	Event* current = front;
	Event* tmp;
	numOfEvents = 0;

	while (current != NULL)
	{
		tmp = current;
		current = current->next;
		delete tmp;
	}
}

	Event* EventList::getFront()
	{
		return front;
	}
	//checks to see if the list is empty. returns true if empty false if not
	bool EventList::isEmpty()
	{
		if (front == NULL)
			return true;
		return false;
	}

	Event* EventList::removeEvent()
	{
		if (isEmpty())
			cout << "Cannot remove event from empty list" << endl;
		Event* tmp = front;
		if (tmp->next != NULL)
		{
			tmp = tmp->next;
			deleteFront();
		}
		return tmp;
	}

	//inserts a node to the front of a ListNode object (list). checks if list is empty.
	void EventList::frontInsert(Event* newFront)
	{
		//may have to create node first test this!!
		if (newFront->next != NULL || newFront->occurTime >= 0)
		{
			if (isEmpty() == true) {
				newFront->next = front;
				front = newFront;
				last = newFront;
			}
			else
			{
				newFront->next = front;
				front = newFront;
			}
			numOfEvents++;
		}
		else cout << "error this node either has no data or is connected to something!!" << endl;
	}

	//method to insert at the end of the list. uses last node as reference to insert the new node after. 
	//error checking for adding a node with no data
	void EventList::insertEnd(Event* newTail)
	{
		if (isEmpty() == true)
			frontInsert(newTail);
		else {
			last->next = newTail;
			last = newTail;
			numOfEvents++;
		}
	}

	//method to insert a node into the nth position in a list
	void EventList::midInsert(Event* event, Event* nextEvent)
	{
		Event* node = event;
		node->occurTime = event->occurTime;
		node->type = event->type;
		node->next = NULL;

		Event* tmp = front;

		if (tmp == NULL)
			insertEnd(node);
		while (tmp->next != nextEvent)
		{
			tmp = tmp->next;
		}
		event->next = nextEvent;
		tmp->next = event;
		numOfEvents++;
	}

	//method to delete a node from the front of the list. 
	void EventList::deleteFront()
	{
		if (isEmpty() == false)
		{
			Event* newFront = front->next;
			delete front;
			front = newFront;
			numOfEvents--;
		}
		else cout << "Cannot delete from empty list!" << endl;
	}

	//method to delete a node from the end of the list.
	//traverse the list to node before the last node.
	void EventList::deleteEnd()
	{
		if (isEmpty() == true)
			cout << "Empty list" << endl;
		if (front->next == NULL)
			deleteFront();

		Event* tmp = front;
		while (tmp->next != last)
		{
			tmp = tmp->next;
		}
		delete last;
		last = tmp;
		last->next = NULL;
		numOfEvents--;
	}

	//method to delete a node from a point in the list. parameter passed is the node to be deleted from the list.
	//traverse the list to the node before the target node. update previous node's pointer to delNode->next then delete target node
	void EventList::deleteMid(Event* delNode)
	{
		Event* p = front;
		if (p == NULL)
			cout << "error Empty List!!" << endl;
		if (delNode == last)
			deleteEnd();

		while (p->next != delNode)
		{
			p = p->next;
		}
		p->next = delNode->next;
		delete delNode;
		delNode = NULL;
		numOfEvents--;
	}

	void EventList::sort(Event* head)
	{
		Event* p = head;
		Event* current = p;
		Event* next = p->next;
		int val;

		if (head == NULL)
			cout << "list is empty" << endl;
		while (p != last)
		{
			while (next != NULL)
			{
				if (current->occurTime > next->occurTime)
				{
					val = current->occurTime;
					current->occurTime = next->occurTime;
					next->occurTime = val;

					current = p;
					next = p->next;
				}
				else {
					current = current->next;
					next = next->next;
				}
			}
			p = p->next;
		}
	}

	const void EventList::print(Event* listFront)
	{
		Event* p = listFront;
		if (p == NULL)
			cout << "NULL" << endl;
		else
		{
			cout << p->occurTime << " ";
			print(p->next);
		}
	}

	Event* EventList::addNode(int num, int type)
	{
		Event* newEvent = new Event;
		newEvent->occurTime = num;
		newEvent->type = type;
		newEvent->next = NULL;

		insertEnd(newEvent);
		return front;
	}

	void EventList::updateLast()
	{
		while (last->next != NULL)
			last = last->next;
	}

	void EventList::clearList()
	{
		if (isEmpty() == true)
			cout << "list is empty" << endl;
		Event* del = front;
		Event* p = front;

		while (p != last)
		{
			p = p->next;
			delete del;
			del = p;
		}
	}


Topic archived. No new replies allowed.