Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SellOperation.h"
- SellOperation::SellOperation(const Seller &seller, const Book &book, const Date &date, int quantity) :
- seller_(seller), book_(book), sellDate_(date) {
- try {
- this->quantity_ = CheckInput::IntBounds(quantity, 0, 20);
- this->sum_ = (double)this->quantity_ * this->book_.getPrice();
- }
- catch (Exception &ex) {
- ex.error();
- this->quantity_ = 0;
- this->sum_ = 0.;
- }
- }
- SellOperation::SellOperation(
- const std::string &s_name, const std::string &s_surname, const Date &s_birth, const Date &s_enroll,
- const std::string &b_name, const Author &b_author, float b_price,
- const Date &date, int quantity)
- : seller_(s_name, s_surname, s_birth, s_enroll), book_(b_name, b_author, b_price), sellDate_(date) {
- try {
- this->quantity_ = CheckInput::IntBounds(quantity, 0, 20);
- this->sum_ = (double)this->quantity_ * this->book_.getPrice();
- }
- catch (Exception &ex) {
- ex.error();
- this->quantity_ = 0;
- this->sum_ = 0.;
- }
- }
- Seller &SellOperation::getSeller() const {
- return (Seller &)this->seller_;
- }
- Book &SellOperation::getBook() const {
- return (Book &)this->book_;
- }
- Date &SellOperation::getSellDate() const {
- return (Date &)this->sellDate_;
- }
- int SellOperation::getQuantity() const {
- return this->quantity_;
- }
- double SellOperation::getSum() const {
- return this->sum_;
- }
- void SellOperation::setSeller(const Seller &seller) {
- this->seller_ = seller;
- }
- void SellOperation::setBook(const Book &book) {
- this->book_ = book;
- }
- void SellOperation::setSellDate(const Date &date) {
- this->sellDate_ = date;
- }
- void SellOperation::setQuantity(int quantity) {
- try {
- this->quantity_ = CheckInput::IntBounds(quantity, 0, 20);
- this->sum_ = (double)this->quantity_ * this->book_.getPrice();
- }
- catch (Exception &ex) {
- ex.error();
- this->quantity_ = 0;
- this->sum_ = 0.;
- }
- }
- void SellOperation::readBinary(std::ifstream &in) {
- try {
- this->sellDate_.readBinary(in);
- this->seller_.readBinary(in);
- this->book_.readBinary(in);
- in.read((char *)&this->quantity_, sizeof(this->quantity_));
- in.read((char *)&this->sum_, sizeof(this->sum_));
- this->quantity_ = CheckInput::IntBounds(this->quantity_, 0, 20);
- double sum = (double)this->quantity_ * this->book_.getPrice();
- if (sum != this->sum_)
- throw InputValueNotAllowed("The read sum does not meet formula (sum = quantity * price)");
- }
- catch (InputOutOfRange &ex) {
- ex.error();
- this->quantity_ = 0;
- this->sum_ = 0;
- }
- catch (InputValueNotAllowed &ex) {
- ex.error();
- this->sum_ = (double)this->quantity_ * this->book_.getPrice();
- }
- }
- void SellOperation::writeBinary(std::ofstream &out) const {
- this->sellDate_.writeBinary(out);
- this->seller_.writeBinary(out);
- this->book_.writeBinary(out);
- out.write((char *)&this->quantity_, sizeof(this->quantity_));
- out.write((char *)&this->sum_, sizeof(this->sum_));
- }
- std::ostream &operator<<(std::ostream &out, const SellOperation &op) {
- out << "Sell operation at: " << op.sellDate_.getDateString() << '\n';
- out << "Seller name: " << op.seller_.getName() << '\n';
- out << "Seller surname: " << op.seller_.getSurname() << '\n';
- out << "Seller birth: " << op.seller_.getBirth().getDateString() << '\n';
- out << "Seller enroll date: " << op.seller_.getEnrollmentDate().getDateString() << '\n';
- out << "Book name: " << op.book_.getName() << '\n';
- out << "Author name: " << op.book_.getAuthor().getName() << '\n';
- out << "Author surname: " << op.book_.getAuthor().getSurname() << '\n';
- out << "Author birth: " << op.book_.getAuthor().getBirth().getDateString() << '\n';
- out << "Price: " << op.book_.getPrice() << '\n';
- out << "Quantity: " << op.quantity_ << '\n';
- out << "Sum to pay: " << op.sum_ << std::endl;
- return out;
- }
- std::istream &operator>> (std::istream &in, SellOperation &op) {
- try {
- in >> op.sellDate_;
- in >> op.seller_;
- in >> op.book_;
- std::string input;
- std::getline(in, input);
- input = input.substr(input.find_first_of(':') + 2);
- op.quantity_ = CheckInput::IntBounds(std::stoi(input), 0, 20);
- std::getline(in, input);
- input = input.substr(input.find_last_of(':') + 2, '\n');
- op.sum_ = std::stod(input);
- double sum = op.book_.getPrice() * op.quantity_;
- if (sum != op.sum_)
- throw InputValueNotAllowed("The read sum does not meet formula (sum = quantity * price)");
- }
- catch (InputOutOfRange &ex) {
- ex.error();
- op.quantity_ = 0;
- op.sum_ = 0;
- }
- catch (InputValueNotAllowed &ex) {
- ex.error();
- op.sum_ = (double)op.quantity_ * op.book_.getPrice();
- }
- return in;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement