#include "make_withdraw_2.h"

// Author: Steve Talbot

make_withdraw_2:: make_withdraw_2(){
}

make_withdraw_2:: ~make_withdraw_2(){
}

bool make_withdraw_2:: make_withdraw( Data* data_ ){

	// this method overrides the default method "make_withdraw()" from the
	// superclass "make_withdraw_1" ..

	// checking done previously in Account to insure that withdraw amount
	// is greater than the minimum ..

	AccountInfo* accountInfo = data_->getCurrentAccountInfo();

	int currentBalance = accountInfo->getB();

	int withdrawAmount = data_->getW();

	int result = ( currentBalance - withdrawAmount );

	// check to see if new “result” is less than the min balance,
	// if so, change “overdrawn” account parameter to indicate
	// that state is now “overdrawn” ..

	if( result < data_->getMinBalance() ) {

		// this indicates that the new “result” changes the current state
		// from “not overdrawn” to “overdrawn”  ..

		if( accountInfo->getIsOverdrawn() == false )
			accountInfo->setIsOverdrawn( true );

	}

	// update the “account” with the new balance ..
	accountInfo->setB( result );

	// indicate that this change to the "account info" object means
	// that this object needs to be written to file during the logout operation ..
	if ( data_->getNeedsToBeFlushedToFile( accountInfo->getID() ) == false )
		data_->setNeedsToBeFlushedToFile( accountInfo->getID(), true );

	if( accountInfo->getIsOverdrawn() == true )
		cout << "This \"withdraw\" transaction makes the account overdrawn." << endl;

	return true;

}