Use C++ std algorithms in controller

Use methods from User instead of directly modify attributes
This commit is contained in:
2011-08-14 21:03:48 +02:00
parent 8d7a019ca7
commit 5b9487b490
3 changed files with 76 additions and 57 deletions

View File

@@ -105,6 +105,35 @@ const wxFont User::GetCategoryFont(const wxString& catId)
return f;
}
void User::AddCategory(const Category& cat)
{
_categories.push_back(cat);
_categoriesFonts.push_back(KissCount::ExtractFont(wxT("")));
}
void User::UpdateCategory(const Category& cat)
{
std::vector<Category>::iterator it = std::find(_categories.begin(), _categories.end(), cat.id);
if (it !=_categories.end())
{
*it = cat;
_categoriesFonts[it-_categories.begin()] = KissCount::ExtractFont(cat.font);
}
}
void User::DeleteCategory(const Category& cat)
{
std::vector<Category>::iterator it = std::find(_categories.begin(), _categories.end(), cat.id);
if (it !=_categories.end())
{
int pos = it - _categories.begin();
_categories.erase(_categories.begin()+pos);
_categoriesFonts.erase(_categoriesFonts.begin()+pos);
}
}
Account User::GetAccount(const wxString& accountId) throw (AccountNotFound)
{
std::vector<Account>::iterator it = std::find(_accounts.begin(), _accounts.end(), accountId);
@@ -134,6 +163,27 @@ wxString User::GetAccountId(const wxString& accountName)
return wxT("0") ;
}
void User::AddAccount(Account& ac)
{
_accounts.push_back(ac);
}
void User::UpdateAccount(Account& ac)
{
std::vector<Account>::iterator it = std::find(_accounts.begin(), _accounts.end(), ac.id);
if (it != _accounts.end())
*it = ac;
}
void User::DeleteAccount(Account& ac)
{
std::vector<Account>::iterator it = std::find(_accounts.begin(), _accounts.end(), ac.id);
if (it != _accounts.end())
_accounts.erase(it);
}
int User::GetCategoriesNumber()
{
return _categories.size();

View File

@@ -57,10 +57,18 @@ public:
wxString GetCategoryName(const wxString& catId);
wxString GetCategoryId(const wxString& catName);
const wxFont GetCategoryFont(const wxString& catId);
void AddCategory(const Category& cat);
void UpdateCategory(const Category& cat);
void DeleteCategory(const Category& cat);
Account GetAccount(const wxString& accountId) throw (AccountNotFound);
wxString GetAccountName(const wxString& accountId);
wxString GetAccountId(const wxString& accountName);
void AddAccount(Account& ac);
void UpdateAccount(Account& ac);
void DeleteAccount(Account& ac);
int GetCategoriesNumber();
int GetAccountsNumber();