I am taking my first course in C++ and been struggling with the recent arrays and structures stuff. Your help will be greatly appreciated.
This program must have 3 files:
1. Account.h which is the interface of the Account class.
2. Account.cpp which is the Account class implementation file.
3. AccountMain.cpp which is the Account class main function file.
The header file was given like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
class Account
{
private:
//Private Data Members :
int accountId; //Account ID
double balance; //Current Balance of the account
double annualInterestRate; //Current Annual Interest Rate of the Account (ex. 4.5)
public:
//Public Member Functions : (Constructors)
Account(); //Default Constructor (no parameters)
Account(int id, double bal, double interest); //Three-Parameter Constructor
//Public Member Functions : (Setters)
void setAccountId(int x); //Function sets id to x
void setBalance(double x); //Function sets balance to x
void setInterest(double x); //Function sets annualInterestRate to x
//Public Member Functions : (Getters)
int getAccountId(); //Function returns accountId
double getBalance(); //Function returns balance
double getInterest(); //Function returns annualInterestRate
//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.
};
|
The problem says: Write a 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
o Ask the user for the amount they wish to deposit
• Enter 2 to make a withdraw
o Ask the user for the amount they wish to withdraw
o Return if withdrawal was successful or unsuccessful depending on function return value
• Enter 3 to check balance
o Display the account’s current balance
• Enter 4 to check interest rate
o Display the account’s monthly and yearly interest rate
• Enter 5 to display account summary
o Display account id, balance, monthly interest rate, and monthly interest amount
• Enter -1 to return to the main menu
o This will return the user to the main menu prompting to select an account number
Here is a picture of an example output
http://s28.postimg.org/l7nwda9cd/Untitled.png
[url=
http://postimg.org/image/7185i1yh5/][img]http://s28.postimg.org/7185i1yh5/Untitled.jpg[/img][/url]
Here is my work that is not working and I need to turn it in by tomorrow night:
Account.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
/////////////
//Account.h//
/////////////
#include <iostream>
#include <cassert>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;
class Account
{
private:
//Private Data Members :
int accountId; //Account ID
double balance; //Current Balance of the account
double annualInterestRate; //Current Annual Interest Rate of the Account (ex. 4.5)
public:
//Public Member Functions : (Constructors)
Account(); //Default Constructor (no parameters)
Account(int id, double bal, double interest); //Three-Parameter Constructor
//Public Member Functions : (Setters)
void setAccountId(int x); //Function sets id to x
void setBalance(double x); //Function sets balance to x
void setInterest(double x); //Function sets annualInterestRate to x
//Public Member Functions : (Getters)
int getAccountId(); //Function returns accountId
double getBalance(); //Function returns balance
double getInterest(); //Function returns annualInterestRate
//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.
};
|
Account.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
///////////////
//Account.cpp//
///////////////
#include <iostream>
#include <cassert>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include "Account.h"
using namespace std;
int id; double bal; double interest;
float mInterestRate; double yInterestRate; double mInterest;
Account accnt[10];
void setAccountId(int x)
{
cout << "\nPlease enter an account number from 0 - 9" << endl;
cin >> id;
id = x;
}
int getAccountId()
{
cout << "Account ID: " << id;
return id;
}
void setBalance(double x)
{
x = rand() % 20000 + 10000.00;
bal = x;
}
void deposit(double amount)
{
cout << "Enter amount to be deposited: " << endl;
cin >> amount;
bal += amount;
}
double getBalance()
{
cout << "\nCurrent Balance: $" << bal << endl;
return bal;
}
void setInterest(double x)
{
x = rand() % 5 + 1.5;
interest = x;
}
double getInterest()
{
cout << "Yearly interest rate: " << interest << "%" << endl;
return interest;
}
double getMonthlyInterestRate()
{
mInterestRate = interest / 12;
cout << "Monthly interest rate: " << mInterestRate << "%" << endl;
return mInterestRate;
}
double getMonthlyInterest()
{
mInterest = mInterestRate*bal;
cout << "Monthly interest: $" << mInterest << endl;
return mInterest;
}
bool withdraw(double amount)
{
if (bal >= amount)
{
bal -= amount;
return true;
}
else
{
return false;
}
}
void start()
{
cout << "\nPlease enter an account number from 0 - 9" << endl;
cin >> id;
}
void options(Account accnt[], int id, double bal, double interest)
{
int option;
cout << "\nEnter 1 to make a deposit";
cout << "Enter 2 to make a withdraw";
cout << "Enter 3 to check balance";
cout << "Enter 4 to check interest rate";
cout << "Enter 5 to display account summary";
cout << "Enter -1 to return to the main menu";
cin >> option;
if (option == 1)
{
deposit(bal);
options(accnt, id, bal, interest);
}
else if (option == 2)
{
withdraw(bal);
options(accnt, id, bal, interest);
}
else if (option == 3)
{
getBalance();
options(accnt, id, bal, interest);
}
else if (option == 4)
{
cout << "\nMonthly interest rate: " << mInterestRate;
cout << "Yearly interest rate: " << yInterestRate;
options(accnt, id, bal, interest);
}
else if (option == 5)
{
cout << "\nAccount ID: " << id;
cout << "Current Balance: " << bal;
cout << "Monthly interest rate: " << mInterestRate;
cout << "Monthly interest amount: " << mInterest;
options(accnt, id, bal, interest);
}
else if (option == -1)
{
start();
options(accnt, id, bal, interest);
}
else
{
cout << "\nInvalid Selection." << endl;
options(accnt, id, bal, interest);
}
}
|