Expand|Select|Wrap|Line Numbers
- #include <string>
- #include <sstream>
- #include <cmath>
- using namespace std;
- #include "Tokeniser.h"
- #include "Utilities.h"
- #include "Currency.h"
- Currency::Currency():_dollars(0),_cents(0){}
- Currency::Currency(const Currency& curr)
- {
- _dollars = curr._dollars;
- _cents = curr._cents;
- }
- Currency::Currency(int dollars, int cents):_dollars(dollars),_cents(cents){}
- Currency::Currency(float curr)
- {
- _dollars = (int)curr;
- _cents = (int)((curr - _dollars) * 100);
- }
- Currency::Currency(const string& curr)
- {
- double f = atof(curr.c_str());
- _dollars = (int)f;
- _cents = (int)((f - _dollars) * 100);
- }
- int Currency::getDollars() const
- {
- return _dollars;
- }
- int Currency::getCents() const
- {
- return _cents;
- }
- void Currency::setDollars(int dollars)
- {
- this->_dollars = dollars;
- }
- void Currency::setCents(int cents)
- {
- this->_cents = cents;
- }
- float Currency::toFloat() const
- {
- return 0;
- }
- string Currency::toString() const
- {
- Currency curr;
- string returning_string;
- string assignStringDollars, assignStringCents ;
- ostringstream dollarStream, centStream;
- dollarStream << curr.getDollars();
- centStream << curr.getCents();
- assignStringDollars = dollarStream.str();
- assignStringCents = centStream.str();
- returning_string = assignStringDollars + "." + assignStringCents;
- return returning_string;
- }
- void Currency::fromFloat(float curr)
- {
- }
- int Currency::fromString(const string& curr)
- {
- Tokeniser tok(curr, ".");
- char *garbage;
- _dollars = strtol(tok.nextToken().c_str(), &garbage, 10);
- if (garbage != NULL && strlen(garbage) > 0)
- {
- return -1;
- }
- _cents = strtol(tok.nextToken().c_str(), &garbage, 10);
- if (garbage != NULL && strlen(garbage) > 0)
- {
- return -1;
- }
- return 0;
- }
- Currency& Currency::operator =(const Currency& obj)
- {
- if (*this != obj)
- {
- _dollars = obj.getDollars();
- _cents = obj.getCents();
- }
- return *this;
- }
- bool Currency::operator <(const Currency& obj) const
- {
- if (_dollars < obj.getDollars())
- {
- return true;
- }
- if (_dollars > obj.getDollars())
- {
- return false;
- }
- if (_cents < obj.getCents())
- {
- return true;
- }
- return false;
- }
- bool Currency::operator ==(const Currency& obj) const
- {
- if (_dollars == obj.getDollars() && _cents == obj.getCents())
- {
- return true;
- }
- return false;
- }
- bool Currency::operator !=(const Currency& obj) const
- {
- return !(*this == obj);
- }
- ostream& operator <<(ostream& out, const Currency& obj)
- {
- out << obj.getDollars() << "." << obj.getCents();
- return out;
- }
• Function: toFloat
This function converts the internal currency representation to a floating point number.
• Function: fromFloat
This function converts a floating point number to the internal representation of the currency class.