diff --git a/src/bank.ts b/src/bank.ts index 2d38059..bcdb39c 100644 --- a/src/bank.ts +++ b/src/bank.ts @@ -53,4 +53,17 @@ export default class Bank { throw new Error('Account not found'); } } + + withdraw(accountNumber: number, amount: number): void { + let account = this.accounts.find(account => account.accountNumber === accountNumber); + if (account) { + if (account.balance < amount) { + throw new Error('Insufficient balance'); + } + account.balance -= amount; + } + else { + throw new Error('Account not found'); + } + } } \ No newline at end of file diff --git a/tests/bank.test.ts b/tests/bank.test.ts index fa812ce..3caa663 100644 --- a/tests/bank.test.ts +++ b/tests/bank.test.ts @@ -62,4 +62,21 @@ describe('Bank class', () => { expect(() => bank.deposit(12345, 100)).toThrow('Account not found'); }); }); + + describe('withdraw', () => { + it('should withdraw money from the account', () => { + bank.deposit(testAccount.accountNumber!, 100); + bank.withdraw(testAccount.accountNumber!, 50); + expect(bank.getBalance(testAccount.accountNumber!)).toBe(50); + }); + + it('should throw an error if withdrawal amount is greater than the balance', () => { + bank.deposit(testAccount.accountNumber!, 100); + expect(() => bank.withdraw(testAccount.accountNumber!, 150)).toThrow('Insufficient balance'); + }); + + it('should throw an error when withdrawing from an invalid account', () => { + expect(() => bank.withdraw(12345, 50)).toThrow('Account not found'); + }); + }); });