This commit is contained in:
Tyler Beckman 2024-09-22 20:25:00 -06:00
parent 68e45dd3b5
commit bf1c77bd06
Signed by: Ty
GPG key ID: 2813440C772555A4
3 changed files with 114 additions and 112 deletions

View file

@ -11,142 +11,144 @@
* @param pDollars A pointer to the amount of dollars * @param pDollars A pointer to the amount of dollars
* @param pCents A pointer to the amount of cents * @param pCents A pointer to the amount of cents
*/ */
void normalize_dollars(int* pDollars, int* pCents) { void normalize_dollars(int *pDollars, int *pCents) {
*pDollars += *pCents / 100; *pDollars += *pCents / 100;
*pCents = *pCents % 100; *pCents = *pCents % 100;
} }
void start_atm() { void start_atm() {
int dollars = 0; int dollars = 0;
int cents = 0; int cents = 0;
std::cout << "You have entered the ATM menu" << std::endl; std::cout << "You have entered the ATM menu" << std::endl;
bool continuing = true; bool continuing = true;
char choice; char choice;
do { do {
print_menu(); print_menu();
choice = get_user_selection(); choice = get_user_selection();
switch (choice) { switch (choice) {
case '1': case '1':
print_balance(&dollars, &cents); print_balance(&dollars, &cents);
break; break;
case '2': case '2':
deposit_money(&dollars, &cents); deposit_money(&dollars, &cents);
break; break;
case '3': case '3':
withdraw_money(&dollars, &cents); withdraw_money(&dollars, &cents);
break; break;
case 'Q': case 'Q':
continuing = false; continuing = false;
std::cout << "Have a nice day!" << std::endl; std::cout << "Have a nice day!" << std::endl;
break; break;
} }
} while (continuing); } while (continuing);
} }
void print_menu() { void print_menu() {
std::cout << "Please make a selection:" << std::endl std::cout << "Please make a selection:" << std::endl
<< "(1) Print Current Balance" << std::endl << "(1) Print Current Balance" << std::endl
<< "(2) Deposit" << std::endl << "(2) Deposit" << std::endl
<< "(3) Withdraw" << std::endl << "(3) Withdraw" << std::endl
<< "(Q) Quit" << std::endl; << "(Q) Quit" << std::endl;
} }
char get_user_selection() { char get_user_selection() {
char input = 'Q'; char input = 'Q';
while (true) { while (true) {
std::cout << "Choice: "; std::cout << "Choice: ";
std::cin >> input; std::cin >> input;
std::cin.clear(); std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (std::cin.fail() || if (std::cin.fail() ||
!(input == '1' || input == '2' || input == '3' || input == 'Q')) { !(input == '1' || input == '2' || input == '3' || input == 'Q')) {
std::cout << "Invalid choice, please try again" << std::endl; std::cout << "Invalid choice, please try again" << std::endl;
} else { } else {
std::cout << std::endl; std::cout << std::endl;
return input; return input;
} }
}; };
} }
void print_balance(int* pDollars, int* pCents) { void print_balance(int *pDollars, int *pCents) {
std::cout << "Your account currently contains a total of $" << *pDollars std::cout << "Your account currently contains a total of $" << *pDollars
<< "." << std::setfill('0') << std::setw(2) << *pCents << "." << "." << std::setfill('0') << std::setw(2) << *pCents << "."
<< std::endl << std::endl
<< std::endl; << std::endl;
} }
void deposit_money(int* pDollars, int* pCents) { void deposit_money(int *pDollars, int *pCents) {
// Prompt the user for the amount to deposit // Prompt the user for the amount to deposit
int dollarsToDeposit = 0, centsToDeposit = 0; int dollarsToDeposit = 0, centsToDeposit = 0;
std::cout << "How many dollars would you like to deposit? "; std::cout << "How many dollars would you like to deposit? ";
std::cin >> dollarsToDeposit; std::cin >> dollarsToDeposit;
std::cout << "How many cents would you like to deposit? "; std::cout << "How many cents would you like to deposit? ";
std::cin >> centsToDeposit; std::cin >> centsToDeposit;
// Ensure valid positive integers were entered // Ensure valid positive integers were entered
if (dollarsToDeposit < 0 || centsToDeposit < 0) { if (dollarsToDeposit < 0 || centsToDeposit < 0) {
std::cout << "Deposits must be positive amounts, please try again with " std::cout << "Deposits must be positive amounts, please try again with "
"proper values" "proper values"
<< std::endl << std::endl
<< std::endl; << std::endl;
return; return;
} }
// Normalize the user-inputted values for later display // Normalize the user-inputted values for later display
normalize_dollars(&dollarsToDeposit, &centsToDeposit); normalize_dollars(&dollarsToDeposit, &centsToDeposit);
*pDollars += dollarsToDeposit; *pDollars += dollarsToDeposit;
*pCents += centsToDeposit; *pCents += centsToDeposit;
// Normalize the true account values after addition // Normalize the true account values after addition
normalize_dollars(pDollars, pCents); normalize_dollars(pDollars, pCents);
// Display the amount deposited nicely // Display the amount deposited nicely
std::cout << "You have successfully deposited a total of $" std::cout << "You have successfully deposited a total of $"
<< dollarsToDeposit << "." << std::setfill('0') << std::setw(2) << dollarsToDeposit << "." << std::setfill('0') << std::setw(2)
<< centsToDeposit << "." << std::endl << centsToDeposit << "." << std::endl
<< std::endl; << std::endl;
} }
void withdraw_money(int* pDollars, int* pCents) { void withdraw_money(int *pDollars, int *pCents) {
// Prompt the user for the amount to withdraw // Prompt the user for the amount to withdraw
int dollarsToWithdraw = 0, centsToWithdraw = 0; int dollarsToWithdraw = 0, centsToWithdraw = 0;
std::cout << "How many dollars would you like to withdraw? "; std::cout << "How many dollars would you like to withdraw? ";
std::cin >> dollarsToWithdraw; std::cin >> dollarsToWithdraw;
std::cout << "How many cents would you like to withdraw? "; std::cout << "How many cents would you like to withdraw? ";
std::cin >> centsToWithdraw; std::cin >> centsToWithdraw;
if (centsToWithdraw < 0 || dollarsToWithdraw < 0) { if (centsToWithdraw < 0 || dollarsToWithdraw < 0) {
std::cout << "Withdrawals must be positive amounts, please try again with " std::cout
"proper values" << "Withdrawals must be positive amounts, please try again with "
<< std::endl "proper values"
<< std::endl; << std::endl
return; << std::endl;
} return;
}
normalize_dollars(&dollarsToWithdraw, &centsToWithdraw); normalize_dollars(&dollarsToWithdraw, &centsToWithdraw);
// Convert dollar amounts to cents for easier subtraction // Convert dollar amounts to cents for easier subtraction
int totalCentWithdrawal = centsToWithdraw + (dollarsToWithdraw * 100); int totalCentWithdrawal = centsToWithdraw + (dollarsToWithdraw * 100);
*pCents += *pDollars * 100; *pCents += *pDollars * 100;
*pDollars = 0; *pDollars = 0;
if (totalCentWithdrawal > *pCents) { if (totalCentWithdrawal > *pCents) {
std::cout << "You do not have enough money for this withdrawal" << std::endl std::cout << "You do not have enough money for this withdrawal"
<< std::endl; << std::endl
return; << std::endl;
} return;
}
*pCents -= totalCentWithdrawal; *pCents -= totalCentWithdrawal;
normalize_dollars(pDollars, pCents); normalize_dollars(pDollars, pCents);
std::cout << "You have successfully withdrawn a total of $" std::cout << "You have successfully withdrawn a total of $"
<< dollarsToWithdraw << "." << std::setfill('0') << std::setw(2) << dollarsToWithdraw << "." << std::setfill('0') << std::setw(2)
<< centsToWithdraw << "." << std::endl << centsToWithdraw << "." << std::endl
<< std::endl; << std::endl;
} }

View file

@ -29,7 +29,7 @@ char get_user_selection();
* @param dollars The amount of full dollars the user currently has * @param dollars The amount of full dollars the user currently has
* @param cents The amount of cents the user currently has * @param cents The amount of cents the user currently has
*/ */
void print_balance(int* pDollars, int* pCents); void print_balance(int *pDollars, int *pCents);
/** /**
* @brief Prompts the user an amount to deposit, validates it, and modifies the * @brief Prompts the user an amount to deposit, validates it, and modifies the
@ -38,7 +38,7 @@ void print_balance(int* pDollars, int* pCents);
* @param dollars The amount of full dollars the user currently has * @param dollars The amount of full dollars the user currently has
* @param cents The amount of cents the user currently has * @param cents The amount of cents the user currently has
*/ */
void deposit_money(int* pDollars, int* pCents); void deposit_money(int *pDollars, int *pCents);
/** /**
* @brief Prompts the user an amount to withdraw, validates it, and modifies the * @brief Prompts the user an amount to withdraw, validates it, and modifies the
@ -47,6 +47,6 @@ void deposit_money(int* pDollars, int* pCents);
* @param dollars The amount of full dollars the user currently has * @param dollars The amount of full dollars the user currently has
* @param cents The amount of cents the user currently has * @param cents The amount of cents the user currently has
*/ */
void withdraw_money(int* pDollars, int* pCents); void withdraw_money(int *pDollars, int *pCents);
#endif // ATMFUNCTIONS_H #endif // ATMFUNCTIONS_H

View file

@ -1,6 +1,6 @@
#include "atmFunctions.h" #include "atmFunctions.h"
int main() { int main() {
start_atm(); start_atm();
return 0; return 0;
} }