L4A/Warehouse.cpp
2024-11-06 16:49:45 -07:00

87 lines
2.1 KiB
C++

/**
* @author Tyler Beckman (tyler_beckman@mines.edu)
* @brief A program to store boxes of custom size into a warehouse,
* using pointers and proper memory-management techniques.
* @version 1
* @date 2024-10-22
*/
#include "Warehouse.h"
#include "Box.h"
#include <iostream>
#include <vector>
using namespace std;
Warehouse::Warehouse() {
_pBoxen = new vector<Box *>;
_warehouseLetter = '?';
}
Warehouse::Warehouse(const Warehouse &OTHER) {
this->_pBoxen = new std::vector<Box *>;
this->_warehouseLetter = OTHER._warehouseLetter;
for (size_t i = 0; i < OTHER._pBoxen->size(); i++) {
this->_pBoxen->push_back(new Box(OTHER._pBoxen->at(i)->getBoxSize()));
}
}
Warehouse &Warehouse::operator=(const Warehouse &OTHER) {
// Check for self assignment
if (this == &OTHER) {
return *this;
}
std::cout << "hi" << std::endl;
// Clear existing contents
for (size_t i = 0; i < this->_pBoxen->size(); i++) {
delete this->_pBoxen->at(i);
}
this->_pBoxen->clear();
// Deep copy OTHER into this
for (size_t i = 0; i < OTHER._pBoxen->size(); i++) {
this->_pBoxen->push_back(new Box(OTHER._pBoxen->at(i)->getBoxSize()));
}
this->_warehouseLetter = OTHER._warehouseLetter;
return *this;
}
Warehouse::~Warehouse() {
for (size_t i = 0; i < this->_pBoxen->size(); i++) {
delete this->_pBoxen->at(i);
}
delete this->_pBoxen;
}
void Warehouse::storeInBox(const int SIZE) {
_pBoxen->push_back(new Box(SIZE));
}
Box *Warehouse::getBox(const int BOX_POS) const { return _pBoxen->at(BOX_POS); }
int Warehouse::getNumberOfBoxes() const { return _pBoxen->size(); }
char Warehouse::getWarehouseLetter() const { return _warehouseLetter; }
void Warehouse::setWarehouseLetter(const char warehouseLetter) {
_warehouseLetter = warehouseLetter;
}
std::ostream &operator<<(ostream &os, const Warehouse &WH) {
os << "Warehouse " << WH.getWarehouseLetter() << " has "
<< WH.getNumberOfBoxes() << " boxes (";
for (int i = 0; i < WH.getNumberOfBoxes(); i++) {
os << WH.getBox(i)->getBoxSize();
if (i < WH.getNumberOfBoxes() - 1) {
os << ", ";
}
}
os << ")";
return os;
}