diff --git a/apresentacao.js b/apresentacao.js new file mode 100644 index 00000000..22e0c987 --- /dev/null +++ b/apresentacao.js @@ -0,0 +1,15 @@ +const { formatarMoeda } = require("./util"); + +function gerarFaturaStr(fatura, calc) { + let faturaStr = `Fatura ${fatura.cliente}\n`; + for (let apre of fatura.apresentacoes) { + faturaStr += ` ${calc.repo.getPeca(apre).nome}: ` + + `${formatarMoeda(calc.calcularTotalApresentacao(apre))} ` + + `(${apre.audiencia} assentos)\n`; + } + faturaStr += `Valor total: ${formatarMoeda(calc.calcularTotalFatura(fatura.apresentacoes))}\n`; + faturaStr += `Créditos acumulados: ${calc.calcularTotalCreditos(fatura.apresentacoes)} \n`; + return faturaStr; +} + +module.exports = gerarFaturaStr; diff --git a/index.js b/index.js index 1291cf36..ae176778 100644 --- a/index.js +++ b/index.js @@ -1,50 +1,10 @@ -const { readFileSync } = require('fs'); +const { readFileSync } = require("fs"); -function gerarFaturaStr (fatura, pecas) { - let totalFatura = 0; - let creditos = 0; - let faturaStr = `Fatura ${fatura.cliente}\n`; - const formato = new Intl.NumberFormat("pt-BR", - { style: "currency", currency: "BRL", - minimumFractionDigits: 2 }).format; - - for (let apre of fatura.apresentacoes) { - const peca = pecas[apre.id]; - let total = 0; - - switch (peca.tipo) { - case "tragedia": - total = 40000; - if (apre.audiencia > 30) { - total += 1000 * (apre.audiencia - 30); - } - break; - case "comedia": - total = 30000; - if (apre.audiencia > 20) { - total += 10000 + 500 * (apre.audiencia - 20); - } - total += 300 * apre.audiencia; - break; - default: - throw new Error(`Peça desconhecia: ${peca.tipo}`); - } - - // créditos para próximas contratações - creditos += Math.max(apre.audiencia - 30, 0); - if (peca.tipo === "comedia") - creditos += Math.floor(apre.audiencia / 5); - - // mais uma linha da fatura - faturaStr += ` ${peca.nome}: ${formato(total/100)} (${apre.audiencia} assentos)\n`; - totalFatura += total; - } - faturaStr += `Valor total: ${formato(totalFatura/100)}\n`; - faturaStr += `Créditos acumulados: ${creditos} \n`; - return faturaStr; - } +const Repositorio = require("./repositorio"); +const ServicoCalculoFatura = require("./servico"); +const gerarFaturaStr = require("./apresentacao"); -const faturas = JSON.parse(readFileSync('./faturas.json')); -const pecas = JSON.parse(readFileSync('./pecas.json')); -const faturaStr = gerarFaturaStr(faturas, pecas); -console.log(faturaStr); +const faturas = JSON.parse(readFileSync("./faturas.json")); +const calc = new ServicoCalculoFatura(new Repositorio()); + +console.log(gerarFaturaStr(faturas, calc)); diff --git a/repositorio.js b/repositorio.js new file mode 100644 index 00000000..5896be62 --- /dev/null +++ b/repositorio.js @@ -0,0 +1,13 @@ +const { readFileSync } = require("fs"); + +class Repositorio { + constructor() { + this.pecas = JSON.parse(readFileSync("./pecas.json")); + } + + getPeca(apre) { + return this.pecas[apre.id]; + } +} + +module.exports = Repositorio; diff --git a/servico.js b/servico.js new file mode 100644 index 00000000..cb448cda --- /dev/null +++ b/servico.js @@ -0,0 +1,44 @@ +class ServicoCalculoFatura { + constructor(repo) { + this.repo = repo; + } + + calcularTotalApresentacao(apre) { + const peca = this.repo.getPeca(apre); + let total = 0; + switch (peca.tipo) { + case "tragedia": + total = 40000; + if (apre.audiencia > 30) total += 1000 * (apre.audiencia - 30); + break; + case "comedia": + total = 30000; + if (apre.audiencia > 20) + total += 10000 + 500 * (apre.audiencia - 20); + total += 300 * apre.audiencia; + break; + default: + throw new Error(`Peça desconhecida: ${peca.tipo}`); + } + return total; + } + + calcularCredito(apre) { + let creditos = Math.max(apre.audiencia - 30, 0); + if (this.repo.getPeca(apre).tipo === "comedia") + creditos += Math.floor(apre.audiencia / 5); + return creditos; + } + + calcularTotalFatura(apresentacoes) { + return apresentacoes + .reduce((tot, apre) => tot + this.calcularTotalApresentacao(apre), 0); + } + + calcularTotalCreditos(apresentacoes) { + return apresentacoes + .reduce((tot, apre) => tot + this.calcularCredito(apre), 0); + } +} + +module.exports = ServicoCalculoFatura; diff --git a/util.js b/util.js new file mode 100644 index 00000000..4aaed261 --- /dev/null +++ b/util.js @@ -0,0 +1,7 @@ +function formatarMoeda(valor) { + return new Intl.NumberFormat("pt-BR", + { style: "currency", currency: "BRL", minimumFractionDigits: 2 }) + .format(valor / 100); +} + +module.exports = { formatarMoeda };