Skip to content

Instantly share code, notes, and snippets.

@dsibinski
Created October 1, 2024 16:08
Show Gist options
  • Save dsibinski/28087f239e57c7dab06d3df2e7532e6d to your computer and use it in GitHub Desktop.
Save dsibinski/28087f239e57c7dab06d3df2e7532e6d to your computer and use it in GitHub Desktop.
export class TicketsPriceCalculator {
calculateTotalTicketsPrice(tickets: Ticket[]): number {
if (tickets.length === 0) {
return 0;
}
let totalPrice = tickets.reduce((sum, ticket) => sum + ticket.price, 0);
const businessClassTickets = tickets.filter(
(ticket) => ticket.isBusinessClass
).length;
let totalDiscountToApply = 0;
// Apply discounts based on total price
if (totalPrice > 2000) {
totalDiscountToApply += 0.25;
} else if (totalPrice > 1000 && totalPrice <= 2000) {
totalDiscountToApply += 0.2;
} else if (totalPrice > 500 && totalPrice <= 1000) {
totalDiscountToApply += 0.15;
} else if (totalPrice >= 200 && totalPrice <= 500) {
totalDiscountToApply += 0.1;
}
// Apply additional discount based on number of tickets
if (tickets.length >= 10) {
totalDiscountToApply += 0.05;
}
// Apply business class discounts
if (businessClassTickets >= 10) {
totalDiscountToApply += 0.2;
} else if (businessClassTickets >= 5) {
totalDiscountToApply += 0.1;
}
return totalPrice * (1 - totalDiscountToApply);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment