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