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

~~~USING C# ONLY~~~ Create a console application Design a class named Person with properties for holding...
~~~USING C# ONLY~~~ Create a console application Design a class named Person with properties for holding a person’s name, address, and telephone number. Design a class named Customer, which is derived from the Person class. The Customer class should have the variables and properties for the customer number, customer email, a spentAmount of the customer’s purchases, and a Boolean variable indicating whether the customer wishes to be on a mailing list. It also includes a function named calcAmount that calculates...
Using C#: Write a class named Employee that has the following properties: Name - The Name...
Using C#: Write a class named Employee that has the following properties: Name - The Name property holds the employee's name IdNumber - The IdNumber property holds the employee's ID number Department - The Department property holds the name of the department in which the employee works Position - The Position property holds the employee's job title The class should have the following overloaded constructors: A constructor that accepts the following values as arguments and assigns them to the appropriate...
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...
Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of...
Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of the account holder. a method, getBalance, that returns a double corresponding to the account balance. a method withdraw that accepts a double, and deducts the amount from the account balance. Write a class definition for a subclass, CheckingAccount, that contains: a boolean instance variable, overdraft. (Having overdraft for a checking account allows one to write checks larger than the current balance). a constructor that...
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,...
In C# thanks please, Design a class named Person with properties for holding a person’s name,...
In C# thanks please, Design a class named Person with properties for holding a person’s name, address, and telephone number. Design a class named Customer, which is derived from the Person class. The Customer class should have the variables and properties for the customer number, customer email, a spentAmount of the customer’s purchases, and a Boolean variable indicating whether the customer wishes to be on a mailing list. It also includes a function named calcAmount that calculates the spentAmount. All...
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,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT