I am setting up a es6/oop tax calculator and having some issues setting the tax amount correctly. I am instantiating product objects with their quantity and price and adding them to an inventory and then calling a total method on the entire inventory. Some products are tax exempt, so I am using a BasicProduct object and an ExemptProduct object:
const Product = require('./Product')
class ExemptProduct extends Product {
constructor(product) {
super(product)
this._salesTax = product.salesTax
}
get salesTax() {
return this.setSalesTax();
}
setSalesTax () {
return null;
}
}
module.exports = ExemptProduct
The BasicObject sets the sales tax to .10. My total
method is here:
total() {
let total = 0
let tax = 0
let importDuty = .05
for (let productId in this.items) {
total += this.inventory.products.find(product => {
return product.id == productId
}).price * this.items[productId]
tax += this.inventory.products.find(product => {
return product.id == productId
}).salesTax * this.items[productId]
console.log(tax)
}
let taxToApply = total * tax
let importDutyToApply = total * importDuty
total = total + taxToApply + importDutyToApply
return total
}
Right now I am testing with three inventory items, two of which are instantiating as tax exempt products. They all log out with the correct tax amount until they hit this for in
. The console.log I left in there is printing out .10 for all three, when two of them should be 0/null. I am trying to avoid hard coding the tax amount per item, as ultimately there will only be two tax types.