Repository navigation
Add unit tests for the "Simple Bank System" problem. #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // Tags: Array, Design | ||
| package com.leetcode.medium; | ||
|
|
||
| public class Bank2043 { | ||
| private final long[] accounts; | ||
|
|
||
| public Bank2043(long[] balance) { | ||
| accounts = balance; | ||
| } | ||
|
|
||
| public boolean transfer(int account1, int account2, long money) { | ||
| if (account1 > accounts.length || account2 > accounts.length) { | ||
| return false; | ||
| } | ||
| if (accounts[account1 - 1] < money) { | ||
| return false; | ||
| } | ||
| accounts[account1 - 1] -= money; | ||
| accounts[account2 - 1] += money; | ||
| return true; | ||
| } | ||
|
|
||
| public boolean deposit(int account, long money) { | ||
| if (account > accounts.length) { | ||
| return false; | ||
|
Comment on lines
+24
to
+25
|
||
| } | ||
| accounts[account - 1] += money; | ||
| return true; | ||
| } | ||
|
|
||
| public boolean withdraw(int account, long money) { | ||
| if (account > accounts.length) { | ||
| return false; | ||
|
Comment on lines
+32
to
+33
|
||
| } | ||
| if (accounts[account - 1] < money) { | ||
| return false; | ||
| } | ||
| accounts[account - 1] -= money; | ||
| return true; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The account validation is incomplete. It only checks if the account number exceeds the array length, but doesn't validate if the account number is less than 1. Account numbers should be valid positive integers (1-indexed). Add a check for
account1 < 1 || account2 < 1to prevent potential ArrayIndexOutOfBoundsException when accessingaccounts[account1 - 1]oraccounts[account2 - 1].