Hello Everyone,
I am working on a package delivery simulation. I believe I have a bug. Lines 47-53 give the user the option to choose a particular moment of time where they can see a snapshot of the system's status. The program seems to break down if I choose to view more than one snapshot. When I view the second snapshot, all the data is either lost, or it does not print correctly. It seems like line 49 does not work appropriately the second time it is used.
I would also like feedback on the overall programming. I believe the main() is too large, and I have considered adding the majority to the Event class. Does this make sense?
I would like to get the bugs worked out before I try to make this conversion.
Thanks for taking a look. I know it is lengthy.
main.cpp
#include "utility.h"
#include "Item.h"
#include "Event.h"
using namespace std;
template< typename T > void printStack( string type, T &stackRef );
template< typename T > void printQueue( string type, T &queueRef );
template <typename InputIterator> void printList(InputIterator first, InputIterator last);
template< typename T > bool checkQueue( T &queueRef );
template< typename T > void loadPlane(list<Event> &mylist, int time, queue<Item> &packageQueue, queue<Item> &planeQueue, T &chiStack, T &memphisStack); // need to add new event to event list in this fnc
template< typename T > void processPackage( list<Event> &mylist, int time, queue<Item> &packageQueue, T &chiStack, T &mempStack);
void forklift( list<Event> &mylist, int time);
template< typename T >void startupParameters( list<Event> &mylist, T &ePackage, T &ePlane, T &endOfService );
template< typename T > void snapShot(list<Event> &mylist, int time, queue<Item> &packageQueue, queue<Item> &planeQueue, T &chiStack, T &memphisStack);
int main()
{
const int maxTime = 2000;
stack< Item > chicagoStack;
stack< Item > memphisStack;
queue< Item > packageQueue;
queue< Item > planeQueue;
list<Event> mylist;
list<Event>::iterator current; //pointer into the list
Item iPackage;
Item iPlane;
Event ePackage;
Event ePlane;
Event endOfService;
int time = 0;
int response;
startupParameters(mylist, ePackage, ePlane, endOfService); // Loads event list with initial parameters per assignment
cout << "Enter a time from 0-" << maxTime << " for a snapshot, otherwise, enter 'F' to see final summary." << endl;
cin >> response;
for (int time = 0; time <= maxTime; time++)
{
if (time == response)
{
snapShot(mylist, time, packageQueue, planeQueue, chicagoStack, memphisStack);
cout << "Enter another time from " << time + 1 << " to " << maxTime << " or press 'F' for final summary." << endl;
cin >> response;
}
Event temp = mylist.front();
while (temp.getTimestamp() == time)
{
if(time == temp.getTimestamp())
{
if (temp.getEventKey() == "planeChicago")
{
mylist.pop_front();
iPlane.makeItem(time, "Chicago");
planeQueue.push(iPlane);
ePlane.makeEvent(time + 150, "planeChicago", "Chicago");
mylist.push_back(ePlane);
}
if (temp.getEventKey() == "planeMemphis")
{
mylist.pop_front();
iPlane.makeItem(time, "Memphis");
planeQueue.push(iPlane);
ePlane.makeEvent(time + 180, "planeMemphis", "Memphis");
mylist.push_back(ePlane);
}
if (temp.getEventKey() == "package")
{
if (time == 5)
{
iPackage.makeItem(5, "Memphis");
}
else
{
iPackage.makeItem(time);
}
packageQueue.push(iPackage);
mylist.pop_front();
ePackage.makeEvent(time + 10, "package", "NA");
mylist.push_back(ePackage);
}
mylist.sort();
if (temp.getEventKey() == "End of Service")
{
if (planeQueue.empty() == 0)
{
loadPlane(mylist, time, packageQueue, planeQueue, chicagoStack, memphisStack);
}
else if(packageQueue.empty() == 0)
{
processPackage(mylist, time, packageQueue, chicagoStack, memphisStack);
}
else
{
forklift(mylist, time);
}
}
mylist.sort();
temp = mylist.front();
//printList(mylist.begin(), mylist.end());
}
} // End while (temp.getTimestamp() == time)
} // end time
// snapShot(mylist, time, packageQueue, planeQueue, chicagoStack, memphisStack);
} // End main
template< typename T > void printStack(string type, T &stackRef )
{
int size = stackRef.size();
cout << type << " Package STACK:" << endl;
cout << "=======================" << endl;
for (int j = 0; j < size; j++) // check this, might be leaving 1 element in queue
{
cout << j << ") " << stackRef.top(); // Returns top element of stack
stackRef.pop(); // Removes top element from the stack
}
cout << "" << endl;
}
template< typename T > void printQueue( string type, T &queueRef )
{
cout << type << " QUEUE:" << endl;
cout << "=======================" << endl;
int j = 0;
while (queueRef.empty() == 0)
{
cout << j << ") " << queueRef.front(); // Returns top element of stack
queueRef.pop();
j++;
}
cout << "" << endl;
}
template <typename InputIterator> void printList(InputIterator first, InputIterator last)
{
cout << "EVENT LIST:" << endl;
cout << "=======================" << endl;
while (first != last)
cout << *first++;
cout << " " << endl;
}
template< typename T > bool checkQueue( T &queueRef )
{
}
template< typename T > void loadPlane(list<Event> &mylist, int time, queue<Item> &packageQueue, queue<Item> &planeQueue, T &chiStack, T &memphisStack)
{
Item qTemp;
qTemp = planeQueue.front();
planeQueue.pop();
int count = 0;
int x;
int y;
if(qTemp.getDestination() == "Chicago")
{
cout << "time " << time << " reached at loadPlane" << endl;
if (chiStack.size() > 25)
{
x = 25;
}
else
{
x = chiStack.size();
count++;
cout << count << endl;
}
for (int i = 0; i < x; i++) // Make sure this || operator (or) is working
{
chiStack.pop();
}
}
else if (qTemp.getDestination() == "Memphis")
{
if (memphisStack.size() > 25)
{
y = 25;
}
else
{
y = memphisStack.size();
}
for (int i = 0; i < y; i++) // Make sure this || operator (or) is working
{
memphisStack.pop();
count++;
cout << count << endl;
}
}
Event eTemp;
mylist.pop_front();
eTemp.makeEvent(time + 25, "End of Service", "endOfService");
mylist.push_back(eTemp);
mylist.sort();
}
template< typename T > void processPackage(list<Event> &mylist, int time, queue<Item> &packageQueue, T &chiStack, T &mempStack)
{
Item temp;
temp = packageQueue.front();
packageQueue.pop();
if (temp.getDestination() == "Chicago")
{
chiStack.push(temp);
}
else if (temp.getDestination() == "Memphis")
{
mempStack.push(temp);
}
Event eTemp; // This section creates a new end of service event and adds it to the event list
mylist.pop_front();
eTemp.makeEvent(time + 8, "End of Service", "endOfService");
mylist.push_back(eTemp);
mylist.sort();
}
void forklift(list<Event> &mylist, int time)
{
Event eTemp; // This section creates a new end of service event and adds it to the event list
mylist.pop_front();
eTemp.makeEvent(time + 6, "End of Service", "endOfService");
mylist.push_back(eTemp);
mylist.sort();
}
template< typename T >void startupParameters( list<Event> &mylist, T &ePackage, T &ePlane, T &endOfService )
{
ePackage.makeEvent(5, "package", "Memphis");
mylist.push_back(ePackage);
endOfService.makeEvent(8, "End of Service", "endOfService");
mylist.push_back(endOfService);
ePlane.makeEvent(80, "planeChicago", "Chicago");
mylist.push_back(ePlane);
ePlane.makeEvent(100, "planeMemphis", "Memphis");
mylist.push_back(ePlane);
}
template< typename T > void snapShot(list<Event> &mylist, int time, queue<Item> &packageQueue, queue<Item> &planeQueue, T &chiStack, T &memphisStack)
{
cout << "time is " << time << endl;
printList(mylist.begin(), mylist.end());
printQueue("Package", packageQueue);
printQueue("Plane", planeQueue);
printStack("Chicago", chiStack);
printStack("Memphis", memphisStack);
}
Event.cpp
#include "utility.h"
#include "Item.h"
#include "Event.h"
Event::Event()
{
//ctor
}
Event::~Event()
{
//dtor
}
bool operator<(Event& aEvent,Event& bEvent) {
return aEvent.getTimestamp() < bEvent.getTimestamp();
}
ostream& operator<<(ostream& output, Event& event)
{
output << event.eventKey << ", " << event.timestamp << ", " << event.destinationCode << endl;
return output;
}
void Event::makeEvent(int x, string key, string dest)
{
timestamp = x;
eventKey = key;
destinationCode = dest;
}
int Event::getTimestamp()
{
return timestamp;
}
string Event::getEventKey()
{
return eventKey;
}
string Event::getDestinationCode()
{
return destinationCode;
}
Event.h
#ifndef EVENT_H
#define EVENT_H
class Event
{
friend ostream &operator<<(ostream &,Event &); // << Overloaded to output Event parameters
friend bool operator<(Event & ,Event & ); // < Overloaded to compare timeStamp of two Events
public:
Event();
virtual ~Event();
void makeEvent(int x, string key, string dest);
int getTimestamp();
string getEventKey();
string getDestinationCode();
private:
int timestamp;
string eventKey;
string destinationCode;
};
#endif // EVENT_H
Item.cpp
#include "utility.h"
#include "Item.h"
// Precondition...
// Postcondition...
ostream& operator<<(ostream& output, Item& item)
{
output << item.dest << " at " << item.arrival << endl;
return output;
}
void Item::makeItem(int x)
{
arrival = x;
randomDest();
}
void Item::makeItem(int x, string aDest) // Overloaded to specify destination
{
arrival = x;
dest = aDest;
}
int Item::getArrival()
{
return arrival;
}
string Item::getDestination()
{
return dest;
}
string Item::randomDest()
{
int random = rand()%10;
if(random < 5)
{
dest = "Chicago";
}
else
{
dest = "Memphis";
}
return dest;
}
Item.h
#ifndef ITEM_H
#define ITEM_H
class Item
{
friend ostream &operator<<(ostream &,Item &); //not sure this is needed
public:
void makeItem(int x);
void makeItem(int x, string dest);
int getArrival();
string getDestination();
string randomDest();
private:
int arrival;
string dest;
};
#endif // ITEM_H
utility.h
#ifndef UTILITY_H
#define UTILITY_H
//Gives ANSI version of standard includes
//Also defines enumerated type for error
//messages
#include <iostream>
#include <limits>
#include <cmath>
#include <cstdlib>
#include <cstddef>
#include <fstream>
#include <cctype>
#include <ctime>
#include <string>
#include <stack>
#include <queue>
#include <list>
#include <algorithm>
using namespace std;
enum Error_code { success, fail, exceeds_range,
not_present, duplicate_error, underflow, overflow };
#endif //UTILITY_H