From b467fd160f08f357b8c648e8a5d77feba1dbf036 Mon Sep 17 00:00:00 2001 From: Joao Vitor Miranda Moura Date: Mon, 18 Aug 2025 23:15:04 -0300 Subject: [PATCH] Commit 9 - Criando Arquivos --- Apresentacao.js | 14 +++++++++++++ index.js | 53 ++++++---------------------------------------- repositorio.js | 13 ++++++++++++ sevico.js | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ util.js | 10 +++++++++ 5 files changed, 99 insertions(+), 47 deletions(-) create mode 100644 Apresentacao.js create mode 100644 repositorio.js create mode 100644 sevico.js create mode 100644 util.js diff --git a/Apresentacao.js b/Apresentacao.js new file mode 100644 index 00000000..92f479ca --- /dev/null +++ b/Apresentacao.js @@ -0,0 +1,14 @@ +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; \ No newline at end of file diff --git a/index.js b/index.js index 1291cf36..eb903589 100644 --- a/index.js +++ b/index.js @@ -1,50 +1,9 @@ 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.js"); +const ServicoCalculoFatura = require("./servico.js"); +const gerarFaturaStr = require("./apresentacao.js"); const faturas = JSON.parse(readFileSync('./faturas.json')); -const pecas = JSON.parse(readFileSync('./pecas.json')); -const faturaStr = gerarFaturaStr(faturas, pecas); -console.log(faturaStr); +const calc = new ServicoCalculoFatura(new Repositorio()); +const faturaStr = gerarFaturaStr(faturas, calc); +console.log(faturaStr); \ No newline at end of file diff --git a/repositorio.js b/repositorio.js new file mode 100644 index 00000000..fafca841 --- /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; \ No newline at end of file diff --git a/sevico.js b/sevico.js new file mode 100644 index 00000000..ab9cfabb --- /dev/null +++ b/sevico.js @@ -0,0 +1,56 @@ +class ServicoCalculoFatura { + + constructor(repo) { + this.repo = repo; + } + + calcularCredito(apre) { + let creditos = 0; + creditos += Math.max(apre.audiencia - 30, 0); + if (this.repo.getPeca(apre).tipo === "comedia") + creditos += Math.floor(apre.audiencia / 5); + return creditos; + } + + calcularTotalCreditos(apresentacoes) { + let creditos = 0; + for (let apre of apresentacoes) { + creditos += this.calcularCredito(apre); + } + return creditos; + } + + calcularTotalApresentacao(apre) { + let total = 0; + const peca = this.repo.getPeca(apre); + 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; + } + + calcularTotalFatura(apresentacoes) { + let total = 0; + for (let apre of apresentacoes) { + total += this.calcularTotalApresentacao(apre); + } + return total; + } +} + +module.exports = ServicoCalculoFatura; + \ No newline at end of file diff --git a/util.js b/util.js new file mode 100644 index 00000000..74efda67 --- /dev/null +++ b/util.js @@ -0,0 +1,10 @@ +function formatarMoeda(valor) { + return new Intl.NumberFormat("pt-BR", { + style: "currency", + currency: "BRL", + minimumFractionDigits: 2 + }).format(valor / 100); +} + +module.exports = formatarMoeda; + \ No newline at end of file