Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "WeekBalance.h"
- WeekBalance::WeekBalance(const SellOperation *arr, int size) {
- try {
- CheckInput::NullPointer(arr);
- if (this->operationList_) {
- delete[]this->operationList_;
- }
- this->length_ = size;
- this->operationList_ = new SellOperation[this->length_];
- copyList(this->operationList_, arr, size);
- }
- catch (Exception &ex) {
- ex.error();
- this->operationList_ = nullptr;
- this->length_ = 0;
- }
- }
- WeekBalance::WeekBalance(const WeekBalance &obj) {
- if (obj.operationList_ == nullptr) {
- this->operationList_ = nullptr;
- this->length_ = 0;
- }
- else {
- if (this->operationList_) {
- delete[]this->operationList_;
- }
- this->length_ = obj.length_;
- this->operationList_ = new SellOperation[this->length_];
- copyList(this->operationList_, obj.operationList_, obj.length_);
- }
- }
- WeekBalance::~WeekBalance() {
- delete[]this->operationList_;
- }
- SellOperation *WeekBalance::getOperations() const {
- if (this->operationList_) {
- SellOperation *arr = new SellOperation[this->length_];
- copyList(arr, this->operationList_, this->length_);
- return arr;
- }
- else {
- return nullptr;
- }
- }
- int WeekBalance::getLength() const {
- return this->length_;
- }
- void WeekBalance::setOperations(const SellOperation *arr, int size) {
- try {
- CheckInput::NullPointer(arr);
- if (this->operationList_) {
- delete[]this->operationList_;
- }
- this->length_ = size;
- this->operationList_ = new SellOperation[this->length_];
- copyList(this->operationList_, arr, size);
- }
- catch (Exception &ex) {
- ex.error();
- this->operationList_ = nullptr;
- this->length_ = 0;
- }
- }
- void WeekBalance::addOperation(const SellOperation &obj) {
- SellOperation *arr = nullptr;
- if (this->operationList_ != nullptr) {
- arr = new SellOperation[this->length_];
- copyList(arr, this->operationList_, this->length_);
- delete[]this->operationList_;
- }
- this->length_ += 1;
- this->operationList_ = new SellOperation[this->length_];
- if (arr != nullptr) {
- copyList(this->operationList_, arr, this->length_ - 1);
- }
- this->operationList_[this->length_ - 1] = obj;
- }
- SellOperation *WeekBalance::begin() const {
- return this->operationList_ ? &operationList_[0] : nullptr;
- }
- SellOperation *WeekBalance::end() const {
- return this->operationList_ ? operationList_ + this->length_ : nullptr;
- }
- void WeekBalance::erase() {
- delete[]this->operationList_;
- this->operationList_ = nullptr;
- this->length_ = 0;
- }
- void WeekBalance::readBinary(std::ifstream &in) {
- try {
- in.seekg(0, in.end);
- int len = in.tellg();
- in.seekg(0, in.beg);
- this->erase();
- SellOperation op;
- while (len > in.tellg()) {
- op.readBinary(in);
- this->addOperation(op);
- len--;
- }
- }
- catch (Exception &ex) {
- ex.error();
- this->erase();
- }
- }
- void WeekBalance::writeBinary(std::ofstream &out) const {
- for (auto item = this->begin(); item != this->end(); item++) {
- item->writeBinary(out);
- }
- }
- SellOperation &WeekBalance::operator[](int index) {
- try {
- CheckInput::IndexRange(index, this->length_);
- return operationList_[index];
- }
- catch (Exception *ex) {
- ex->error();
- SellOperation *op = new SellOperation();
- return *op;
- }
- }
- const SellOperation &WeekBalance::operator[](int index) const {
- try {
- CheckInput::IndexRange(index, this->length_);
- return operationList_[index];
- }
- catch (Exception *ex) {
- ex->error();
- return *(new SellOperation());
- }
- }
- WeekBalance::operator int*() const {
- if (this->operationList_ == nullptr)
- return nullptr;
- int *arr = new int[this->length_];
- for (int i = 0; i < this->length_; i++) {
- arr[i] = this->operationList_[i].getQuantity();
- }
- return arr;
- }
- WeekBalance::operator double *() const {
- if (this->operationList_ == nullptr)
- return nullptr;
- double *arr = new double[this->length_];
- for (int i = 0; i < this->length_; i++) {
- arr[i] = this->operationList_[i].getSum();
- }
- return arr;
- }
- std::ostream &operator<<(std::ostream &out, const WeekBalance &week) {
- try {
- if (week.operationList_ == nullptr)
- throw NullPointerUse("Week balance list is empty!");
- for (auto item = week.begin(); item != week.end(); item++) {
- out << *item;
- }
- }
- catch (Exception &ex) {
- ex.error();
- }
- return out;
- }
- std::istream &operator>>(std::istream &in, WeekBalance &week) {
- try {
- week.erase();
- SellOperation op;
- while (!in.eof()) {
- in >> op;
- week.addOperation(op);
- }
- if (in.fail())
- throw InvalidFileToRead("Invalid read operation!");
- }
- catch (Exception &ex) {
- ex.error();
- week.erase();
- }
- return in;
- }
- void WeekBalance::copyList(SellOperation *dest, const SellOperation *src, int size) const {
- for (int i = 0; i < size; i++) {
- dest[i] = src[i];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement