Skip to content

Commit

Permalink
Resolves #3
Browse files Browse the repository at this point in the history
  • Loading branch information
eshwarhs committed Sep 17, 2024
1 parent f6093df commit bb9e313
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/bank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
}
17 changes: 17 additions & 0 deletions tests/bank.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});

0 comments on commit bb9e313

Please sign in to comment.