Management thinks our branding is so creative that our SaaS customers will pay for Textio merch.
Assignment
Complete the placeOrder function.
It returns a bool indicating whether the order was successful (true is a success) and a float64 representing the user's balance after the order. The placeOrder function should always return the account balance regardless of if it was adjusted or not.
The amountInStock and calcPrice functions can be used to look up the current stock and price of an item.
If the quantity is greater than the amount in stock, the order should be rejected.
If the user doesn't have enough money in their account, the order should be rejected.
Otherwise, the order should be accepted and you should return the new balance.
packagemainfuncplaceOrder(productIDstring,quantityint,accountBalancefloat64)(bool,float64){total:=calcPrice(productID,quantity)stock:=amountInStock(productID)ifquantity>stock{returnfalse,accountBalance}elseiftotal>accountBalance{returnfalse,accountBalance}else{newBalance:=accountBalance-totalreturntrue,newBalance}}// Don't touch below this linefunccalcPrice(productIDstring,quantityint)float64{returnpriceList(productID)*float64(quantity)}funcpriceList(productIDstring)float64{ifproductID=="1"{return1.50}elseifproductID=="2"{return2.25}elseifproductID=="3"{return3.00}elseifproductID=="4"{return1.00}elseifproductID=="5"{return2.50}elseifproductID=="6"{return8.99}elseifproductID=="7"{return22.50}elseifproductID=="8"{return50.00}elseifproductID=="9"{return999.99}else{return0.00}}funcamountInStock(productIDstring)int{ifproductID=="1"{return11}elseifproductID=="2"{return25}elseifproductID=="3"{return4}elseifproductID=="4"{return6}elseifproductID=="5"{return50}elseifproductID=="6"{return2}elseifproductID=="7"{return0}elseifproductID=="8"{return99}elseifproductID=="9"{return1}else{return0}}
Solution
The provided implementation already follows the specified rules:
Rejects if requested quantity exceeds stock.
Rejects if total price exceeds account balance.
Returns the updated balance only when the order is accepted; otherwise returns the original balance.