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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
#include <iostream>
#include <iomanip>
// HEADER
struct MonthlyBudget
{
static inline std::string category[]
{
"Housing", "Utilities", "Household", "Transportation","Food",
"Medical", "Insurance", "Entertainment", "Clothing", "Miscellaneous",
"TOTALS" /* LIST CAN BE AMENDED */
};
const int LIMIT = sizeof(category)/sizeof(std::string) - 1;
double* budget = nullptr;
double* expense = nullptr;
void setBudget(double* a_budget);
void setExpenses(double* a_expense);
void displayBudget();
void displayLine(int index, double budget, double expense);
bool enterValues(char selection);
};
// IMPLEMENTATION
void MonthlyBudget::setBudget(double* a_budget){budget = a_budget;}
void MonthlyBudget::setExpenses(double* a_expense){expense = a_expense;}
void MonthlyBudget::displayBudget()
{
double total_budget{0}, total_expenses{0}, total_difference{0};
std::cout
<< " *** BUDGET ****\n"
<< "Category Budget Expense Diff\n"
<< "=============================================\n";
for(int i = 0; i < LIMIT; i++)
{
total_budget += budget[i];
total_expenses += expense[i];
total_difference = total_expenses - total_budget;
displayLine(i, budget[i], expense[i]);
}
std::cout
<< "=============================================\n";
displayLine(LIMIT, total_budget,total_expenses);
std::cout << '\n';
};
void MonthlyBudget::displayLine(int index, double budget, double expense)
{
std::cout
<< std::setw(15) << std::left << category[index]
<< std::right << std::setw(10) << std::fixed << std::setprecision(2)
<< budget
<< std::right << std::setw(10) << std::fixed << std::setprecision(2)
<< expense
<< std::right << std::setw(10) << std::fixed << std::setprecision(2)
<< expense - budget
<< '\n';
}
bool MonthlyBudget::enterValues(char selection)
{
double* array = nullptr;
std::string type;
switch( toupper(selection) )
{
case 'B':
type = "budget";
array = budget;
break;
case 'E':
type = "expenses";
array = expense;
break;
default:
std::cout << "*** Invalid budget/expense selection\n";
return false;
}
std::cout << "Enter " << type << " items:\n";
for(int i = 0; i < LIMIT; i++)
{
std::cout
<< "Enter " << category[i]
<< " (current amount $" << array[i] << ") ";
std::cin >> array[i];
}
std::cout << '\n';
return true;
}
int main()
{
MonthlyBudget monthly;
// WITH ALREADY PREPARED NUMBERS
double budget[]
{580, 150, 65, 50, 250, 30, 100, 150, 75, 50};
double expenses[]
{580,130, 50,50 ,230.0, 30,100,120, 100,30};
monthly.setBudget(budget);
monthly.setExpenses(expenses);
monthly.displayBudget();
// OR DO IT YOURSELF
monthly.enterValues('b'); // budget
monthly.enterValues('E'); // expenses
monthly.displayBudget();
return 0;
}
| |