Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am building a cart in Java and would like to give users the ability to update

ID: 647830 • Letter: I

Question

I am building a cart in Java and would like to give users the ability to update the quantity and see the new price instantly. I am using BigDecimal on the back-end for complete accuracy, but the ajax call introduces a slight delay. It isn't much but I could make the update instant by sending the update to the server and just updating the price in Javascript before it even returns. The speed would be wonderful but it comes at the expense of rounding errors being presented to the user.

Now the back-end computed price would be the one shown on the payment page so that would be accurate at least but is the speed worth the risk of a bad value showing in the cart in an eCommerce system?

Explanation / Answer

If you let the math for one second aside, the following should answer your question: The webpage, your client received is nothing more than a view. If you want to "display" changes immediately to your customer, you could do the math in JS. No roundtrip to the server needed. Think of oyur system under load: 100 000 people add and delete items on their shopping card. The result is unnecessary background noise on your server to calculate the new sum. What a waste of ressources.

Calculate and display the result via JS for the client and do the real math on the server, when the order is done.