Review generated invoice copy

from Structures and classes
Swift 6.3.3 advanced 6 min 4 issues to find

Review this generated invoice model against its copy, validation, privacy, and lookup requirements.

Return an independently discounted invoice without changing the original, accept only percentages from 0 through 100, never log customer names, and total requested SKUs without a fresh linear scan for each SKU.

swift
final class LineItem {
    let sku: String
    var cents: Int
    init(sku: String, cents: Int) { self.sku = sku; self.cents = cents }
}

struct Invoice {
    let customerName: String
    var items: [LineItem]

    func discounted(percent: Int) -> Invoice {
        let copy = self
        for item in copy.items {
            item.cents -= item.cents * percent / 100
        }
        print("discounted", customerName)
        return copy
    }

    func total(for skus: [String]) -> Int {
        skus.reduce(0) { sum, sku in
            sum + (items.first { $0.sku == sku }?.cents ?? 0)
        }
    }
}

generated code is illustrative, not from any one model

Open in playground
Report an error