A2/atmFunctions.cpp

96 lines
No EOL
2.4 KiB
C++

#include "atmFunctions.h"
#include <iomanip>
#include <iostream>
#include <limits>
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, &cents);
break;
case '2':
deposit_money(&dollars, &cents);
break;
case '3':
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 newDollars = 0, newCents = 0;
std::cout << "How many dollars would you like to deposit? ";
std::cin >> newDollars;
std::cout << "How many cents would you like to deposit? ";
std::cin >> newCents;
// Ensure valid positive integers were entered
if (newDollars < 0 || newCents < 0) {
std::cout << "Deposits must be positive amounts, please try again with "
"proper values"
<< std::endl
<< std::endl;
}
// Add dollar and cent values to variables passed-by-pointer
*pDollars += newDollars;
*pCents += newCents;
// Condense cent amounts > 100 into dollars
*pDollars += *pCents / 100;
*pCents = *pCents % 100;
// Display the amount deposited nicely
std::cout << "You have successfully deposited a total of $"
<< (newDollars + (newCents / 100)) << "." << std::setfill('0')
<< std::setw(2) << newCents % 100 << "." << std::endl
<< std::endl;
}