1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
class Entry
{
public:
Entry(void) :title(""), amount(0) {}
Entry(std::string title, double amount) : title(title), amount(amount) {}
std::string title;
double amount;
void set(std::string title, double amount)
{
this->title = title;
this->amount = amount;
}
void print(std::ofstream output = cout)
{
output << title << ": " << amount << std::endl;
}
};
class Budget
{
std::string name;
std::vector<Entry> entries;
double initialBudget;
double currentSpendings;
public:
Budget(std::string name, double initial = 0) :name(name), initialBudget(initial), currentSpendings(0) {}
bool addEntry(std::string entryName, double amount);
void print(std::ostream output = cout);
};
bool Budget::addEntry(std::string entryName, double amount)
{
if (currentSpendings + amount > initialBudget)
{
std::cout << "We can't spend " << amount << "." << std::endl;
return false;
}
entries.push_back(Entry(entryName, amount);
currentSpendings += amount;
return true;
}
void Budget::print(std::ostream output)
{
output << "Budget: " << name << std::endl
"Initial Budget: " << initialBudget << std::endl
"Current Spendings: " << currentSpendings << std::endl
"Amount Available: " << initialBudget - currentSpendings << std::endl;
for (std::vector<Entry>::iterator iter = entries.begin(); iter !+ entries.end(); iter++)
{
*iter.print(output);
}
output << "End of " << name << "\'s budget\n\n";
}
int main(void)
{
Budget myBudget("Lowest0ne", 200);
Entry temp;
temp.set("Internet", 25);
std::cout << temp.title << " is " << (myBudget.addEntry(temp.title, temp.amount) ? "" << "not" ) << " okay\n";
temp.set("Everything else", 3000);
std::cout << temp.title << " is " << (myBudget.addEntry(temp.title, temp.amount) ? "" << "not" ) << " okay\n";
temp.set("Won lottery", -5000);
std:: cout << temp.title << " is " << (myBudget.addEntry(temp.title, temp.amount) ? "" << "not" ) << " okay\n";
temp.set("Everything else", 3000);
std::cout << temp.title << " is " << (myBudget.addEntry(temp.title, temp.amount) ? "" << "not" ) << " okay\n";
myBudget.print();
std::ofstream outFile("myBudget.txt");
myBudget.print(outFile);
outFile.close();
return 0;
}
|