/**** * * Implementation of rolodex.h. * */ #include "rolodex.h" #include "std-macros.h" #include Rolodex::Rolodex(View* v) : Model(v) { cl = new CardList(); aie = new AddInputErrors(); die = new DeleteInputError(); cie = new ChangeInputErrors(); } Rolodex::~Rolodex() { delete cl; } void Rolodex::Add(Card* c) { /* * Clear out the input error data. */ aie->Clear(); /* * Throw a precond violation if a card of the given id is already in * this->data. */ if (cl->Find(c->GetId())) { aie->SetAlreadyThereError(); throw aie; } /* * Throw a precond violation if the validy check fails for one or more of * the card fields. The exception value is a tuple of values for each of * the invalid fields. */ if (ValidateAddInput(c)->AnyErrors()) { throw aie; } /* * If preconditions are met, add the given card to this->data using the * inherited List::Put function. */ cl->Put(new CardListElem(c)); } void Rolodex::Delete(Id id) { /* * Throw a precond violation if a card of the given id is not in * this->data. */ if (not cl->Find(id)) { throw die; } /* * If the precondition is met, delete the given card from this->data using * the inherited List::FindPos and List::DelNth functions. */ cl->DelNth(cl->FindPos((ListElemKey*) id)); } void Rolodex::Change(Id id, Card* c) { /* * Clear out the input error tuple. */ cie->Clear(); /* * Throw a precond violation if a card of the given id is not already in * this->data. */ if (not cl->Find(id)) { cie->SetNotThereError(); throw cie; } /* * Throw a precond violation if the given card is identical to the extant * card of the given id. */ if (cl->Find(id)->Equal(c)) { cie->SetIdenticalToExtantError(); throw cie; } /* * Throw a precond violation if the validy check fails for one or more of * the card fields. The exception value is a tuple of values for each of * the invalid fields. */ if (ValidateAddInput(c)->AnyErrors()) { throw aie; } /* * If preconditions are met, delete the extant card and add the new one. */ Delete(id); Add(c); } CardList* Rolodex::Find(Name* n) { Card* c; CardList* rtn = new CardList(); while (c = cl->Enum()) { if (*(c->GetName()) == *n) rtn->Put(new CardListElem(c)); } return rtn; } void Rolodex::Print() { Dump(); } void Rolodex::Dump() { cl->Print(); } Sex Rolodex::StringToSex(String* s) { if (*s == "M") return Male; else if (*s == "M") return Female; else return InvalidSex; } String* Rolodex::SexToString(Sex s) { if (s == Male) return new String("M"); else if (s == Female) return new String("F"); else return new String("Invalid Sex"); } AddInputErrors* Rolodex::ValidateAddInput(Card* c) { if (c->GetName()->Len() > 30) aie->SetNameError(); if ((c->GetId() < 100000000) or (c->GetId() > 999999999)) aie->SetIdError(); if ((c->GetAge() < 0) or (c->GetAge() > 200)) aie->SetAgeError(); if (c->GetSex() == InvalidSex) aie->SetSexError(); if (c->GetAddr()->Len() > 40) aie->SetAddressError(); return aie; }