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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
// code for a simple bank account program that has 10 accounts as objects
// in an array and will be required to give an a display menu
// with a few options (check balance, deposit, etc.) and randomly generate
// the balances/interest rates.
// I just need a help understanding how the array works with objects in them
// and how to use functions and variables in them.
// If anyone is available for a little while later tonight about 3 hours
// from this post i can send you a little money for a small tutoring lesson
// or if anyone could help for free that would be awesome
#include <iomanip>
#include <iostream>
#include <random>
#include <string>
struct Account {
std::string id;
double balance;
double rate;
};
constexpr int TOTAL_ACCOUNTS = 10;
Account mockAccountGen(Account& acco);
std::string embellishString(std::string& str);
size_t findAccount();
void displayBalance(Account* accounts, size_t index);
void displayID(Account* accounts, size_t index);
void displayRate(Account* accounts, size_t index);
int main()
{
Account accounts[TOTAL_ACCOUNTS] = {};
for(auto& acco : accounts) {
mockAccountGen(acco);
}
// Display menu
int choice {};
do {
std::cout << "\nMock bank account\n";
std::cout << "=================\n";
std::cout << "1) Check balance of your account\n";
std::cout << "2) Print out your account ID\n";
std::cout << "3) Are you curious about interest rate?\n";
std::cout << "4) Exit\nYour choice? ";
std::cin >> choice;
std::cin.ignore();
size_t index {};
switch (choice) {
case 1:
index = findAccount();
displayBalance(accounts, index);
break;
case 2:
index = findAccount();
displayID(accounts, index);
break;
case 3:
index = findAccount();
displayRate(accounts, index);
break;
case 4:
std::cout << "Goodbye, pal...\n";
break;
default:
std::cout << "Choices are from 1 to 4. Need glasses?\n";
break;
}
} while(choice != 4);
return 0;
}
Account mockAccountGen(Account &acco)
{
std::random_device randev{};
std::default_random_engine generator{randev()};
// Generate mock balance in range -1000 - 20000.
std::uniform_real_distribution<> distrBal(-1000, 20000);
acco.balance = distrBal(generator);
// Generate mock interest rate in range 1 - 2
std::uniform_real_distribution<> distrRate(1, 2);
acco.rate = distrRate(generator);
// Generate mock id in range 100000 - 999999 adding a dot
// and a hyphen inside.
std::uniform_int_distribution<> distrId(100000, 999999);
int tmpid = distrId(generator);
acco.id = std::to_string(tmpid);
embellishString(acco.id);
return acco;
}
// Not optimized: dot and hyphen can follow each other.
std::string embellishString(std::string& str)
{
std::random_device randev{};
std::default_random_engine generator{randev()};
std::uniform_int_distribution<> distrDotsHyphs(1, str.length()-2);
str.insert(distrDotsHyphs(generator), 1, '.');
str.insert(distrDotsHyphs(generator), 1, '-');
return str;
}
size_t findAccount()
{
std::cout << "\nPlease, insert your account ID, dot and hyphen included: ";
std::string tmp;
std::getline(std::cin, tmp);
std::cout << "Sorry: no corrispondance for \"" << tmp
<< "\".\nA random one will be choosen :-)\n";
std::random_device randev{};
std::default_random_engine generator{randev()};
std::uniform_int_distribution<> distrInd(0, TOTAL_ACCOUNTS-1);
return size_t(distrInd(generator));
}
void displayBalance(Account* accounts, size_t index)
{
std::cout << "Your current balance is: " << std::fixed
<< std::setprecision(2) << accounts[index].balance << '\n';
}
void displayID(Account* accounts, size_t index)
{
std::cout << "Your current ID is: " << accounts[index].id << '\n';
std::cout << "\n\n(I know, you've been asked of your ID to get your ID...\n"
"Looks really what a Bank could do, doesn't it?)\n\n";
}
void displayRate(Account* accounts, size_t index)
{
std::cout << "Your current interest rate is: " << std::fixed
<< std::setprecision(2) << accounts[index].rate << '\n';
}
|