[英]How to check if there is more than one product on a receipt?
我正在編寫一個方法,它檢查客戶是否已達到閾值以獲取客戶購買的積分。 這個方法有兩個參數:一張發票(我已經把它移到另一個類)和一個模型(我們在其中存儲忠誠度積分的閾值)。 我知道如何遍歷價格以將其加總,但我不知道如果發票上有數量之類的東西,我不知道如何操作:4。
下面我附上了一個方法,還有一個發票模型。
提前致謝!
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")
}
}
和發票模型:
class Invoice {
public code: string | undefined;
public count: number | undefined;
public price: number | undefined;
}
您只需要將數量乘以價格:
for (let item of invoice) {
totalPriceForProducts += item.count * item.price
}
如果有可能未定義 count ,您可以先檢查它並默認為 1:
totalPriceForProducts += (item.count === undefined ? 1 : item.count) * item.price
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.