Question

In: Computer Science

Here is the assignment description. * Create a class named 'Account' having the following private attributes...

Here is the assignment description.


* Create a class named 'Account' having the following private attributes


int accountNumber;

double balance;


* Write a constructor with parameters for each of the attributes.

* Write another constructorwith one parameter for the accountNumber.

* Write getter and setter methods for each of the private attributes.

* Write a method void credit(double amount) which adds the given amount to the balance.

* Write a method void debit(double amount) which subtracts the given amount from the balance, if the amount doesn’t exceed the balance. Otherwise, print out the “Amount withdrawn exceeds the current balance!”

* Write a method void print() which prints description for this Account instance, accountNumber value and balance value. Please refer to the sample output to figure out the format of the print.

*Write a method void mergeAccounts(Account other) which adds the balance in “other” account to the balance of the current instance of Account.

Here is a sample main() function.


int main() {

Account a1(8111, 99.99);

a1.print();   

a1.credit(20);

a1.debit(10);

a1.print();   


Account a2(8222);

a2.print();   

a2.setBalance(100);

a2.credit(20);

a2.debit(200);

a2.print();   


a1.mergeAccounts(a2); // I added these statements, please run the code to update the output accordingly.

a1.print();

return 0;

}


Sample output should be like this:


A/C no: 8111 Balance=$99.99

A/C no: 8111 Balance=$109.99

A/C no: 8222 Balance=$0.00

Amount withdrawn exceeds the current balance!

A/C no: 8222 Balance=$120.00

A/C no: 8111 Balance=$229.99

-------

Solutions

Expert Solution

#include<iostream>
#include<iomanip> // FOR 2 DECIMAL POINT
using namespace std;

class Account{
   private:
       int accountNumber;
       double balance;
   public:
       // constructor with 2 attribute
       Account(int accountNumber,double balance){
           this->accountNumber = accountNumber;
           this->balance = balance;
       }
       // construct with one attribute
       Account(int accountNumber){
           this->accountNumber = accountNumber;
           balance= 0.0;
       }
       // getter and setter
       void setAccountNumber(int accountNumber){
           this->accountNumber = accountNumber;
       }
       int getAccountNumber(){
           return accountNumber;
       }
       void setBalance(double balance){
           this->balance = balance;
       }
       double getBalance(){
           return balance;
       }
       // credit method
       void credit(double amount){
           balance+=amount;
       }
       // debit method
       void debit(double amount){
           if(amount > balance){
               cout<<"Amount withdrawn exceeds the current balance!"<<endl<<endl;
           }else{
               balance -= amount;
           }
       }
       // print() method
       void print(){
           cout<<fixed<<showpoint<<setprecision(2);
           cout<<"A/C no: "<<accountNumber<<" Balance=$"<<balance<<endl<<endl;
       }
       void mergeAccounts(Account other){
           this->balance += other.balance;
       }
};
int main(){
   Account a1(8111,99.99);
   a1.print();
   a1.credit(20);
   a1.debit(10);
   a1.print();   
   Account a2(8222);
   a2.print();   
   a2.setBalance(100);
   a2.credit(20);
   a2.debit(200);
   a2.print();   
   a1.mergeAccounts(a2); // I added these statements, please run the code to update the output accordingly.
   a1.print();
   return 0;
}

/* OUTPUT */

/* PLEASE UPVOTE */


Related Solutions

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,...
Challenge: Documents Description: Create a class in Python 3 named Document that has specified attributes and...
Challenge: Documents Description: Create a class in Python 3 named Document that has specified attributes and methods for holding the information for a document and write a program to test the class. Purpose: The purpose of this challenge is to provide experience creating a class and working with OO concepts in Python 3. Requirements: Write a class in Python 3 named Document that has the following attributes and methods and is saved in the file Document.py. Attributes __title is a...
7.3 (The Account class) Design a class named Account that contains: ■ A private int data...
7.3 (The Account class) Design a class named Account that contains: ■ A private int data field named id for the account. ■ A private float data field named balance for the account. ■ A private float data field named annualInterestRate that stores the current interest rate. ■ A constructor that creates an account with the specified id (default 0), initial balance (default 100), and annual interest rate (default 0). ■ The accessor and mutator methods for id, balance, and...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
Design a class named Account that contains: A private int data field named id for the...
Design a class named Account that contains: A private int data field named id for the account. A private double data field named balance for the account. A private double data field named annualInterestRate that stores the current interest rate. A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. The accessor and mutator methods for id, balance, and annualInterestRate. A method named getMonthlyInterestRate() that returns the monthly interest rate. A method named withdraw(amount)...
Design a class named Account that contains: A private String data field named accountNumber for the...
Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account. A constructor...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT