Skip to content

Instantly share code, notes, and snippets.

@brianteachman
Last active August 17, 2016 07:23
Show Gist options
  • Save brianteachman/b695cce99a0319d8ccfa45ed78adc46c to your computer and use it in GitHub Desktop.
Save brianteachman/b695cce99a0319d8ccfa45ed78adc46c to your computer and use it in GitHub Desktop.
/*
* CH.10 Structs: Class Exercize
*
* Select the revision tab above for previous versions (github only)
*
* Revision 1: Brian Teachman, Brandon Sobjack, Carolyn Stratford
* Revision 2-7: Brian
*
* 8/16/2016
*/
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct BankAccount
{
string name;
double balance;
int number;
};
const string FILE_DIR = "c:\\cs131_temp\\";
vector<BankAccount> manageAccounts(void);
void addAccount(vector<BankAccount> &accountList);
void withdrawFromAccount(vector<BankAccount> &accountList);
void listAccounts(vector<BankAccount> accountList, bool isMenu=false);
// Default display does include balance info (isMenu=false)
//TODO: Fix layout
void writeToCSV(vector<BankAccount> accountList, string directory);
// Write accountList to file as csv (account_list.csv)
// TODO: log and throw error
int main()
{
vector<BankAccount> accountList = manageAccounts();
//do something with this so called list of accounts
return 0;
}
vector<BankAccount> manageAccounts()
{
vector<BankAccount> list;
char users_selection;
bool done = false;
while (!done)
{
cout << " Select a choice: (L)ist, (A)dd, (W)ithdraw, (S)ave, (Q)uit: ";
cin >> users_selection;
cout << endl;
switch (users_selection)
{
case 'L': case 'l':
listAccounts(list);
break;
case 'A': case 'a':
addAccount(list);
listAccounts(list);
break;
case 'W': case 'w':
listAccounts(list, true); //isMenu=true
withdrawFromAccount(list);
listAccounts(list);
break;
case 'S': case 's':
writeToCSV(list, FILE_DIR);
break;
case 'Q': case 'q':
done = true;
break;
}
}
return list;
}
void addAccount(vector<BankAccount> &accountList)
{
BankAccount account;
cout << " Enter an account name: ";
cin.ignore(); //clean input stream
getline(cin, account.name); //capture proper names
cout << " Enter the starting balance: ";
cin >> account.balance;
account.number = accountList.size() + 1;
accountList.push_back(account);
}
void listAccounts(vector<BankAccount> accountList, bool isMenu)
{
cout << endl;
if (isMenu)
{
cout << " Acc.#" << "\t" << "Name" << endl
<< "------------------------------" << endl;
for (BankAccount account : accountList)
{
cout << account.number << ") \t" << account.name << endl;
}
}
else
{
cout << " Acc.#" << "\t" << "Name" << "\t\t" << "Balance" << endl
<< "-----------------------------------" << endl;
for (BankAccount account : accountList)
{
cout << " " << account.number << "\t" << account.name << "\t";
if (account.name.size() < 8) cout << "\t"; //TODO Fix layout
cout << "$" << static_cast<double>(account.balance) << endl;
}
}
cout << endl;
}
void withdrawFromAccount(vector<BankAccount> &accountList)
{
double amount_to_withdraw = 0;
int account_number;
cout << endl;
cout << " Enter an account number to withdraw from: ";
cin >> account_number;
cout << " Enter the amount to withdraw: ";
cin >> amount_to_withdraw;
for (BankAccount &account : accountList)
{
if (account_number == account.number)
{
BankAccount found;
account.balance -= amount_to_withdraw;
}
}
}
void writeToCSV(vector<BankAccount> accountList, string directory)
{
ofstream accountsFile;
accountsFile.open(directory+"accounts_list.csv");
if (accountsFile.fail())
{
// TODO: log and throw error
exit(1);
}
for (BankAccount account: accountList)
{
accountsFile << account.number << ", " << account.name << ", " << account.balance << "\n";
}
accountsFile.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment