How to calculate total cost in real time?