Solución
solution.tsTypeScript
const TOTAL_MIN = 5
const STRETCH_MIN = 100
const STRETCH_RATE_MIN = 0.10
const STRETCH_MID = 300
const STRETCH_RATE_MID = 0.15
const STRETCH_MID_HIGH = 200
const STRETCH_RATE_MID_HIGH = 0.20
const STRETCH_HIGH = 500
const STRETCH_MAX = 1000
const UNIVERSAL_RATE = 50
const PLAIN_RATE = 0.12
const PLAIN_RATE_DISCOUNT = 0.85
const PLAIN_RATE_HIGH = 0.18
const PLAIN_RATE_HIGH_DISCOUNT = 0.90
function calculateBillForClientType(stretch: number, clientType: string) {
// Calcular costo según el tipo de cliente
if (clientType === "residential") {
// Aplicar tarifa por tramos según consumo
if (stretch <= STRETCH_MIN) return stretch * STRETCH_RATE_MIN;
else if (stretch <= STRETCH_MID) return STRETCH_MIN * STRETCH_RATE_MIN + (stretch - STRETCH_MIN) * STRETCH_RATE_MID;
else return STRETCH_MIN * STRETCH_RATE_MIN + STRETCH_MID_HIGH * STRETCH_RATE_MID + (stretch - STRETCH_MID) * STRETCH_RATE_MID_HIGH;
}else if (clientType === "commercial") {
// Aplicar tarifa plana con posible descuento por alto consumo
if (stretch > STRETCH_HIGH) return stretch * PLAIN_RATE_HIGH * PLAIN_RATE_HIGH_DISCOUNT;
else return stretch * PLAIN_RATE_HIGH;
} else if (clientType === "industrial") {
// Aplicar tarifa plana con posible descuento y cargo fijo
if (stretch > STRETCH_MAX) return +(stretch * PLAIN_RATE * PLAIN_RATE_DISCOUNT + UNIVERSAL_RATE).toFixed(1);
else return +(stretch * PLAIN_RATE + UNIVERSAL_RATE).toFixed(1);
}
}
export function calculateElectricityBill(stretch: number, clientType: string): number {
let total = calculateBillForClientType(stretch, clientType);
// Aplicar cobro mínimo si el costo calculado es menor al permitido
if (total < TOTAL_MIN) return TOTAL_MIN
return total;
}0respuestas