Grading rubric: Indent code and insert comments to document your program. [10 pt
ID: 3891095 • Letter: G
Question
Grading rubric:
Indent code and insert comments to document your program. [10 pts]
Program must be implemented and run as instructed. [80 pts]
To receive the maximum of 80 points, this program must separate the
Account class implementation from the main function file and, in addition, the Account class
must have its interface (Account.h) in a separate file from its implementation.
The code in the following files:
AccountMain.cpp
Account.cpp
Account.h -- provided in the attached file.
Please use these file names exactly. [10 pts]
Summary
The lab requires you to create two files Account.cpp and AccountMain.cpp. You will end up with Account.h, Account.cpp and AccountMain.cpp.
Account Class Definition:
Class Account
Private Data Members:
static int genId
int accountId
double balance
double annualInterestRate
Static data member used to assign value to accountId;
Account ID
Current Balance of the account
Current Annual Interest Rate of the Account (ex. 4.5)
Public Member Functions:
Account()
Account(int id, double bal, double interest)
Constructors:
Default Constructor (no parameters)
Overloaded Constructor: Three-Parameter Constructor
Public Member Functions:
void setAccountId (int x)
void setBalance(double x)
void setInterest(double x)
Setters:
Function sets id to x
Function sets balance to x
Function sets annualInterestRate to x
Public Member Functions:
int getAccountId()
double getBalance()
double getInterest()
static double getGenId()
Getters:
Function returns accountId
Function returns balance
Function returns annualInterestRate
Static Function used to returns genId
Public Member Functions:
double getMonthlyInterestRate()
Function calculates the monthly interest rate and returns the value
double getMonthlyInterest()
Function calculates the amount earned per month from interest and returns the value rounded to 2 decimal places. (Assume interest is not compounding)
Bool withdraw(double amount)
Function only allows withdraw if the current balance is greater than or equal to the withdraw amount. Return true if withdrawal is successful, else return false.
void deposit(double amount)
Function adds the amount passed as a parameter to the current balance.
Write a program that creates an array of 10 Account objects.
When creating each Account object, make sure the object is initialized in both the default and overloaded constructor functions with following guidelines:
Each object’s accountId should be the index of its position within the array. Add a static data member to initialize the accountId. (10%)
The balance of each Account object should be created from a random number generator function returning values between 100.00 and 200.00. (5%)
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. (5%)
Main Program and remaining member function defintions: (80%)
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 the following options:
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 99 to exit to the main menu to select another account number.
Class Account
Private Data Members:
static int genId
int accountId
double balance
double annualInterestRate
Static data member used to assign value to accountId;
Account ID
Current Balance of the account
Current Annual Interest Rate of the Account (ex. 4.5)
Public Member Functions:
Account()
Account(int id, double bal, double interest)
Constructors:
Default Constructor (no parameters)
Overloaded Constructor: Three-Parameter Constructor
Public Member Functions:
void setAccountId (int x)
void setBalance(double x)
void setInterest(double x)
Setters:
Function sets id to x
Function sets balance to x
Function sets annualInterestRate to x
Public Member Functions:
int getAccountId()
double getBalance()
double getInterest()
static double getGenId()
Getters:
Function returns accountId
Function returns balance
Function returns annualInterestRate
Static Function used to returns genId
Public Member Functions:
double getMonthlyInterestRate()
Function calculates the monthly interest rate and returns the value
double getMonthlyInterest()
Function calculates the amount earned per month from interest and returns the value rounded to 2 decimal places. (Assume interest is not compounding)
Bool withdraw(double amount)
Function only allows withdraw if the current balance is greater than or equal to the withdraw amount. Return true if withdrawal is successful, else return false.
void deposit(double amount)
Function adds the amount passed as a parameter to the current balance.
Explanation / Answer
// File name: Account.h
#ifndef _ACCOUNT
#define _ACCOUNT
using namespace std;
// Class Account definition
class Account
{
private:
// Data members
static int genId;
int accountId;
double balance;
double annualInterestRate;
public:
// Prototype of member functions
Account();
Account(int id, double bal, double interest);
void setAccountId (int x);
void setBalance(double x);
void setInterest(double x);
int getAccountId();
double getBalance();
double getInterest();
int getGenId();
double getMonthlyInterestRate();
double getMonthlyInterest();
bool withdraw(double amount);
void deposit(double amount);
};// End of class
// Initializes static data member
int Account::genId = 0;
#endif // End of header file
------------------------------------------------------------------------------------------------------
// File name: Account.cpp
#include "Account.h"
#include <iostream>
using namespace std;
// Default constructor definition
Account::Account()
{
accountId = 0;
balance = 0;
annualInterestRate = 0;
// Increases the static data member by one for each account created
genId++;
}// End of default constructor
// Parameterized constructor definition
Account::Account(int id, double bal, double interest)
{
accountId = id;
balance = bal;
annualInterestRate = interest;
genId++;
}// End of parameterized constructor
// Function to set account id
void Account::setAccountId (int id)
{
accountId = id;
}// End of function
// Function to set balance
void Account::setBalance(double bal)
{
balance = bal;
}// End of function
// Function to set interest
void Account::setInterest(double inte)
{
annualInterestRate = inte;
}// End of function
// Function to return account id
int Account::getAccountId()
{
return accountId;
}// End of function
// Function to return balance
double Account::getBalance()
{
return balance;
}// End of function
// Function to return interest
double Account::getInterest()
{
return annualInterestRate;
}// End of function
// Function to return static id
int Account::getGenId()
{
return genId;
}// End of function
// Function to calculate and return monthly interest rate
double Account::getMonthlyInterestRate()
{
return annualInterestRate / 12.0;
}// End of function
// Function to calculate and return monthly interest amount
double Account::getMonthlyInterest()
{
return balance * getMonthlyInterestRate() / 100.0;
}// End of function
// Function to return true if amount is less than balance otherwise returns false
bool Account::withdraw(double amount)
{
// Checks if current balance is greater than or equals to amount then return true
if(balance >= amount)
return true;
// Otherwise return false
else
return false;
}// End of function
// Function to add deposit amount to current balance
void Account::deposit(double amount)
{
balance += amount;
}// End of function
-------------------------------------------------------------------------------------------------
// File name: AccountMain.cpp
#include "Account.cpp"
#include <iostream>
#include <stdlib.h>
#include <cstdlib>
#include <ctime>
using namespace std;
// Function to generate and return random number between 100 and 200 for balance
double getRandomBalance()
{
return rand()%((200 - 100) + 1);
}// End of function
// Function to generate and return random number between 2 and 5 for interest rate
double getRandomInterestRate()
{
return rand()%((5 - 2) + 1);;
}// End of function
// Function to display menu, accept choice and return choice
int menu()
{
// To store choice
int ch;
// Displays menu
cout<<" 1 Deposit";
cout<<" 2 Withdraw";
cout<<" 3 Check balance";
cout<<" 4 Check interest rate";
cout<<" 5 Display account summary";
// Accepts choice
cout<<" Enter your choice: ";
cin>>ch;
// Returns choice
return ch;
}// End of function
// main function definition
int main()
{
// generates random seed val
srand(time(NULL));
// Declare an array of pointer of size 10
Account *acc[10];
double amt;
int accNo = 101;
// Loops 10 times to create 10 objects using parameterized constructor
for(int c = 0; c < 10; c++)
acc[c] = new Account(accNo++, getRandomBalance(), getRandomInterestRate());
// Loops till user choice for account number is not -1
do
{
// Accepts account number
cout<<" Select an account number from 0 - 9 or -1 to exit.";
cin>>accNo;
// Checks if account index is -1 then stop
if(accNo == -1)
break;
// Calls the function for user choice and checks the choice
// Calls the appropriate function based on the user choice
switch(menu())
{
case 1:
cout<<" Enter the amount to deposit: ";
cin>>amt;
acc[accNo]->deposit(amt);
break;
case 2:
cout<<" Enter the amount to withdraw: ";
cin>>amt;
if(acc[accNo]->withdraw(amt))
acc[accNo]->setBalance(acc[accNo]->getBalance() - amt);
else
cout<<" Insufficient balance.";
break;
case 3:
cout<<" Current balance: "<<acc[accNo]->getBalance();
break;
case 4:
cout<<" Yearly Interest Rate: "<<acc[accNo]->getInterest()<<" Monthly Interest Rate: "<<acc[accNo]->getMonthlyInterest();
break;
case 5:
cout<<" Account Number: "<<acc[accNo]->getAccountId();
cout<<" Account Balance: "<<acc[accNo]->getBalance();
break;
default:
cout<<" Invalid choice.";
}// End of switch case
}while(1); // End of do - while loop
}// End of main function
Sample Output:
Select an account number from 0 - 9 or -1 to exit.1
1 Deposit
2 Withdraw
3 Check balance
4 Check interest rate
5 Display account summary
Enter your choice: 1
Enter the amount to deposit: 200
Select an account number from 0 - 9 or -1 to exit.3
1 Deposit
2 Withdraw
3 Check balance
4 Check interest rate
5 Display account summary
Enter your choice: 500
Invalid choice.
Select an account number from 0 - 9 or -1 to exit.3
1 Deposit
2 Withdraw
3 Check balance
4 Check interest rate
5 Display account summary
Enter your choice: 1
Enter the amount to deposit: 500
Select an account number from 0 - 9 or -1 to exit.1
1 Deposit
2 Withdraw
3 Check balance
4 Check interest rate
5 Display account summary
Enter your choice: 3
Current balance: 202
Select an account number from 0 - 9 or -1 to exit.1
1 Deposit
2 Withdraw
3 Check balance
4 Check interest rate
5 Display account summary
Enter your choice: 2
Enter the amount to withdraw: 300
Insufficient balance.
Select an account number from 0 - 9 or -1 to exit.1
1 Deposit
2 Withdraw
3 Check balance
4 Check interest rate
5 Display account summary
Enter your choice: 3
Current balance: 202
Select an account number from 0 - 9 or -1 to exit.1
1 Deposit
2 Withdraw
3 Check balance
4 Check interest rate
5 Display account summary
Enter your choice: 5
Account Number: 102
Account Balance: 202
Select an account number from 0 - 9 or -1 to exit.-1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.