简体   繁体   中英

How to check if there is more than one product on a receipt?

I'm writing a method, which checks if a client has reached a treshold to get a customer points for their purchases. This method takes 2 arguments: an invoice ( which I've moved to another class), and a model( where we store a treshold for loyalty points). I know how to loop over the price to total it up, but i don't know how to act if on the invoice there will be something like quantity: 4.

Below I'm attaching a method, and a model for invoice also.

Thanks in advance!

 public checkPoints(invoice: Invoice[], promotion: Promotion) {
    let totalPriceForProducts = 0;

    for (let totalPrice of invoice) {
        totalPriceForProducts = totalPriceForProducts + totalPrice.price
    }
    if (totalPriceForProducts < promotion.treshold) {
        throw new TresholdNotReached("Treshold not reached")
    }
}

And an invoice model:

  class Invoice {
  public code:  string | undefined;
  public count: number | undefined;
  public price: number | undefined;
}

You just need to multiply count by price:

for (let item of invoice) {
  totalPriceForProducts += item.count * item.price
}

If there's a possibility that count is undefined, you can check it first and default to 1:

totalPriceForProducts += (item.count === undefined ? 1 : item.count) * item.price

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM