Solución
solution.tsTypeScript
type FlyCategory = 'economy' | 'business' | 'first';
type CategoryPrices = Record<FlyCategory, number>;
const MIN_PASSENGERS = 1;
const MIN_DISTANCE = 100;
const BASE_PRICE = 0.10;
const BUSINESS_PRICE = 0.25;
const FIRST_CLASS_PRICE = 0.45;
const PRICE_PER_LUGGAGE = 30;
const MIN_PASSENGERS_DISCOUNT = 5;
const DISCOUNT = 0.10;
const INVALID_OPERATION = -1;
const FOR_ROUNDING = 100;
const PRICES_PER_CATEGORY: CategoryPrices = {
economy: BASE_PRICE,
business: BUSINESS_PRICE,
first: FIRST_CLASS_PRICE,
};
function isVerified(passengers: number, distance: number): number | undefined {
if (passengers < MIN_PASSENGERS || distance < MIN_DISTANCE) return INVALID_OPERATION;
return;
};
function subtotalPerCategory(
passengers: number,
category: FlyCategory,
distance: number,
) {
const SUBTOTAL = distance * passengers * PRICES_PER_CATEGORY[category];
return SUBTOTAL;
};
function getPriceBeforeDiscount(
passengers: number,
category: FlyCategory,
distance: number,
luggage: boolean,
) {
let subTotal = subtotalPerCategory(passengers, category, distance);
if (category === 'economy' && luggage) {
subTotal = subTotal + PRICE_PER_LUGGAGE * passengers;
}
return (subTotal = Math.round(subTotal * FOR_ROUNDING) / FOR_ROUNDING);
};
function getDiscount( total: number ): number {
const FINAL_PRIZE = total - (total * DISCOUNT);
return FINAL_PRIZE;
};
export function calculateFlightTicketPrice( passengers: number, category: FlyCategory, distance: number, luggage: boolean,): number {
if(isVerified(passengers, distance) === INVALID_OPERATION) return INVALID_OPERATION;
const TICKET_PRIZE = getPriceBeforeDiscount( passengers, category, distance, luggage );
if (passengers >= MIN_PASSENGERS_DISCOUNT) {
return getDiscount(TICKET_PRIZE);
} else {
return TICKET_PRIZE;
}
}0respuestas