Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This exercise is mainly concerned with understanding how program flow works in o

ID: 3757373 • Letter: T

Question

This exercise is mainly concerned with understanding how program flow works in object-oriented programs, i.e. object interaction based on messages. Each class must be separated into a header and source file. Tests and the main program must also be in their own source files. Use the format skeleton from exercise collection 1 as a template. Use the const and constexpr keywords correctly To emulate transfers between bank accounts the functions of an ATM have been analyzed and the following classes identified account, database, and interaction. An account should have attributes for account number (id_), a secret personal identifi- cation number (pin), and an account balance (balance_) Exercise 2.1 Implement the methods account: :pay in and account: :pay_out. Money values should be represented with a floating-point number (float). A strict separation between pay-in and pay-out is required, which means the argument for both functions cannot be negative. If a method fails, the object state shall be unchanged and the failure signaled by a return value of false. [4.5 pt Exercise 2.2 Test account: :pay_in and account::pay out by implementing a test program, i.e. a main function in a separate source file. 1.5pt] Exercise 2.3 Implement a freestanding function transfer. Use the methods from exercise 2.1. The function declaration should be located near the class definition of account 3 pt] Hint: The arguments of the transfer function follow the source/sink semantic in the ordering from-to Exercise 2.4 Implement a database that contains a list of accounts. The number of accounts should be unlimited, in principle, but at the current time a simplified (prototype) solution is of more interest (see source code below). The account class must be extended to fulfill the requirement of default-constructible. Also extend account with a valid attribute because initialized accounts usually do not contain valid data. 3pt A possible simplified version of database class database public: // c'tors & d'tor database() : size (O) public: // methodas

Explanation / Answer

//account.h

#ifndef ACCOUNT
#define ACCOUNT
#include<iostream>
using namespace std;

class account{
  
   public:
      
   int id_; //Account number
   int pin_;           //Personal Idenrification Number
   float balance_;     //Account balance
  
  
      
   account(); //Constructor
   account(int id_,int pin_,float balance_); //Constructor
      
   int pay_in(float);
   int pay_out(float);
  
  
   void display();  
};

void transfer(account ,account, float); //Excercise 2.3

#endif

//account.cpp


#include<iostream>
#include"account.h"

account::account()
{
   id_ = pin_ = balance_ = 0;
}

account::account(int id_,int pin_,float balance_)
{
   this->id_= id_ ;
   this->pin_ =pin_;
   this->balance_ = balance_;
}

//Excercise 2.1  

int account::pay_in(float x)   //pay_in() method
{
   if(x<0)
   return 0;
   else
   {  
   balance_+= x;
   return 1;
   }
}

int account::pay_out(float y)     //pay_out() method
{
   if(y<0)
   return 0;
   else
   {
  
   balance_-= y;
   return 1;
    }
}

void account::display()
{
cout<<"ID is:"<<id_<<endl;
cout<<"PIN is:"<<pin_<<endl;
cout<<"Balance is:"<<balance_<<endl;  
cout<<" ";
}

void transfer(account from, account to, float amount)
{
   if(amount>from.balance_)
   cout<"Insuffiecient funds";
   else
   {
       from.pay_out(amount);
       to.pay_in(amount);
   }
}

int main()    //Excercise 2.2
{
   account a1(101,1234,100.00);
  
   a1.pay_in(20);
   a1.display();
   a1.pay_out(20);
   a1.display();

return 0;      
}

//database.h

#include "account.h"
#include<iostream>

class database    //Excercise 2.4
{
  
   private:
   account account_[100];
   unsigned size_;
   public:
  
   database();
   ~database();
   void append(account const& );
  
   account* find(database,int) const; //Excercise 2.5
   account* find(database, int);
  
   };

//database.cpp

#include "database.h"

#include<iostream>

database::database()     //Constructor
   {
      size_=0;
   }
  
database::~database(){   }    //Destructor

void database::append(account const& a){
       account_[size_]=a;
       ++size_;
      
   }

account* database::find(database obj1,int var) const
{
int i;
for(i=0;i<size_;i++)
{
   if(obj1.account_[i].id_==var)
   return &account_[i];
   else
   return NULL;
}

}  

account* database::find(database obj1,int var)
{
int i;
for(i=0;i<size_;i++)
{
   if(obj1.account_[i].id_==var)
   return &account_[i];
   else
   return NULL;
}

int main()   

//we are using mutiple main() functions here. It may show error because we have used one main() in

// account.cpp .we acan avoid it by combining the main() in account.cpp and main() in database.cpp

// I did not combine since it was requested in the question to write main() seperately in the source file // for each class. Please look into it
{
   account a1(111,2978,1000);
  
   database d1;
  
   d1.append(a1);
  
   return 0;
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote