Created
February 23, 2024 15:09
-
-
Save douglasselias/1077da62c017647bf8d86dbac172731c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Movie = { | |
title: string | |
code: string | |
} | |
type Rental = { | |
movieID: string | |
days: number | |
} | |
type Customer = { | |
name: string | |
rentals: Rental[] | |
} | |
function statement(customer: Customer, movies: Record<string, Movie>) { | |
let totalAmount = 0; | |
let frequentRenterPoints = 0; | |
let result = `Rental Record for ${customer.name}\n`; | |
for (let r of customer.rentals) { | |
let movie = movies[r.movieID]; | |
let thisAmount = 0; | |
// determine amount for each movie | |
switch (movie.code) { | |
case "regular": | |
thisAmount = 2; | |
if (r.days > 2) { | |
thisAmount += (r.days - 2) * 1.5; | |
} | |
break; | |
case "new": | |
thisAmount = r.days * 3; | |
break; | |
case "childrens": | |
thisAmount = 1.5; | |
if (r.days > 3) { | |
thisAmount += (r.days - 3) * 1.5; | |
} | |
break; | |
} | |
//add frequent renter points | |
frequentRenterPoints++; | |
// add bonus for a two day new release rental | |
if (movie.code === "new" && r.days > 2) frequentRenterPoints++; | |
//print figures for this rental | |
result += `\t${movie.title}\t${thisAmount}\n`; | |
totalAmount += thisAmount; | |
} | |
// add footer lines | |
result += `Amount owed is ${totalAmount}\n`; | |
result += `You earned ${frequentRenterPoints} frequent renter points\n`; | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment