Advertisement
Guest User

Untitled

a guest
Dec 14th, 2020
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.55 KB | None | 0 0
  1. #include "SellOperation.h"
  2.  
  3. SellOperation::SellOperation(const Seller &seller, const Book &book, const Date &date, int quantity) :
  4.     seller_(seller), book_(book), sellDate_(date) {
  5.     try {
  6.         this->quantity_ = CheckInput::IntBounds(quantity, 0, 20);
  7.         this->sum_ = (double)this->quantity_ * this->book_.getPrice();
  8.     }
  9.     catch (Exception &ex) {
  10.         ex.error();
  11.         this->quantity_ = 0;
  12.         this->sum_ = 0.;
  13.     }
  14. }
  15.  
  16. SellOperation::SellOperation(
  17.     const std::string &s_name, const std::string &s_surname, const Date &s_birth, const Date &s_enroll,
  18.     const std::string &b_name, const Author &b_author, float b_price,
  19.     const Date &date, int quantity)
  20.     : seller_(s_name, s_surname, s_birth, s_enroll), book_(b_name, b_author, b_price), sellDate_(date) {
  21.     try {
  22.         this->quantity_ = CheckInput::IntBounds(quantity, 0, 20);
  23.         this->sum_ = (double)this->quantity_ * this->book_.getPrice();
  24.     }
  25.     catch (Exception &ex) {
  26.         ex.error();
  27.         this->quantity_ = 0;
  28.         this->sum_ = 0.;
  29.     }
  30. }
  31.  
  32. Seller &SellOperation::getSeller() const {
  33.     return (Seller &)this->seller_;
  34. }
  35.  
  36. Book &SellOperation::getBook() const {
  37.     return (Book &)this->book_;
  38. }
  39.  
  40. Date &SellOperation::getSellDate() const {
  41.     return (Date &)this->sellDate_;
  42. }
  43.  
  44. int SellOperation::getQuantity() const {
  45.     return this->quantity_;
  46. }
  47.  
  48. double SellOperation::getSum() const {
  49.     return this->sum_;
  50. }
  51.  
  52. void SellOperation::setSeller(const Seller &seller) {
  53.     this->seller_ = seller;
  54. }
  55.  
  56. void SellOperation::setBook(const Book &book) {
  57.     this->book_ = book;
  58. }
  59.  
  60. void SellOperation::setSellDate(const Date &date) {
  61.     this->sellDate_ = date;
  62. }
  63.  
  64. void SellOperation::setQuantity(int quantity) {
  65.     try {
  66.         this->quantity_ = CheckInput::IntBounds(quantity, 0, 20);
  67.         this->sum_ = (double)this->quantity_ * this->book_.getPrice();
  68.     }
  69.     catch (Exception &ex) {
  70.         ex.error();
  71.         this->quantity_ = 0;
  72.         this->sum_ = 0.;
  73.     }
  74. }
  75.  
  76. void SellOperation::readBinary(std::ifstream &in) {
  77.     try {
  78.         this->sellDate_.readBinary(in);
  79.         this->seller_.readBinary(in);
  80.         this->book_.readBinary(in);
  81.  
  82.         in.read((char *)&this->quantity_, sizeof(this->quantity_));
  83.         in.read((char *)&this->sum_, sizeof(this->sum_));
  84.  
  85.         this->quantity_ = CheckInput::IntBounds(this->quantity_, 0, 20);
  86.  
  87.         double sum = (double)this->quantity_ * this->book_.getPrice();
  88.         if (sum != this->sum_)
  89.             throw InputValueNotAllowed("The read sum does not meet formula (sum = quantity * price)");
  90.     }
  91.     catch (InputOutOfRange &ex) {
  92.         ex.error();
  93.         this->quantity_ = 0;
  94.         this->sum_ = 0;
  95.     }
  96.     catch (InputValueNotAllowed &ex) {
  97.         ex.error();
  98.         this->sum_ = (double)this->quantity_ * this->book_.getPrice();
  99.     }
  100. }
  101.  
  102. void SellOperation::writeBinary(std::ofstream &out) const {
  103.     this->sellDate_.writeBinary(out);
  104.     this->seller_.writeBinary(out);
  105.     this->book_.writeBinary(out);
  106.  
  107.     out.write((char *)&this->quantity_, sizeof(this->quantity_));
  108.     out.write((char *)&this->sum_, sizeof(this->sum_));
  109. }
  110.  
  111. std::ostream &operator<<(std::ostream &out, const SellOperation &op) {
  112.     out << "Sell operation at: " << op.sellDate_.getDateString() << '\n';
  113.     out << "Seller name: " << op.seller_.getName() << '\n';
  114.     out << "Seller surname: " << op.seller_.getSurname() << '\n';
  115.     out << "Seller birth: " << op.seller_.getBirth().getDateString() << '\n';
  116.     out << "Seller enroll date: " << op.seller_.getEnrollmentDate().getDateString() << '\n';
  117.     out << "Book name: " << op.book_.getName() << '\n';
  118.     out << "Author name: " << op.book_.getAuthor().getName() << '\n';
  119.     out << "Author surname: " << op.book_.getAuthor().getSurname() << '\n';
  120.     out << "Author birth: " << op.book_.getAuthor().getBirth().getDateString() << '\n';
  121.     out << "Price: " << op.book_.getPrice() << '\n';
  122.     out << "Quantity: " << op.quantity_ << '\n';
  123.     out << "Sum to pay: " << op.sum_ << std::endl;
  124.     return out;
  125. }
  126.  
  127. std::istream &operator>> (std::istream &in, SellOperation &op) {
  128.     try {
  129.         in >> op.sellDate_;
  130.         in >> op.seller_;
  131.         in >> op.book_;
  132.  
  133.         std::string input;
  134.  
  135.         std::getline(in, input);
  136.         input = input.substr(input.find_first_of(':') + 2);
  137.         op.quantity_ = CheckInput::IntBounds(std::stoi(input), 0, 20);
  138.  
  139.         std::getline(in, input);
  140.         input = input.substr(input.find_last_of(':') + 2, '\n');
  141.         op.sum_ = std::stod(input);
  142.        
  143.         double sum = op.book_.getPrice() * op.quantity_;
  144.         if (sum != op.sum_)
  145.             throw InputValueNotAllowed("The read sum does not meet formula (sum = quantity * price)");
  146.     }
  147.     catch (InputOutOfRange &ex) {
  148.         ex.error();
  149.         op.quantity_ = 0;
  150.         op.sum_ = 0;
  151.     }
  152.     catch (InputValueNotAllowed &ex) {
  153.         ex.error();
  154.         op.sum_ = (double)op.quantity_ * op.book_.getPrice();
  155.     }
  156.     return in;
  157. }
  158.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement