Solución
solution.tsTypeScript
const RESIDENTIAL_MINIMAL_COST = 0.10;
const RESIDENTIAL_MEDIUM_COST = 0.15;
const RESIDENTIAL_MAX_COST = 0.20;
const COMMERCIAL_COST = 0.18
const COMMERCIAL_DISCUAL_PERCENTAGE = 0.90
const INDUSTRIAL_BASE_COST = 0.12
const INDUSTRIAL_BASE_CHARGE = 50;
const INDUSTRIAL_DISCOUNT_PERCENTAGE = 0.85
type clientType = "residential" | "commercial" | "industrial"
export function calculateElectricityBill(cost: number, clientType: clientType): number {
let ELECTRICITY_COST = 0
const MINIMAL_COST = 5
// Calcular costo según el tipo de cliente
ELECTRICITY_COST = getElectricityAmounts(clientType,cost)
// Aplicar cobro mínimo si el costo calculado es menor al permitido
if (ELECTRICITY_COST < MINIMAL_COST) ELECTRICITY_COST = MINIMAL_COST;
return +ELECTRICITY_COST.toFixed(2);
}
function getElectricityAmounts(clientType:clientType, cost:number) {
switch(clientType) {
case "residential":
if (cost <= 100) {
cost = cost * RESIDENTIAL_MINIMAL_COST;
} else if (cost <= 300) {
cost = 100 * RESIDENTIAL_MINIMAL_COST + (cost - 100) * RESIDENTIAL_MEDIUM_COST;
} else {
cost = 100 * RESIDENTIAL_MINIMAL_COST + 200 * RESIDENTIAL_MEDIUM_COST + (cost - 300) * RESIDENTIAL_MAX_COST;
}
return cost;
case "commercial":
if (cost > 500) {
cost = cost * COMMERCIAL_COST * COMMERCIAL_DISCUAL_PERCENTAGE;
} else {
cost = cost * COMMERCIAL_COST;
}
return cost;
case "industrial":
if (cost > 1000) {
cost = cost * INDUSTRIAL_BASE_COST * INDUSTRIAL_DISCOUNT_PERCENTAGE + INDUSTRIAL_BASE_CHARGE;
} else {
cost = cost * INDUSTRIAL_BASE_COST + INDUSTRIAL_BASE_CHARGE;
}
return cost;
}
}0respuestas