% This program requests data and calculates the bank balance,  
% the mortgage payments and balance, 
% the student loan payment and balance and displays the balances.

clc; clear; % clear the console window and the memory

%Set the monthly interest rate
r=0.005;

% Set the maximum number of months allowed
N=360;
% The final balance will be at the beginning of the nth month
n=N+1; 

% 1. INPUT
        
% Request the annual salary
sal1=input('Please enter annual salary: ');

% Request the amount saved monthly
s1=input('Please enter amount saved monthly: ');

% Request the initial amount in the bank account
p1=input('Please enter initial amount in bank account: ');

% Request the initial amount of the student loan
p2=input('Please enter initial amount of student loan: ');

% The regular payment of student loan is computed next using
% as input the annual salary
s2=studentloan3(sal1); 

% Use the matlab function duration1.m to 
% compute the duration nst and final payment sst of the student loan.
[nst, sst]=duration1(p2, s2, r);           

% Request the initial amount of the mortgage   
p3=input('Please enter initial amount of mortgage: ');

% Choose to specify mortgage by regular payment or duration
while 1
  mort=input(...
  'Mortgage payment given (enter 1) or Mortgage duration given (enter 2): ');
  if mort~=1 && mort~=2
    fprintf('..you must enter 1 or 2, you need to try again\n');
  else
    break;
  end
end

% Depending on specification of mortgage ask for further input
if mort==1
  % Request the regular payment amount of the mortgage
  s3=input('Please enter regular mortgage payment: ');
                                           
  % compute the duration nm and final payment sm
  [nm, sm]=duration1(p3, s3, r);   

elseif mort==2
  % Request the duration of the mortgage
  nm=input('Please enter duration of mortgage (in whole months): ');

  % compute the payment s3 per month
  s3=payment1(p3, nm, r); 
  % final mortgage payment
  sm=s3;
end

% 2. COMPUTATION

% allocate space and initialise bank, student and mortgage
% for the initial values and n-1 further values
bank=zeros(1, n);
student=zeros(1, n);
mortgage=zeros(1, n);

% set the 1st amount for bank, student and mortgage
bank(1)=p1;
student(1)=p2; 
mortgage(1)=p3;

% loop through n-1 monthly transactions
for k=1:n-1
    % the bank account earns interest and the salary is paid
    bank(k+1)=(1+r)*bank(k)+s1;
  if k<=nst-1                            
    % the case that student is being paid back,
    % the student loan is paid out of bank account
    bank(k+1)=bank(k+1)-s2;
 
    % student loan earns interest and is reduced by regular payment
    student(k+1)=(1+r)*student(k)-s2;     

  elseif k==nst                          
    % the case that the last payment of student loan is being made
    % student loan is paid out of bank account
    bank(k+1)=bank(k+1)-sst;      
    student(k+1)=0;
  
  else
    % the case that the student loan has been paid back,
    % ensure that student loan is zero at this stage
    student(k+1)=0;     
  end

  if k<=nm-1     
    % the case that mortgage is being paid back
    % balance on bank account is reduced by mortgage payment
    bank(k+1)=bank(k+1)-s3;
                
    % mortgage earns interest and balance is reduced by regular payment
    mortgage(k+1)=(1+r)*mortgage(k)-s3;    
  elseif k==nm                           
    %consider the case that last payment of mortgage is begin made,
    %the balance on bank account is reduced by mortgage payment
    bank(k+1)=bank(k+1)-sm;             
    mortgage(k+1)=0;   

  else 
    % the case that the mortgage has been paid back,
    % ensure that mortgage is zero at this stage
    mortgage(k+1)=0;
  end
end

% 3. OUTPUT

% plot the balances of the bank account, student loan and mortgage
% over time
plot(bank)
hold on
plot(student) 
hold on
plot(mortgage)


