// PostOffice.cpp
/**
* Definition of class PostOffice. PostOffice is a class that
* is used to represent a Post Office.
* Mail items can be dropped into the Post Office. Each Post Office
* is identified by the zipcode. Post Office is responsible in
* filltering all the Mail Items and delivering them to the residents
* or other Post Offices.
*
*/
#include "Address.h"
#include "MailItem.h"
#include "PostalBox.h"
#include "PostOffice.h"
#include <iostream>
using namespace std;
/*
MailItem** allMails;
MailItem** myMails;
int sizeAll;
int capacityAll;
int sizeMy;
int capacityMy;
string zip;
*/
// Constructor to create a Post Office identified by the zipcode
PostOffice::PostOffice(string zip)
{
zip = "";
sizeAll = 10;
sizeMy = 10;
capacityAll = 0;
capacityMy = 0;
for (int i = 0; i < sizeAll; i++)
{
allMails[i] = NULL;
myMails[i] = NULL;
}
}
// Collect all the mail from the Postal Box
void PostOffice::collectMail(PostalBox& box)
{
int size;
MailItem** tempPB = box.getMail(size);
for (int i = 0; i < size; i++)
{
allMails[sizeAll++] = tempPB[i];
tempPB[i] = NULL;
}
delete[] tempPB;
tempPB = NULL;
}
// Receive a single Mail Item from a Person
void PostOffice::receiveMail(MailItem* item)
{
allMails[sizeAll++] = item;
}
// Receive Mail from Post Office as a bulk
void PostOffice::receiveMailFromOffice(MailItem** items, int size)
{
for (int i = 0; i < size; i++)
{
myMails[sizeMy++] = items[i];
items[i] = NULL;
}
}
// Filter all the Mail Items and send to appropriate offices
void PostOffice::filterMail()
{
}
// Deliver Mail to residents
void PostOffice::deliverMail()
{
if (sizeMy == 0)
{
cout << "PostOffice is empty" << endl;
return;
}
for (int i = 0; i < sizeMy; i++)
{
// cout << myMails[i]->output();
cout << endl;
}
}
// identify the Post Office
string PostOffice::getZip() const
{
return zip;
}
// Deallocate the instance
PostOffice::~PostOffice()
{
for (int i = 0; i < sizeMy; i++)
{
delete myMails[i];
}
delete[] myMails;
}
// Deep copy constructor
PostOffice::PostOffice(const PostOffice& other)
{
}
// Deep copy assignment operator
PostOffice& PostOffice::operator = (const PostOffice& other)
{
if (this != &other)
{
for (int i = 0; i < sizeMy; i++)
{
delete myMails[i];
}
delete[] myMails;
sizeMy = other.sizeMy;
capacityMy = other.capacityMy;
zip = other.zip;
myMails = new MailItem*[capacityMy];
for (int i = 0; i < sizeMy; i++)
{
myMails[i] = new MailItem(*other.myMails[i]);
}
for (int j = sizeMy; j < capacityMy; j++)
{
myMails[j] = NULL;
}
}
return *this;
}
static void PostOffice::addOffice(PostOffice* office)
{
if (numOffices == 0)
{
zipcodes = new string[5];
offices = new PostOffice*[5];
}
offices[numOffices] = office;
zipcodes[numOffices++] = office->getZip();
}
static void PostOffice::removeOffice(PostOffice* office)
{
string zip = office->getZip();
for (int i = 0; i < numOffices; i++)
{
if(zip == zipcodes[i])
{
zipcodes[i] = zipcodes[numOffices - 1];
offices[i] = offices[numOffices - 1];
numOffices--;
break;
}
}
if (numOffices == 0)
{
delete [] zipcodes;
delete [] offices;
}
}
void PostOffice::growAll()
{
capacityAll = (2* capacityAll) + 1;
MailItem** bigger = new MailItem*[capacityAll];
for (int i = 0; i < sizeAll; i++)
{
bigger[i] = allMails[i];
}
for (int j = sizeAll; j < capacityAll; j++)
{
bigger[j] = NULL;
}
delete[] allMails;
allMails = bigger;
}
void PostOffice::growMy()
{
capacityMy = (2* capacityMy) + 1;
MailItem** bigger2 = new MailItem*[capacityMy];
for (int i = 0; i < sizeMy; i++)
{
bigger2[i] = myMails[i];
}
for (int j = sizeMy; j < capacityMy; j++)
{
bigger2[j] = NULL;
}
delete[] myMails;
myMails = bigger2;
}
// PostOffice.h
/**
* Declaration of class PostOffice. PostOffice is a class that
* is used to represent a Post Office.
* Mail items can be dropped into the Post Office. Each Post Office
* is identified by the zipcode. Post Office is responsible in
* filltering all the Mail Items and delivering them to the reidents
* or other Post Offices.
*
*/
#ifndef POST_OFFICE_H
#define POST_OFFICE_H
#include "Address.h"
#include "MailItem.h"
#include "PostalBox.h"
#include <string>
#include <iostream>
using namespace std;
class PostOffice
{
private:
MailItem** allMails;
MailItem** myMails;
int sizeAll;
int capacityAll;
int sizeMy;
int capacityMy;
string zip;
static PostOffice** offices;
static int numOffices;
static string* zipcodes;
// Increase the capacity of the arrays of pointers
void growAll();
void growMy();
public:
// Constructor to create a Post Office identified by the zipcode
PostOffice(string zip);
// Collect all the mail from the Postal Box
void collectMail(PostalBox& box);
// Receive a single Mail Item from a Person
void receiveMail(MailItem* item);
// Receive Mail from Post Office as a bulk
void receiveMailFromOffice(MailItem** items, int size);
// Filter all the Mail Items and send to appropriate offices
void filterMail();
// Deliver Mail to residents
void deliverMail();
// identify the Post Office
string getZip() const;
// Deallocate the instance
~PostOffice();
// Deep copy constructor
PostOffice(const PostOffice& other);
// Deep copy assignment operator
PostOffice& operator = (const PostOffice& other);
static void addOffice(PostOffice* office);
static void removeOffice(PostOffice* office);
};
PostOffice** PostOffice::offices = NULL;
int PostOffice::numOffices = 0;
string* PostOffice::zipcodes = NULL;
#endif