#include "Grocery.h" //Grocery methods Grocery::Grocery() { name = "undefined"; markup = 1.0; cost = 0; } Grocery::Grocery( ifstream& fin ) throw(TokenError) { Extract( fin ); } Grocery::Grocery( string Name, double Markup, int Cost ) { name = Name; markup = Markup; cost = Cost; } string Grocery::getName() const { return name; } double Grocery::getMarkup() const { //markup is always >= 0.0 //markup == 1.0 for no profit return markup; } int Grocery::getCost() const { //cost is non-negative measured in cents return cost; } int Grocery::getPrice() const { return (int)(cost * markup + 0.5); } void Grocery::Extract(ifstream& fin ) throw(TokenError) { string opentkn, closetkn; fin >> opentkn; if( opentkn != "Grocery{" ) throw TokenError("Invalid Open Token!", "Grocery::Extract(1)"); Get( fin ); fin >> closetkn; if( closetkn != "}Grocery" ) throw TokenError("Invalid Close Token!", "Grocery::Extract(2)"); } void Grocery::Insert( ostream& fout ) { fout << endl; fout << " Grocery{ "; Put( fout ); fout << " }Grocery "; } void Grocery::Get( ifstream& fin) throw(TokenError) { string label; fin >> label; if( label != "name:" ) throw TokenError("Invalid field label, name: expected", "Grocery::Get(1)"); fin >> name; fin >> label; if( label != "markup:" ) throw TokenError("Invalid field label, markup: expected", "Grocery::Get(2)"); fin >> markup; fin >> label; if( label != "cost:" ) throw TokenError("Invalid field label, cost: expected", "Grocery::Get(3)"); fin >> cost; } void Grocery::Put ( ostream& fout) { fout << " name: " << name << " markup: " << markup << " cost: " << cost; } //FRIENDS ifstream& operator>>(ifstream& fin, Grocery& obj) throw(TokenError) { obj.Extract(fin); return fin; } ostream& operator<<(ostream& fout, Grocery& obj) { obj.Insert(fout); return fout; }