We will use structs to represent bank accounts and money respectively, and write
ID: 3895869 • Letter: W
Question
We will use structs to represent bank accounts and money respectively, and write functions that allow us to interact with our accounts.
Requirements:
• Name your source code file program1.cpp
• Create two structures and name the types Account and Money.
• The Money struct has two variables. – One represents how many dollars you have. – The other represents how many cents you have.
• The Account struct has three variables. – A Money struct that will represent how much money is in the account – One variable to represent the interest rate as a decimal value. – The final variable will contain the name of the account. (Checking, Savings, CD, etc.)
• Negative amounts of Money are stored by making both variables of the Money object negative
• Use the following function prototypes
– Account createAccount(std::string file)
* The function shall look for a file matching the name of the parameter that is passed to the function. The file contains the name, interest rate, and starting balance in that order
* You may assume that each of the three items are contained on their own line
* You may NOT assume that the name of the account contains only a single word
* You may also assume that a positive and valid (whole number or no more than two decimal places) amount of money will be contained in the file
* An Account object with the proper values is then returned
* If the file does not exist or otherwise cannot be opened, a default Account with name “Savings,” an interest rate of 1%, and a starting balance of $100.00 is returned
--createAccount(std::string name, double rate, Money balance)
* The function will return an account with the values already set based on the parameters passed
– Account deposit(Account account, Money deposit)
*The function shall not accept negative amounts of money
· If a negative amount of money is attempted to be deposited, an error message will be displayed, and the original account will be returned
*A message shall be printed to the screen that takes the form “$X.XX deposited into [NAME].” only if a successful deposit is made.
· The message appears on its own line
· [NAME] shall be replaced with the name of the account
* The balance of the account shall be updated accordingly
– Money withdraw(Account &account, Money withdraw)
*The function shall not accept negative amounts of money · If a negative amount of money is attempted to be withdrawn, an error message will be displayed, and a Money object equivalent to $0.00 will be returned * You may allow the account to be overdrawn, but by no more than $50.00 *
A message shall be printed to the screen that takes the form “$X.XX withdrawn from [NAME].” whether or not a successful withdrawl is made. · The message appears on its own line · [NAME] shall be replaced with the name of the account * The balance of the account shall be updated accordingly
– void accrue(Account &account)
*A message shall be printed to the screen that takes the form ”At X.XX%, your [NAME] account earned $X.XX.” · The message appears on its own line · [NAME] shall be replaced with the name of the account
– void print([SINGLE PARAMETER]) x2
* The function shall be overloaded to accept either a Money object or an Account object * The functions shall print ONLY the amount of money · This means no extra phrases like ”You have blah blah” *
The amount of money shall be printed with a ‘$’, a decimal point, and only 2 decimal digits * There shall be no extra whitespace before or after the amount of money when it is printed * Negative amounts of money shall be represented in the following manner: ($X.XX)
Hints: • The functions listed in the Requirements are required (shocker!), but you may find it useful to write other “helper” functions • Converting a double to a Money object can cause rounding errors – You may want to look up the round() function • Converting an amount of money to an equivalent amount of pennies makes a lot of logical work go away • Take note of what statements are required to be printed from within functions, the rest are printed in the main() function
Explanation / Answer
#include <iostream>
#include <iomanip>
using namespace std;
struct Money
{
int dollars;
int cents;
};
struct Account
{
Money balance;
double rate;
string name;
};
Account createAccount();
Account deposit(Account &account, Money deposit);
Money withdraw(Account &account, Money withdraw);
void accrue(Account &account);
void print(Account account);
void print(Money m);
//helper functions to convert to / from double to money
Money doubleToMoney(double amount);
double moneyToDouble(Money m);
int main()
{
cout << fixed << setprecision(2) ;
Account s = {{50,0}, 0.03, "Savings"};
Account c = {{10,0}, 0.05, "Checking"};
print(s);
print(c);
Account acc1 = createAccount();
print(acc1);
deposit(s, Money{50,0});
withdraw(c, Money{14, 99});
accrue(acc1);
withdraw(s, Money{150, 0});
Money w = withdraw(acc1, {-50,0});
print(w);
cout << " withdrawn from your " << acc1.name << endl;
return 0;
}
Money doubleToMoney(double amount)
{
Money m;
int cents = amount * 100;
m.dollars = cents / 100;
m.cents = cents % 100;
return m;
}
double moneyToDouble(Money m)
{
return m.dollars + m.cents / 100.0;
}
Account createAccount()
{
Account a;
double bal;
cout << "Let's set up your account." << endl;
cout << "First, what's the name of the account? " ;
getline(cin, a.name);
cout << "What is the interest rate of your " << a.name << " account? ";
cin >> a.rate;
cout << "Finally, what is the starting balance of your " << a. name << " account? $";
cin >> bal;
a.balance = doubleToMoney(bal);
return a;
}
Account deposit(Account &account, Money deposit)
{
if(deposit.dollars < 0 || deposit.cents < 0)
{
cout << "Deposit amount can not be -ve" << endl;
}
else
{
double newBalance = moneyToDouble(account.balance) + moneyToDouble(deposit);
account.balance = doubleToMoney(newBalance);
cout << "$" << moneyToDouble(deposit) << " deposited into " << account.name << endl << endl;
cout << "You now have " ; print(account.balance);
cout << " in your " << account.name << " account " << endl;
}
return account;
}
Money withdraw(Account &account, Money withdraw)
{
if(withdraw.dollars < 0 || withdraw.cents < 0)
{
cout << "Withdrawal amount can not be -ve" << endl;
withdraw = Money{0,0};
}
else
{
cout << "Attempting to withdraw "; print(withdraw) ;
cout << " from " << account.name << endl;
double w = moneyToDouble(withdraw);
double a = moneyToDouble(account.balance);
if(a < w)
{
withdraw = doubleToMoney(a);
}
double newBalance = a - w;
account.balance = doubleToMoney(newBalance);
cout << "$" << moneyToDouble(withdraw) << " withdrawn from " << account.name << endl << endl;
cout << "You now have " ; print(account.balance);
cout << " in your " << account.name << " account " << endl;
}
return withdraw;
}
void accrue(Account &account)
{
double amt = moneyToDouble(account.balance);
double earning = account.rate * amt;
cout << "At " << (account.rate *100)<< "% your " << account.name << " earned $" << earning << endl;
account.balance = doubleToMoney(amt + earning);
}
void print(Account account)
{
cout << " In your " << account.name << ", you have ";
print(account.balance);
cout << endl;
}
void print(Money m)
{
double a = moneyToDouble(m);
if(m.dollars < 0 || m.cents < 0)
{
cout << "($" << -a << ")";
}
else
cout << "$" << a ;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.