If everything is working now, try doing it all in a stored FUNCTION with a CURSOR as IN parameter to return a NUMBER. Learn about variable types and cursors.
The Oracle docs should be enough for you to do it yourself.

After that, if you encounter problems with your code post it here and you certainly will be helped.