Skip to content

Commit

Permalink
Resolves #2
Browse files Browse the repository at this point in the history
  • Loading branch information
eshwarhs committed Sep 16, 2024
1 parent bf5ac34 commit 89cec17
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/bank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,13 @@ export default class Bank {
}
this.accounts.push({ name, age, balance: 0, accountNumber: Math.floor(Math.random() * 100000) });
}

getAccount(accountNumber: number): BankAccount | undefined {
if (this.accounts.find(account => account.accountNumber === accountNumber)) {
return this.accounts.find(account => account.accountNumber === accountNumber);
}
else {
throw new Error('Account not found');
}
}
}
16 changes: 14 additions & 2 deletions tests/bankTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,23 @@ describe('Bank class', () => {
expect(account.balance).toBe(0);
expect(account.accountNumber).toBeDefined();
});
});

describe('createAccount', () => {
it('should throw an error if age is negative', () => {
expect(() => bank.createAccount('John Doe', -30)).toThrow('Age must be positive');
});
});

describe('getAccount', () => {
it('should return the correct account when a valid account number is provided', () => {
bank.createAccount('John Doe', 30);
const account = bank.accounts[0];
const foundAccount = bank.getAccount(account.accountNumber!);
expect(foundAccount).toEqual(account);
});

it('should throw an error when an invalid account number is provided', () => {
expect(() => bank.getAccount(12345)).toThrow('Account not found');
});
});

});

0 comments on commit 89cec17

Please sign in to comment.