interestcalculator/Project2/Menu.cpp
2023-04-11 18:40:15 -07:00

157 lines
4.8 KiB
C++

/*
* Cody Cook
* Project 2
* SNHU
* 2023/04/02
*/
#include <string>
#include <iomanip>
#include <iostream>
#include "Interest.h"
// surround text with a character, add buffer when needed
void surroundText(std::string t_text, int t_lineLength, char t_dataInputLine, bool t_bothTopBottom)
{
if (t_text.length() % 2 == t_lineLength % 2)
{
t_text = " " + t_text + " ";
}
else
{
t_text = " " + t_text + " ";
}
if (t_bothTopBottom)
{
std::cout << std::string(t_lineLength, t_dataInputLine) << std::endl;
}
std::cout << std::string((t_lineLength - t_text.length()) / 2, t_dataInputLine) << t_text << std::string((t_lineLength - t_text.length()) / 2, t_dataInputLine) << std::endl;
if (t_bothTopBottom)
{
std::cout << std::string(t_lineLength, t_dataInputLine) << std::endl;
}
}
// add a separator line
void separator(int t_lineLength, char t_dataInputLine, bool t_emptyLine = true)
{
if (t_emptyLine)
{
std::cout << t_dataInputLine << std::string(t_lineLength - 2, ' ') << t_dataInputLine << std::endl;
}
else
{
std::cout << std::string(t_lineLength, t_dataInputLine) << std::endl;
}
}
// print details out to screen
void printDetails(unsigned int t_year, double t_yearEndBalance, double t_interestEarned)
{
std::string tabs = "\t\t\t $";
std::cout << t_year << tabs;
std::cout << std::fixed << std::setprecision(2);
std::cout << t_yearEndBalance << tabs;
std::cout << t_interestEarned << std::endl;
}
// print out the bank header
void printHeader() {
unsigned int lineLength = 35;
std::string bankName = "Airgead Banking";
char dataInputLine = '#';
// print out the headers
surroundText(bankName, lineLength, dataInputLine, true);
separator(lineLength, dataInputLine, true);
}
void menu() {
// set some default variables
int menuOption = 0;
double initialInvestment = 0;
double monthlyDeposit = 0;
double interestRate = 0;
int numberOfYears = 0;
std::string invalidInput = "Invalid input. Please try again:";
// until the menu is -1, loop through the menu
while (menuOption != -1)
{
unsigned int lineLength = 35;
const std::string DATA_INPUT = "Data Input";
char dataInputLine = '#';
// output the step
surroundText(DATA_INPUT, lineLength, dataInputLine, false);
// start collecting user input
std::cout << "Initial Investment:\t$";
while (!(std::cin >> initialInvestment))
{
std::cout << invalidInput;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
std::cout << "Monthly Deposit:\t$";
while (!(std::cin >> monthlyDeposit))
{
std::cout << invalidInput;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
std::cout << "Interest Rate:\t\t%";
while (!(std::cin >> interestRate))
{
std::cout << invalidInput;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
std::cout << "Number of Years:\t ";
while (!(std::cin >> numberOfYears))
{
std::cout << invalidInput;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
// setup myInterest
auto myInterest = Interest(initialInvestment, monthlyDeposit, interestRate, numberOfYears);
system("pause");
system("cls");
// show the balances without the monthly payment
myInterest.calculateBalance(false);
// show the balances with the monthly payment
myInterest.calculateBalance(true);
system("pause");
// find out what user wants to do now
std::cout << std::endl << std::endl << "To enter new values, press 1. To exit, press 2: ";
while (!(std::cin >> menuOption))
{
// not a valid option
std::cout << invalidInput;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
if (menuOption == 1)
{
// clear the screen and start over again
system("cls");
printHeader();
}
else if (menuOption == 2)
{
// exit the program
menuOption = -1;
}
else
{
//ask the user to try again with a valid input
std::cout << "Invalid input. Please try again: ";
}
}
}