// CreditCardAccount.cpp
#include "stdafx.h" 
#include "CreditCardAccount.h"

int CreditCardAccount::numberOfAccounts = 0;

// #using <mscorlib.dll>
using namespace System;

CreditCardAccount::CreditCardAccount(long number, double limit)
{
    accountNumber = number;
    creditLimit = limit;
    currentBalance = 0.0;
	numberOfAccounts++;
    Console::Write("Number of accounts created: ");
    Console::WriteLine(numberOfAccounts);
	ptrLoyaltyScheme = 0;
}

CreditCardAccount::~CreditCardAccount()
{
	Console::Write("Account being destroyed: ");
	Console::WriteLine(accountNumber);
	Console::Write("Closing balance: ");
	Console::WriteLine(currentBalance);
	delete ptrLoyaltyScheme;
}

bool CreditCardAccount::MakePurchase(double amount)
{
    if (currentBalance + amount > creditLimit)
    { return false; }
    else
    {
        currentBalance += amount;

        // If current balance is 50% (or more) of credit limit...
        if (currentBalance >= creditLimit / 2)
        {
            // If LoyaltyScheme object doesn’t exist yet... 
            if (ptrLoyaltyScheme == 0)
            {
                // Create it
                ptrLoyaltyScheme = new LoyaltyScheme();
            }
            else
            {
                // LoyaltyScheme already exists, so accrue bonus points
                ptrLoyaltyScheme->EarnPointsOnAmount(amount);
            }
        }
        return true;
    }
}

void CreditCardAccount::MakeRepayment(double amount)
{
    currentBalance -= amount;
}

void CreditCardAccount::PrintStatement() 
{
    Console::Write("Account number: ");
    Console::WriteLine(accountNumber);

    Console::Write("Current balance: ");
    Console::WriteLine(currentBalance);
}

int CreditCardAccount::GetNumberOfAccounts()
{
    return numberOfAccounts;
}

void CreditCardAccount::RedeemLoyaltyPoints()
{
    // If the LoyaltyScheme object doesn’t exist yet...
    if (ptrLoyaltyScheme == 0)
    {
        // Display an error message
        Console::WriteLine("Sorry, you do not have a loyalty scheme yet");
    }
    else
    {
        // Tell the user how many points are currently available
        Console::Write("Points available: ");
        Console::WriteLine(ptrLoyaltyScheme->GetPoints());
		// Ask the user how many points they want to redeem
        Console::WriteLine("How many points do you want to redeem? ");
        String ^ input = Console::ReadLine();
		int points = System::Convert::ToInt32(input);
        // Redeem the points
        ptrLoyaltyScheme->RedeemPoints(points);
        // Tell the user how many points are left
        Console::Write("Points remaining: ");
        Console::WriteLine(ptrLoyaltyScheme->GetPoints());
    }
}

