In: Computer Science
using C++ Please create the class named BankAccount with the following properties below:
// The BankAccount class sets up a clients account (using name & id), and - keeps track of a user's available balance. - how many transactions (deposits and/or withdrawals) are made.
public class BankAccount { private int id; private String name private double balance; private int numTransactions;
// ** Please define method definitions for Accessors and Mutators functions below for the class private member variables string getName(); // No set method required. It will be set using contructors int getId(); // No set method required. It will be set using contructors double getBalance(); // No set Method required. It will be initialized with constructor and then by Deposit method. int getNumTransactions(); void setNumTransactions(); // Used to reset number of transactions
// Additional methods public void deposit(double amount) // Adds amount to balance. Note that this will counts as 1 transaction. public void withdraw(double amount) // Subtracts the amount from the balance (also counts as 1 transaction). ** Remember to add check for sufficient funds public void printOutput(); // This will output to screen, the account info: name/ID, balance and number of transactions.
// ** Please also define constructors for this class (the default constructor AND parameterized Constructors) // These will be used to create an object with the given name and id and initial balance (this should set numTransactions to zero). public BankAccount(string name, int id, double balance); // Client account is created and an initial balance is set. public BankAccount(string name, int id); // This is the case where the client creates an account and initial balance will be zero. public BankAccount(); // Default constructor - this should initialize values as per normal. }
For this project, you are to: 1) Create the above class definition along with the method definitions 2) You should validate any data coming from the user. (ie. the amount parameter should be positive, the client id should be in the range 1..9999 3) You need to create a main () function which will test your BankAccount Class. For testing your class you should creat multiple clients and call all the methods with different inputs. You should also test your error conditions - insufficient funds, or bad "amount" provided. When testing, you may have to modify your Main() function to modify test data slightly, recompile and rerun your program for each test condition OR you can do it all at once. 4) Please add comments as appropriate, and use good coding styles (proper indenting and naming conventions as has been discussed in class). THE TOP OF FILE SHOULD CONTAIN YOUR NAME, CLASS/SECTION and brief description of your class
Below is example of some class invocations. BankAccount savings("John", 1234, 50.00); // Creates an account for John, with $50 initial deposit. Note that numTransactions should be set to zero as well.
savings.deposit(10.00); savings.deposit(50.00); savings.deposit(10.00); savings.deposit(70.00); savings.withdraw(100.00); Account info: John, 1234 has a Balance = $90, with 5 transactions
Code:
#include <iostream>
# include <string>
using namespace std;
class BankAccount {
private:
int id;
string name;
double balance;
int numTransactions;
public:
BankAccount(string name, int id,
double balance){
if(id>0 && id<=9999
&& balance>0){
this->id =
id;
this->name =
name;
this->balance
= balance;
this->numTransactions = 0;
}
else{
cout<<"ERROR: Improper details"<<endl;
}
}
string getName(){
return
name;
}
int getId(){
return id;
}
double getBalance(){
return
balance;
}
int getNumTransactions(){
return
numTransactions;
}
void setNumTransactions(){
this->numTransactions = 0;
}
void deposit(double amount){
if(amount>0){
this->balance = this->balance +
amount;
this->numTransactions =
this->numTransactions+1;
}
else
cout<<"ERROR: Amount is less than
0"<<endl;
}
void withdraw(double amount){
if(amount>0){
if(amount<balance){
this->balance =
this->balance - amount;
this->numTransactions =
this->numTransactions+1;
}
else
cout<<"ERROR:
Insufficient balance"<<endl;
}
else
cout<<"ERROR: Amount is less than
0"<<endl;
}
void printOutput(){
cout<<"Account info: "<<this->name<<",
"<<this->id<<" has a Balnce =
$"<<this->balance<<", with
"<<this->numTransactions<<"
transactions"<<endl;
}
};
int main(){
BankAccount savings("John", 1234, 50.00);
savings.deposit(10.00);
savings.printOutput();
savings.deposit(-50.00); //test case for
negetive balance
savings.printOutput();
savings.deposit(50.00);
savings.printOutput();
savings.deposit(10.00);
savings.deposit(70.00);
savings.withdraw(100.00);
savings.printOutput();
BankAccount savings2("John", -1234, 50.00);
//test case for negetive id
BankAccount savings3("John", 12000, 50.00);
//test case for id>9999
BankAccount savings4("John", 1234, -50.00);
//test case for negetive balance
}
Output:
Hope this helps.