Solution: Calculate Sales Tax
Go over the implementation of calculating sales tax and look at the use of modules.
We'll cover the following...
Solution
Press + to interact
module GSTCalcGST_RATE = 10.0def net_amountif @sales_tax_applicable(@amount / (100.0 + GST_RATE) * 100.0).round(2)elsereturn @amountendenddef gstif @sales_tax_applicabletax = (@amount - self.net_amount).round(2)elsereturn 0.0endendendclass ServiceIteminclude GSTCalcattr_accessor :name, :amount, :sales_tax_applicabledef initialize(name, amount)self.name = nameself.amount = amountself.sales_tax_applicable = false # => default no sales taxendendclass Goodsinclude GSTCalcattr_accessor :name, :amount, :sales_tax_applicabledef initialize(name, amount)self.name = nameself.amount = amountself.sales_tax_applicable = true # => default has sales taxendendfoam_roller = Goods.new("Foam Roller", 49.95)puts "#{foam_roller.name} Net Amount: #{foam_roller.net_amount}, GST: #{foam_roller.gst}"physio_service = ServiceItem.new("Physio Consultation", 120.0)puts "#{physio_service.name} Net Amount: #{physio_service.net_amount}, GST: #{physio_service.gst}"pilates_class = ServiceItem.new("Pilates Classes", 80.0)pilates_class.sales_tax_applicable = trueputs "#{pilates_class.name} Net Amount: #{pilates_class.net_amount}, GST: #{pilates_class.gst}"
Explanation
Lines 1–20: We define the
GSTCalc
...