Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sstream>
- #include <stdexcept>
- #include "all.h"
- namespace Sandbox {
- using namespace std;
- class Item::Implementation {
- private:
- unsigned int id;
- string name;
- double price;
- static unsigned int lastId;
- static unsigned int aliveCount;
- private:
- void setId();
- friend class Item;
- friend std::ostream& operator<<(std::ostream& o, const Item &item);
- friend std::istream& operator>>(std::istream& i, Item &item);
- };
- unsigned int Item::Implementation::lastId = 0;
- unsigned int Item::Implementation::aliveCount = 0;
- Item::Item(const string &name, const double price){
- impl = new Implementation();
- impl->setId();
- setName(name);
- setPrice(price);
- ++Implementation::aliveCount;
- }
- Item::Item(const Item &other){
- impl = new Implementation(*(other.impl));
- ++Implementation::aliveCount;
- }
- Item& Item::operator=(const Item &other){
- if(this == &other)
- return *this;
- delete impl;
- impl = new Implementation(*(other.impl));
- return *this;
- }
- Item::~Item(){
- --Implementation::aliveCount;
- delete impl;
- }
- unsigned int Item::getId() const {
- return impl->id;
- }
- void Item::Implementation::setId(){
- id = Implementation::lastId++;
- }
- string Item::getName() const {
- return impl->name;
- }
- void Item::setName(const string &name) {
- impl->name = name;
- }
- double Item::getPrice() const {
- return impl->price;
- }
- void Item::setPrice(const double price){
- if (price >= 0){
- impl->price = price;
- } else {
- throw invalid_argument("Error: price should not be negative");
- }
- }
- Item& Item::operator++(){
- setPrice(getPrice() + 1.0);
- return *this;
- }
- Item Item::operator++(int){
- Item temp = *this;
- setPrice(getPrice() + 1.0);
- return temp;
- }
- string Item::operator=(const string &name){
- setName(name);
- return name;
- }
- Item Item::operator+(const Item &i2){
- if (getName() == i2.getName()){
- return Item(getName(), getPrice() + i2.getPrice());
- } else {
- throw NameMismatchException();
- }
- }
- void Item::operator+=(const double change){
- impl->price += change;
- //setPrice(getPrice() + change);
- }
- void Item::operator-=(const double change){
- impl->price -= change;
- //setPrice(getPrice() - change);
- }
- unsigned int Item::getAliveCount() {
- return Implementation::aliveCount;
- }
- string Item::toString() const {
- stringstream ss;
- ss << getId() << " " << getName() << " " << getPrice();
- return ss.str();
- }
- std::ostream& operator<<(std::ostream& o, const Item &item){
- o << item.toString();
- return o;
- }
- std::istream& operator>>(std::istream& i, Item &item){
- i >> item.impl->id;
- i >> item.impl->name;
- i >> item.impl->price;
- return i;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement