Advertisement
Guest User

Untitled

a guest
Dec 14th, 2020
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.75 KB | None | 0 0
  1. #include "WeekBalance.h"
  2.  
  3. WeekBalance::WeekBalance(const SellOperation *arr, int size) {
  4.     try {
  5.         CheckInput::NullPointer(arr);
  6.  
  7.         if (this->operationList_) {
  8.             delete[]this->operationList_;
  9.         }
  10.         this->length_ = size;
  11.         this->operationList_ = new SellOperation[this->length_];
  12.         copyList(this->operationList_, arr, size);
  13.     }
  14.     catch (Exception &ex) {
  15.         ex.error();
  16.         this->operationList_ = nullptr;
  17.         this->length_ = 0;
  18.     }
  19. }
  20.  
  21. WeekBalance::WeekBalance(const WeekBalance &obj) {
  22.     if (obj.operationList_ == nullptr) {
  23.         this->operationList_ = nullptr;
  24.         this->length_ = 0;
  25.     }
  26.     else {
  27.         if (this->operationList_) {
  28.             delete[]this->operationList_;
  29.         }
  30.         this->length_ = obj.length_;
  31.         this->operationList_ = new SellOperation[this->length_];
  32.         copyList(this->operationList_, obj.operationList_, obj.length_);
  33.     }
  34. }
  35.  
  36. WeekBalance::~WeekBalance() {
  37.     delete[]this->operationList_;
  38. }
  39.  
  40. SellOperation *WeekBalance::getOperations() const {
  41.     if (this->operationList_) {
  42.         SellOperation *arr = new SellOperation[this->length_];
  43.         copyList(arr, this->operationList_, this->length_);
  44.         return arr;
  45.     }
  46.     else {
  47.         return nullptr;
  48.     }
  49. }
  50.  
  51. int WeekBalance::getLength() const {
  52.     return this->length_;
  53. }
  54.  
  55. void WeekBalance::setOperations(const SellOperation *arr, int size) {
  56.     try {
  57.         CheckInput::NullPointer(arr);
  58.  
  59.         if (this->operationList_) {
  60.             delete[]this->operationList_;
  61.         }
  62.         this->length_ = size;
  63.         this->operationList_ = new SellOperation[this->length_];
  64.         copyList(this->operationList_, arr, size);
  65.     }
  66.     catch (Exception &ex) {
  67.         ex.error();
  68.         this->operationList_ = nullptr;
  69.         this->length_ = 0;
  70.     }
  71. }
  72.  
  73. void WeekBalance::addOperation(const SellOperation &obj) {
  74.     SellOperation *arr = nullptr;
  75.     if (this->operationList_ != nullptr) {
  76.         arr = new SellOperation[this->length_];
  77.         copyList(arr, this->operationList_, this->length_);
  78.         delete[]this->operationList_;
  79.     }
  80.  
  81.     this->length_ += 1;
  82.     this->operationList_ = new SellOperation[this->length_];
  83.  
  84.     if (arr != nullptr) {
  85.         copyList(this->operationList_, arr, this->length_ - 1);
  86.     }
  87.     this->operationList_[this->length_ - 1] = obj;
  88. }
  89.  
  90. SellOperation *WeekBalance::begin() const {
  91.     return this->operationList_ ? &operationList_[0] : nullptr;
  92. }
  93.  
  94. SellOperation *WeekBalance::end() const {
  95.     return this->operationList_ ? operationList_ + this->length_ : nullptr;
  96. }
  97.  
  98. void WeekBalance::erase() {
  99.     delete[]this->operationList_;
  100.     this->operationList_ = nullptr;
  101.     this->length_ = 0;
  102. }
  103.  
  104. void WeekBalance::readBinary(std::ifstream &in) {
  105.     try {
  106.         in.seekg(0, in.end);
  107.         int len = in.tellg();
  108.         in.seekg(0, in.beg);
  109.  
  110.         this->erase();
  111.  
  112.         SellOperation op;
  113.         while (len > in.tellg()) {
  114.             op.readBinary(in);
  115.  
  116.             this->addOperation(op);
  117.             len--;
  118.         }
  119.     }
  120.     catch (Exception &ex) {
  121.         ex.error();
  122.         this->erase();
  123.     }
  124. }
  125.  
  126. void WeekBalance::writeBinary(std::ofstream &out) const {
  127.     for (auto item = this->begin(); item != this->end(); item++) {
  128.         item->writeBinary(out);
  129.     }
  130. }
  131.  
  132. SellOperation &WeekBalance::operator[](int index) {
  133.     try {
  134.         CheckInput::IndexRange(index, this->length_);
  135.         return operationList_[index];
  136.     }
  137.     catch (Exception *ex) {
  138.         ex->error();
  139.         SellOperation *op = new SellOperation();
  140.         return *op;
  141.     }
  142. }
  143.  
  144. const SellOperation &WeekBalance::operator[](int index) const {
  145.     try {
  146.         CheckInput::IndexRange(index, this->length_);
  147.         return operationList_[index];
  148.     }
  149.     catch (Exception *ex) {
  150.         ex->error();
  151.         return *(new SellOperation());
  152.     }
  153. }
  154.  
  155. WeekBalance::operator int*() const {
  156.     if (this->operationList_ == nullptr)
  157.         return nullptr;
  158.  
  159.     int *arr = new int[this->length_];
  160.     for (int i = 0; i < this->length_; i++) {
  161.         arr[i] = this->operationList_[i].getQuantity();
  162.     }
  163.     return arr;
  164. }
  165.  
  166. WeekBalance::operator double *() const {
  167.     if (this->operationList_ == nullptr)
  168.         return nullptr;
  169.  
  170.     double *arr = new double[this->length_];
  171.     for (int i = 0; i < this->length_; i++) {
  172.         arr[i] = this->operationList_[i].getSum();
  173.     }
  174.     return arr;
  175. }
  176.  
  177. std::ostream &operator<<(std::ostream &out, const WeekBalance &week) {
  178.     try {
  179.         if (week.operationList_ == nullptr)
  180.             throw NullPointerUse("Week balance list is empty!");
  181.  
  182.         for (auto item = week.begin(); item != week.end(); item++) {
  183.             out << *item;
  184.         }
  185.     }
  186.     catch (Exception &ex) {
  187.         ex.error();
  188.     }
  189.     return out;
  190. }
  191.  
  192. std::istream &operator>>(std::istream &in, WeekBalance &week) {
  193.     try {
  194.         week.erase();
  195.  
  196.         SellOperation op;
  197.         while (!in.eof()) {
  198.             in >> op;
  199.             week.addOperation(op);
  200.         }
  201.  
  202.         if (in.fail())
  203.             throw InvalidFileToRead("Invalid read operation!");
  204.     }
  205.     catch (Exception &ex) {
  206.         ex.error();
  207.         week.erase();
  208.     }
  209.     return in;
  210. }
  211.  
  212. void WeekBalance::copyList(SellOperation *dest, const SellOperation *src, int size) const {
  213.     for (int i = 0; i < size; i++) {
  214.         dest[i] = src[i];
  215.     }
  216. }
  217.  
  218.  
  219.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement