Question

In: Computer Science

using C++ Please create the class named BankAccount with the following properties below: // The BankAccount...

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

Solutions

Expert Solution

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.


Related Solutions

In C++, define the class bankAccount to implement the basic properties of a bank account. An...
In C++, define the class bankAccount to implement the basic properties of a bank account. An object of this class should store the following data: Account holder’s name (string), account number (int), account type (string, checking/saving), balance (double), and interest rate (double). (Store interest rate as a decimal number.) Add appropriate member functions to manipulate an object. Use a static member in the class to automatically assign account numbers. Also declare an array of 10 components of type bankAccount to...
c++ programming 1.1 Class definition Define a class bankAccount to implement the basic properties of a...
c++ programming 1.1 Class definition Define a class bankAccount to implement the basic properties of a bank account. An object of this class should store the following data:  Account holder’s name (string)  Account number (int)  Account type (string, check/savings/business)  Balance (double)  Interest rate (double) – store interest rate as a decimal number.  Add appropriate member functions to manipulate an object. Use a static member in the class to automatically assign account numbers. 1.2 Implement...
The Account class Create a class named Account, which has the following private properties:
in java The Account class Create a class named Account, which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNumber (), getBalance (), setBalan newBalance). There is no setNumber () once an account is created, its account number cannot change. Now implement these methods: void deposit (double amount) and void withdraw (double amount). For both these methods, if the amount is less than...
The Account class Create a class named Account , which has the following private properties:
 The Account class Create a class named Account , which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNunber(), getBalance(), setBalance (double newBalance) . There is no setNunber() - once an account is created, its account number cannot change. Now implement these methods: void deposit (double anount) and void withdraw(double anount). For both these methods, if the amount is less than zero,...
The Account class Create a class named Account, which has the following private properties: number: long...
The Account class Create a class named Account, which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNumber(), getBalance(), setBalance(double newBalance). There is no setNumber() -- once an account is created, its account number cannot change. Now implement deposit(double amount) and withdraw(double amount) methods. If the amount is less than zero,...
The Account class Create a class named Account, which has the following private properties: number: long...
The Account class Create a class named Account, which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNumber(), getBalance(), setBalance(double newBalance). There is no setNumber() -- once an account is created, its account number cannot change. Now implement deposit(double amount) and withdraw(double amount) methods. If the amount is less than zero,...
The Account class Create a class named Account, which has the following private properties: number: long...
The Account class Create a class named Account, which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNumber(), getBalance(), setBalance(double newBalance). There is no setNumber() -- once an account is created, its account number cannot change. Now implement these methods: void deposit(double amount) and void withdraw(double amount). For both these methods,...
Using C# Create a class named Inches To Feet. Its Main()method holds an integer variable named...
Using C# Create a class named Inches To Feet. Its Main()method holds an integer variable named inches to which you will assign a value. Create a method to which you pass inches. The method displays inches in feet and inches. For example, 67 inches is 5 feet 7 inches.
Create a class using C# named InchesToFeet. Its Main()method holds an integer variable named inches to...
Create a class using C# named InchesToFeet. Its Main()method holds an integer variable named inches to which you will assign a value. Create a method to which you pass inches. The method uses 2 ref parameters: feet, inches left of type int and a parameter that is not ref of type int to which you pass inchesinches. For example, 67 inches is 5 feet 7 inches.
(In Matlab) Create a base class named Point that has the properties for x and y...
(In Matlab) Create a base class named Point that has the properties for x and y coordinates. From this class derive a class named Circle having an additional property named radius. For this derived class, the x and y properties represent the center coordinates of a circle. The methods of the base class should consist of a constructor, an area function that returns 0, and a distance function that returns the distance between two points. The derived class should have...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT