Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions apresentacao.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { formatarMoeda } = require('./util.js');

module.exports = 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;
};
52 changes: 6 additions & 46 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,10 @@
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;
}
var Repositorio = require("./repositorio.js");
var ServicoCalculoFatura = require("./servico.js") ;
var gerarFaturaStr = require("./apresentacao.js");

const faturas = JSON.parse(readFileSync('./faturas.json'));
const pecas = JSON.parse(readFileSync('./pecas.json'));
const faturaStr = gerarFaturaStr(faturas, pecas);
const calc = new ServicoCalculoFatura(new Repositorio());
const faturaStr = gerarFaturaStr(faturas, calc);
console.log(faturaStr);
//
11 changes: 11 additions & 0 deletions repositorio.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { readFileSync } = require('fs');

module.exports = class Repositorio {
constructor() {
this.pecas = JSON.parse(readFileSync('./pecas.json'));
}

getPeca(apre) {
return this.pecas[apre.id];
}
};
51 changes: 51 additions & 0 deletions servico.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module.exports = class ServicoCalculoFatura {
constructor(repo) {
this.repo = repo;
}

calcularTotalApresentacao(apre) {
let total = 0;
switch (this.repo.getPeca(apre).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: ${this.repo.getPeca(apre).tipo}`);
}
return total;
}

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;
}

calcularTotalFatura(apresentacoes) {
let total = 0;
for (let apre of apresentacoes) {
total += this.calcularTotalApresentacao(apre);
}
return total;
}

calcularTotalCreditos(apresentacoes) {
let totalCreditos = 0;
for (let apre of apresentacoes) {
totalCreditos += this.calcularCredito(apre);
}
return totalCreditos;
}
};
7 changes: 7 additions & 0 deletions util.js
Original file line number Diff line number Diff line change
@@ -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 };