Write a C++ program that creates an array of 10 Account objects. When creating e
ID: 3810290 • Letter: W
Question
Write a C++ program that creates an array of 10 Account objects.
When creating each Account object use the following guidelines:
Each object’s accountId should be the index of its position within the array.
The balance of each Account object should be created from a random number generator function returning values between 10,000.00 and 20,000.00. This return value should be rounded to two decimal places.
The interest rate of each Account object should be created from a random number generator function returning values between 1.5 and 5.0. This return value should be rounded to one decimal place.
The program should then ask the user to select an account number from 0 – 9 or -1 to exit the program.
If an account number is selected, then the user should be presented with some options. The five main bullets should form the menu presented. If a user makes any of the top five selections, then the menu should be represented. The menu should only not be represented with an entry of -1.
Enter 1 to make a deposit
Ask the user for the amount they wish to deposit
Enter 2 to make a withdraw
Ask the user for the amount they wish to withdraw
Return if withdrawal was successful or unsuccessful depending on function return value
Enter 3 to check balance
Display the account’s current balance
Enter 4 to check interest rate
Display the account’s monthly and yearly interest rate
Enter 5 to display account summary
Display account id, balance, monthly interest rate, and monthly interest amount
Enter -1 to return to the main menu
This will return the user to the main menu prompting to select an account number
Explanation / Answer
Account class :
#include <iostream>
#include <string>
using namespace std;
class Account
{
public:
int id;
double bal;
double rate;
Account(int i,double balance, double r) //constructor to receive initial balance
{
setBalance(balance); //validate and store initial balance
id = i;
rate = r;
}
//validates that the initial balance is greater than 0 dollars
void setBalance(double balance)
{
bal = balance;
}
double deposit(double amt ) //adds an amount to the initial balance
{
bal += amt;
cout << "Amount Deposited Sucessfully!!." << endl;
}
double withDraw(double amt ) //subtracts money from the initial balance
{
if(amt > bal){
cout << "debit amount exceeded account balance." << endl;
}
else{
cout << "Amount WithDrawn Sucessfully!!." << endl;
bal -= amt;
}
}
double getBalance() //returns the current account balance
{
return bal;
}
void setRate(double r)
{
rate = r;
}
double getRate()
{
return rate;
}
int getId()
{
return id;
}
};
Driver class :
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <math.h>
#include <vector>
#include "Account.h"
using namespace std;
int main()
{
vector<Account> acc;
double min = 20000.0;
double max = 40000.0;
srand(static_cast <unsigned int> (time(0)));
for(int i = 0;i < 10; i++ )
{
double range = max - min;
double d = RAND_MAX / range;
double randNum = min + (rand() / d);
double bal = roundf(randNum * 100.0f) / 100.0f;
cout << bal << endl;
range = (5.0 - 1.5);
double div = RAND_MAX / range;
double r = (1.5 + (rand() / div));
double y = roundf(r * 100.0f) / 100.0f;
cout << y << endl;
Account account(i,bal,y);
acc.push_back(account);
}
int flag = 1;
do{
cout << "Enter Account number(0-9) or -1 to exit" << endl;
int i;
cin >> i;
if(i <0 || i> 9)
{
cout << "Invalid account number" << endl;
}
else if(i==-1){
flag = 0;
}
else{
int iFlag = 1;
do{
cout << "Menu:(Enter 1-5 or -1 to select another account)" << endl;
cout << "1. Enter 1 for deposit" << endl;
cout << "2. Enter 2 for withdraw:" << endl;
cout << "3. Enter 3 to check balance" << endl;
cout << "4. Enter 4 for rate of intrest" << endl;
cout << "5. Enter 5 for Account summary" << endl;
int m;
cin >> m;
if(m == -1){
iFlag = 0;
}
else if(m<1||m>5){
cout << "Invalid input!! Enter 1-5 or -1 to exit" cout << endl;;
}
else{
switch(m){
case 1:
cout << "Enter amount to deposit:";
double a;
cin >> a;
acc.at(i).deposit(a);
cout << endl;
break;
case 2:
cout << "Enter amount to withdraw:";
double w;
cin >> w;
acc.at(i).withDraw(w);
cout << endl;
break;
case 3:
cout << "Your balance is:" << acc.at(i).getBalance() << endl;
break;
case 4:
cout << "Your rate of intrest is:" << acc.at(i).getRate()<< endl;
break;
case 5:
cout << "Account summary:" << endl;
cout << "Account No:" << i << endl;
cout << "Account bal:" << acc.at(i).getBalance() << endl;
cout << "Monthly rate:" << acc.at(i).getRate()<< endl;
cout << "Monthly intrest amount:" << acc.at(i).getRate() * acc.at(i).getBalance() << endl;
break;
}
}
}while(iFlag);
}
}while(flag);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.