J.P. Morgan interview question

Coding challenge, I used Java for it