Question

In: Computer Science

1.   Design a class called BankAccount. The member fields of the class are: Account Name, Account...

1.   Design a class called BankAccount. The member fields of the class are: Account Name, Account Number and Account Balance. There are also other variables called MIN_BALANCE=9.99, REWARDS_AMOUNT=1000.00, REWARDS_RATE=0.04. They look like constants, but for now, they are variables of type double
Here is the UML for the class:
                                                        BankAccount
-string accountName // First and Last name of Account holder
-int accountNumber // integer
-double accountBalance // current balance amount
+ BankAccount()                     //default constructor that sets name to “”, account number to 0 and balance to 0
+BankAccount(string accountName, int accountNumber, double accountBalance)   // regular constructor
+getAccountBalance(): double // returns the balance
+getAccountName: string // returns name
+getAccountNumber: int
+setAccountBalance(double amount) : void
+withdraw(double amount) : bool //deducts from balance and returns true if resulting balance is less than minimum balance
+deposit(double amount): void //adds amount to balance. If amount is greater than rewards amount, calls
// addReward method
-addReward(double amount) void // adds rewards rate * amount to balance
+toString(): String   // return the account information as a string with three lines. “Account Name: “ name
                                                                                                                      “Account Number:” number
                                                                                                                      “Account Balance:” balance

2.   Create a file called BankAccount.cpp which implements the BankAccount class as given in the UML diagram above. The class will have member variables( attributes/data) and instance methods(behaviours/functions that initialize, access and process data)

3.   Create a driver class to do the following:
a.   Declare and instantiate a bank account called accountZero using the default constructor
b.   Declare and instantiate a bank account called accountOne with name= “Matilda Patel” number =1232, balance=-4.00
c.   Declare and instantiate a bank account called accountTwo with name = “Fernando Diaz”, number=1234, balance=250
d.   Declare and instantiate a bank account called accountThree with name=”Howard Chen”, number=1236, balance = 194.56
e.   Display the bank accounts in the three line format as above
f.   Deposit 999 dollars in Fernando’s account and 1000.25 in Howards account
g.   Display their account information
h.   Withdraw 10000 from Matildas account and 90 dollars from Feranandos account
i.   Display the results. If withdrawal is not possible your program should say “Insufficient funds” otherwise it should say “Remaining Balance :” balance amount
j.   Print the total amount of all the bank accounts created.

Solutions

Expert Solution

Thanks for the question.

Here is the completed code for this problem.

Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change.

Notes : Refer the screenshot for the files you need to create, there will be in total 3 files for this project.

If you are satisfied with the solution, please rate the answer.

Thanks
===========================================================================

#ifndef BANKACCOUNT_H
#define BANKACCOUNT_H
#include <string>
using std::string;


class BankAccount
{
   public:
      
       BankAccount();
       BankAccount(string,int,double);
       double getAccountBalance() const;
       string getAccountName() const;
       int getAccountNumber() const;
       void setAccountBalance(double);
       bool withdraw(double);
       void deposit(double);
       void addReward(double);
       string toString() const;
      
       static double rate;
      
   private:
       string accountName;
       int accountNumber;
       double accountBalance;
      
};

#endif

================================================================

#include "BankAccount.h"
#include <string>
#include <sstream>
using namespace std;

double BankAccount::rate =0.0;
BankAccount::BankAccount():accountName(""),accountNumber(0),accountBalance(0.0){
  
}
BankAccount::BankAccount(string name,int number ,double balance):
   accountName(name),accountNumber(number), accountBalance(balance){
      
   }
double BankAccount::getAccountBalance() const{
   return accountBalance;
}
string BankAccount::getAccountName() const{
   return accountName;
}
int BankAccount::getAccountNumber() const{
   return accountNumber;
}
void BankAccount::setAccountBalance(double balance){
   accountBalance=balance;
}
bool BankAccount::withdraw(double amount){
   if(amount>accountBalance){
       return false;
   }
   accountBalance-=amount;
   return true;
}
void BankAccount::deposit(double amount){
   accountBalance+=amount;
}
void BankAccount::addReward(double amount){
   accountBalance+=amount*rate;
}
string BankAccount::toString() const{
   string accountNum ,accountBal;
   stringstream ss;
   ss<<accountNumber;
   ss>>accountNum;
   ss.clear();
   ss<<accountBalance;
   ss>>accountBal;
   string desc = "Account Name: "+accountName + ", Acccount Number: "+ accountNum +
   ", Account Balance: $"+accountBal;
  
   return desc;
  
}

================================================================

#include <iostream>
#include "BankAccount.h"
using namespace std;


int main(){
  
   //a. Declare and instantiate a bank account called accountZero using the default constructor
   BankAccount account = BankAccount();
   cout<<account.toString()<<endl;
  
   //b. Declare and instantiate a bank account called accountOne with name= “Matilda Patel” number =1232, balance=-4.00
   BankAccount mat = BankAccount("Matilda Patel",1232,-4.00);
  
  
   //c. Declare and instantiate a bank account called accountTwo with name = “Fernando Diaz”, number=1234, balance=250
   BankAccount fer = BankAccount("Fernando Diaz",1234,250);
  
  
   // d. Declare and instantiate a bank account called accountThree with name=”Howard Chen”, number=1236, balance = 194.56
   BankAccount how = BankAccount("Howard Chen",1236,194.56);
  
  
   //e. Display the bank accounts in the three line format as above
   cout<<mat.toString()<<endl;
   cout<<fer.toString()<<endl;
   cout<<how.toString()<<endl;
  
   //f. Deposit 999 dollars in Fernando’s account and 1000.25 in Howards accoun
   fer.deposit(999);
   how.deposit(1000.25);
  
   //g. Display their account information
   cout<<mat.toString()<<endl;
   cout<<fer.toString()<<endl;
   cout<<how.toString()<<endl;
  
   //h. Withdraw 10000 from Matildas account and 90 dollars from Feranandos account
   bool flag= mat.withdraw(10000);
   if(!flag){
       cout<<"Insufficient funds.\n";
   }else{
       cout<<mat.toString()<<endl;
   }
   flag = fer.withdraw(90);
   if(!flag){
           cout<<"Insufficient funds.\n";
   }else{
       cout<<fer.toString()<<endl;
   }
  
   //j. Print the total amount of all the bank accounts created.
   double total = mat.getAccountBalance()+fer.getAccountBalance()+how.getAccountBalance();
   cout<<"Total Balance: $"<<total<<endl;

}

================================================================


Related Solutions

Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE-...
Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE- class-level attribute initialized to 1000 -acctID: int -bank: String -acctType: String (ex: checking, savings) -balance: Float METHODS: <<Constructor>>Account(id:int, bank: String, type:String, bal:float) +getAcctID(void): int                        NOTE: retrieving a CLASS-LEVEL attribute! -setAcctID(newID: int): void           NOTE: PRIVATE method +getBank(void): String +setBank(newBank: String): void +getBalance(void): float +setBalance(newBal: float): void +str(void): String NOTE: Description: prints the information for the account one item per line. For example: Account #:        ...
Design a Ship class that has the following members: • A member variable for the name...
Design a Ship class that has the following members: • A member variable for the name of the ship (a string) • A member variable for the year that the ship was built (a string) • A constructor and appropriate accessors and mutators • A virtual print function that displays the ship’s name and the year it was built. Design a CruiseShip class that is derived from the Ship class. The CruiseShip class should have the following members: • A...
Design a class named Pet, which should have the following fields: Name – The name field...
Design a class named Pet, which should have the following fields: Name – The name field holds the name of a pet. Type – The type field holds the type of animal that is the pet. Example values are “Dog”, “Cat”, and “Bird”. Age – The age field holds the pet’s age. The Pet class should also have the following methods: setName – The setName method stores a value in the name field. setType – The setType method stores a...
Java... Design a Payroll class with the following fields: • name: a String containing the employee's...
Java... Design a Payroll class with the following fields: • name: a String containing the employee's name • idNumber: an int representing the employee's ID number • rate: a double containing the employee's hourly pay rate • hours: an int representing the number of hours this employee has worked The class should also have the following methods: • Constructor: takes the employee's name and ID number as arguments • Accessors: allow access to all of the fields of the Payroll...
Design a ship class that has the following data fields: A data field for the name...
Design a ship class that has the following data fields: A data field for the name of the ship (a string). A data field for the year that the ship was built (a String). A constructor and appropriate accessors and mutators. A toString method that displays the ship’s name and the year it was built. Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following member: A field for the maximum number of passengers...
(Java) Design a class named Person with fields for holding a person’s name, address, and telephone...
(Java) Design a class named Person with fields for holding a person’s name, address, and telephone number. Write one or more constructors and the appropriate mutator and accessor methods for the class’s fields. Next, design a class named Customer, which extends the Person class. The Customer class should have a field for a customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write one or more constructors and the appropriate mutator and...
Design a class named Person with fields for holding a person's name, address, and telephone number(all...
Design a class named Person with fields for holding a person's name, address, and telephone number(all as Strings). Write a constructor that initializes all of these values, and mutator and accessor methods for every field. Next, design a class named Customer, which inherits from the Person class. The Customer class should have a String field for the customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write a constructor that initializes these...
Design a class named Person with fields for holding a person's name, address, and telephone number(all...
Design a class named Person with fields for holding a person's name, address, and telephone number(all as Strings). Write a constructor that initializes all of these values, and mutator and accessor methods for every field. Next, design a class named Customer, which inherits from the Person class. The Customer class should have a String field for the customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write a constructor that initializes these...
JAVA/Netbeans •Design a class named Person with fields for holding a person’s name, address and phone...
JAVA/Netbeans •Design a class named Person with fields for holding a person’s name, address and phone number (all Strings) –Write a constructor that takes all the required information. –Write a constructor that only takes the name of the person and uses this to call the first constructor you wrote. –Implement the accessor and mutator methods. Make them final so subclasses cannot override them –Implement the toString() method •Design a class named Customer, which extends the Person class. It should have...
Write a class called Account that contains: • Three privateinstance variables: name (String), account (String),...
Write a class called Account that contains: • Three private instance variables: name (String), account (String), account balance (double). • One constructor, which constructs all instances with the values given. • Getters and setters for all the instance variables. • Debit function that takes an amount from the user and subtract the balance (make sure the balance will not drop below zero after the operation, if this was the case, the function should return false else it should return true)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT