Questions asking for code to be written to solve a specific problem are off-topic here as there is no code to review.
Your question must contain working code for us to review it here. For questions regarding specific problems encountered while coding, try Stack Overflow. After getting your code to work, you may edit this question seeking a review of your working code.
Questions must include the code you want reviewed. Code must not appear only in a link to an external source. Doing so makes us dependent on a third-party site and makes it harder to review your code. If your code is very large, please select only the portions in which you are especially interested in for a review. You are welcome to keep the link to the rest of your code.
I've posted a proposal below. Upvote it, or post your own counterproposal.