In this exercise we are going to create interfaces and how they are implemented in typescript, we will also see how to use polymorphism
1) Following the exercises that we have been doing, in the other posts
2) we will start by creating the following interface AccountSettings, it will help us to assign the type to the constructor as well
import {AccountType} from '../scripts/enums';
import { AccountInfo } from './account-info';
export interface AccountSettings {
id: number;
title: string;
balance: number;
accountType: AccountType;
accountInfo: AccountInfo<number, number>;
}
3) We will create the interface AccountInfo
export interface AccountInfo<TRouteNumber, TBankNumber>{
routingNumber: TRouteNumber;
accountNumber: TBankNumber;
}
4) We will create the interface DepositWithdrawal
export interface DepositWithdrawal {
deposit(amount: number): void;
withdrawal(amount: number): void;
}
5) Finally we create the Account interface
import { AccountSettings } from './account-settings';
import { DepositWithdrawal } from './deposit-withdrawal';
export interface Account extends AccountSettings, DepositWithdrawal {}
6) in our BankAccount class, we will implement the Account interface
import { AccountType } from './enums';
import { Account } from './../interfaces/account';
import { AccountInfo } from '../interfaces/account-info';
import { AccountSettings } from '../interfaces/account-settings';
export class BankAccount implements Account{
private _balance = 0;
id:number;
title : string;
accountType : AccountType;
accountInfo: AccountInfo<number, number>;
constructor(accuntSettings : AccountSettings) {
this.id = accuntSettings.id;
this.title = accuntSettings.title;
this._balance = accuntSettings.balance;
}
//get set balance
public get balance() : number {
return this._balance;
}
public set balance(value : number) {
this._balance = value;
}
//deposit money
public deposit(amount : number) : void {
this._balance += amount;
}
//withdraw money
public withdrawal(amount : number) : void {
this._balance -= amount;
}
}
7) now in our index class, we create the AccountSettings object, it will serve to pass it to the constructor, because now it no longer receives an "any" type
//create object of AccountSettings
let accountSettings : AccountSettings = {
id: 1,
title: "Checking Account",
balance: 1000,
accountType: AccountType.Checking,
accountInfo: {
routingNumber: 12345,
accountNumber: 1234567890
}
}
//instantiate
const _checkingAccount = new CheckingAccount(accountSettings);
8) we call the methods and show by console when executing the project
//Deposit 50
_checkingAccount.deposit(100);
//show balance
console.log("Balance: ",_checkingAccount.balance);
9) Now for the polymorphism, we create the ATM class, we will use the methods of the DepositWithdrawal interface and we will add a different functionality to it
import { DepositWithdrawal } from './../interfaces/deposit-withdrawal';
import { BankAccount } from './bank-account';
export class ATM implements DepositWithdrawal {
constructor(private account: BankAccount) {
}
withdrawal(amount: number): void {
amount += amount * 0.1;
this.account.withdrawal(amount)
}
deposit(amount: number): void {
amount += amount * 0.2;
this.account.deposit(amount)
}
}
10) in our index method, we create the ATM class object, we pass the created object _checkingAccount to the constructor, and we make the call to the methods that we have changed
//create object ATM
const _atm = new ATM(_checkingAccount);
_atm.withdrawal(500);
console.log("withdraw 500 from ATM");
console.log("Balance: ",_checkingAccount.balance);
console.log("deposit 100 to ATM");
_atm.deposit(100);
console.log("Balance: ",_checkingAccount.balance);
console.log("withdraw 50 from ATM");
_atm.withdrawal(50);
console.log("Balance: ",_checkingAccount.balance);
11) index.ts File
import { AccountSettings } from './interfaces/account-settings';
import { CheckingAccount } from './scripts/checking-account';
import { AccountType } from './scripts/enums';
import { ATM } from './scripts/atm';
//create object of AccountSettings
let accountSettings : AccountSettings = {
id: 1,
title: "Checking Account",
balance: 1000,
accountType: AccountType.Checking,
accountInfo: {
routingNumber: 12345,
accountNumber: 1234567890
}
}
//instantiate
const _checkingAccount = new CheckingAccount(accountSettings);
//Deposit 50
_checkingAccount.deposit(100);
//show balance
console.log("Balance: ",_checkingAccount.balance);
//create object ATM
const _atm = new ATM(_checkingAccount);
_atm.withdrawal(500);
console.log("withdraw 500 from ATM");
console.log("Balance: ",_checkingAccount.balance);
console.log("deposit 100 to ATM");
_atm.deposit(100);
console.log("Balance: ",_checkingAccount.balance);
console.log("withdraw 50 from ATM");
_atm.withdrawal(50);
console.log("Balance: ",_checkingAccount.balance);
12) We run the project and see the console