Creating a banking system in C++. Requirements: • Create two structures and name
ID: 3878298 • Letter: C
Question
Creating a banking system in C++.
Requirements:
• 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()
* The function shall prompt the user for an account name, interest rate, and starting balance in that order
* The prompts shall appear as follows:
Let’s set up your account.
First, what’s the name of the account?
What is the interest rate of your [NAME] account?
Finally, what is the starting balance of your [NAME] account? $
· [NAME] shall be replaced with the name of the account
* You may NOT assume that the name of the account contains only a single word
* You may assume the user will always type an interest rate as a decimal value (e.g., 0.05)
* You may also assume that a positive and valid (whole number or no more than two decimal places) amount of money will be entered
– 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)
• A sample run of your program shall look like this:
NOTE: “Savings,” “Checking,” & “Roth IRA” are the values of the Account data member name for each Account object NOTE: All money values are printed using one of the overloaded print() functions
In Savings, you have $50.00.
In Checking, you have $10.00.
First, what’s the name of the account? Roth IRA
What is the interest rate of your Roth IRA account? 0.07
Finally, what is the starting balance of your Roth IRA account? $555.55
In your Roth IRA, you have $555.55.
$50.00 deposited into Savings
You now have $100.00 in your Savings account.
$14.99 withdrawn from Checking.
Withdrew $14.99 for HBO Now, you now have ($4.99)
At 7.00%, your Roth IRA account earned $38.89.
You now have $594.44 in your Roth IRA.
At 7.00%, your Roth IRA account earned $41.61.
You now have $636.05 in your Roth IRA.
An unexpected expense appears! $200.00
Attempting to withdraw from Savings.
$150.00 withdrawn from Savings.
It’s not very effective.
You now have ($50.00) in your Savings account.
Attempting some bad withdraws and deposits
Cannot make negative deposit.
Cannot make negative withdrawl.
0.00 withdrawn fromm Roth IRA.
• You must use both overloads of the print() function in your main() function
Explanation / Answer
Solution:
code:
#include <iostream>
#include <cmath>
#define MAX_SIZE 50
using namespace std;
// create a struct for Money which contains variables for dollars and cents
struct Money {
int dollars;
int cents;
};
// create class Account which contains variables for Account amount, interest rate and account name.
struct Account {
Money money;
double interestRate;
char name[MAX_SIZE];
};
// createAccount() is used to setup a new account from user.
Account createAccount() {
Account acc;
cout<<" Let's setup your account.";
cout<<" First, what's the name of the account? ";
cin.getline(acc.name, MAX_SIZE, ' ');
cout<<" What is the interest rate of your "<<acc.name<<" account? ";
cin>>acc.interestRate;
cout<<" Finally, what's the starting balance of your "<<acc.name<<" account? $";
double balance;
cin>>balance;
// after getting the decimal amount from the user, we need to create a new Money object
Money mon;
// For xample, if the user has entered 100.5, then dollars = 100 and cents = 0.5 * 1100 = 50
mon.dollars = floor(balance);
mon.cents = (balance - mon.dollars)*100;
acc.money = mon;
return acc;
}
// deposit() function is used to deposit a given money into the account.
Account deposit(Account acc, Money deposit) {
// if the amount to be deposited is -ve, print an error message
if(deposit.dollars < 0 || deposit.cents < 0) {
cout<<" Sorry, cannot deposit negative amount!!";
return acc;
}
// else add the money into the account
acc.money.dollars += deposit.dollars;
acc.money.cents += deposit.cents;
// if the value of cents becomes more than 100, increment dollar value by 1
// and set to cents value to cents % 100.
// ex., if new amount becomes 100.140, then dollars = 101 and cents = 40
if(acc.money.cents >= 100) {
acc.money.cents %= 100;
acc.money.dollars += 1;
}
cout<<" $"<<deposit.dollars<<"."<<deposit.cents<<" deposited into "<<acc.name;
return acc;
}
// withdraw() function is used to withdraw a given money from the account.
Money withdraw(Account &acc, Money withdraw) {
// if the amount to be withdrawn is -ve, print an error message
if(withdraw.dollars < 0 || withdraw.cents < 0) {
cout<<" Sorry, cannot withdraw negative amount!!";
withdraw.dollars = 0;
withdraw.cents= 0;
return withdraw;
}
// else withdraw the given amount
acc.money.dollars -= withdraw.dollars;
acc.money.cents -= withdraw.cents;
// if the value of cents becomes less than 100, decrement dollar value by 1
// and set to cents value to cents + 100.
// ex., if new amount becomes 100.-40, then dollars = 99 and cents = 60
if(acc.money.cents < 100) {
acc.money.cents += 100;
acc.money.dollars -= 1;
}
cout<<" $"<<withdraw.dollars<<"."<<withdraw.cents<<" withdrawn from "<<acc.name;
return withdraw;
}
// accrue() method is used to display the account details.
void accrue(Account &acc) {
cout<<" At "<<acc.interestRate<<"%, your "<<acc.name<<" account earned $"
<<acc.money.dollars<<"."<<acc.money.cents;
}
void print(Account acc) {
cout<<"$"<<acc.money.dollars<<"."<<acc.money.cents;
}
void print(Money money) {
cout<<"$"<<money.dollars<<"."<<money.cents;
}
int main () {
Account account = createAccount();
Money dep;
dep.dollars = 45;
dep.cents = 50;
cout<<" Trying to deposit ";
print(dep);
cout<<"....";
account = deposit(account, dep);
dep.dollars = -9;
dep.cents = 50;
cout<<" Trying to deposit ";
print(dep);
cout<<"....";
account = deposit(account, dep);
Money with;
with.dollars = 10;
with.cents = 80;
cout<<" Trying to withdraw ";
print(with);
cout<<"....";
with = withdraw(account, with);
with.dollars = -9;
with.cents = 50;
cout<<" Trying to withdraw ";
print(with);
cout<<"....";
with = withdraw(account, with);
accrue(account);
}
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.