Skip to content

Instantly share code, notes, and snippets.

@SergioEnrique
Last active June 7, 2022 13:05
Show Gist options
  • Save SergioEnrique/79e90b1bd51b170dec3428b0ba7536e7 to your computer and use it in GitHub Desktop.
Save SergioEnrique/79e90b1bd51b170dec3428b0ba7536e7 to your computer and use it in GitHub Desktop.
Ejercicio Teórico
/* ******
* Ejercicio trabajado por Sergio Enrique Vargas
*
* Problemas detectados en el código:
* Todos los condicionales dentro del método getTotal usaban "typeof" lo cual sirve para identificar
* el tipo de la instancia pero no es útil sobretodo a nivel ejecución donde las interfaces no tienen tipo.
* Se recomienda especificar claramente qué tipo de servicio y contenido es el que se está manejando.
* La solución propuesta consiste en agregar algunas propiedades a las clases propuestas, por ejemplo
* "isPremiumContent" que regresa un boleano en el caso de MultimediaContent y "isStreamingService" e
* "isDownloadService" para los Servicios.
*
* Se agregaron validaciones para no sumar o sumar +0 en caso de que el servicio no tenga multimediaContent
* o si algunas de sus propiedades están vacías, nulas o inexistentes.
*
* Otra solución podría ser con una propiedad llamada type de tipo string. Para este caso se podrían
* crear constantes y para la condicionales revisar si el type y la constante coinciden.
* p.e. if(service.type === 'STREAMING'). Sin embargo se decidió usar booleanos puesto que hay pocos
* tipos y no es necesario usar constantes.
*
* Notas extra:
* La estructura del código puede no estar completa para su uso real, puesto que sería necesario agregar
* identificadores y métodos para agregar los diferentes servicios al usuario. En esta revisión sólo se
* mejora la estructura de la información y el código para obtener el costo total.
* El index del forEach no se ocupa, es redundante aunque no provocaría errores, pero por buenas prácticas
* es mejor removerlo.
* *******/
interface MultimediaContent {
title: string;
streamingPrice: number;
downloadPrice: number;
duration: number;
adult: boolean;
size: number;
isPremiumContent: boolean;
}
interface PremiumContent extends MultimediaContent {
isPremiumContent: true;
additionalFee: number;
}
type MultimediaContentType = MultimediaContent & PremiumContent;
interface Service {
timestamp: number;
getMultimediaContent(): MultimediaContentType;
isStreamingService: boolean;
isDownloadService: boolean;
}
interface StreamingService extends Service {
isStreamingService: true;
isDownloadService: false;
}
interface DownloadService extends Service {
isStreamingService: false;
isDownloadService: true;
}
type ServiceType = StreamingService | DownloadService;
export default class RegisteredUser {
private email: string;
private password: string;
private registration: string;
private adult: boolean;
private services: ServiceType[];
constructor(services = []) {
this.services = services;
}
getTotal(): number {
let total = 0;
this.services.forEach((service) => {
const multimediaContent = service.getMultimediaContent();
if (multimediaContent) {
if (service.isStreamingService) {
total += multimediaContent.streamingPrice || 0;
} else if (service.isDownloadService) {
total += multimediaContent.downloadPrice || 0;
}
if (multimediaContent.isPremiumContent) {
total += multimediaContent.additionalFee || 0;
}
}
});
return total;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment