Skip to content

Instantly share code, notes, and snippets.

@douglasselias
Created February 23, 2024 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save douglasselias/1077da62c017647bf8d86dbac172731c to your computer and use it in GitHub Desktop.
Save douglasselias/1077da62c017647bf8d86dbac172731c to your computer and use it in GitHub Desktop.
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