class Negociacao {
constructor(_data,_quantidade,_valor) {
Object.assign(this, {_quantidade,_valor})
this._data=new Date(_data.getTime());
Object.freeze(this);
}
get volume () {
return this._quantidade * this._valor;
}
get dados() {
return new Date(this._data.getTime());
}
get quantidade() {
return this._quantidade;
}
get valor() {
return this._valor;
}
}
var campos = [
document.querySelector('#data'),
document.querySelector('#valor'),
document.querySelector('#quantidade')
] ;
console.log(campos);
var tbody = document.querySelector('table tbody');
document.querySelector('.form').addEventListener('enviar', function(event) {
event.preventDefault();
var tr = document.createElement('tr') ;
campos.forEach ( function (campo) {
var td = document.createElement('td');
td.textContent = campo.value;
tr.appendChild(td);
} ) ;
var tdVolume = document.createElement('td');
tdVolume.textContent = campos[1].value * campos[2].value;
tr.appendChild(tdVolume);
tbody . appendChild ( tr ) ;
campos[0].value = '';
campos[1].value = 1;
campos[2].value = 0;
campos[0].focus();
} ) ;
class NegociacaoController {
constructor() {
let $ = document.querySelector.bind( document);
this._inputData = $ ( '#data' ) ;
this._inputQuantidade = $('#quantidade');
this._inputValor = $('#valor');
this._negociacoes = new Negociacoes();
this._negociacoesView = new NegociacoesView('#negociacoes');
// recebe boletim, o modelo que encapsula uma lista vazia
this._negociacoesView.update(this._negociacoes);
}
adiciona(event) {
event.preventDefault ( ) ;
this._negociacoes.adiciona(this._criaNegociacao());
this._negociacoesView.update(this._negociacoes);
this._limpaFormulario();
}
_limpaFormulario( ) {
this._inputData.value = '';
this._inputQuantidade.value = 1;
this._inputValor.value = 0,0
this._inputData.focus();
}
_criaNegociacao ( ) {
return new Negociacao (
DateConverter.paraData(this._inputData.value),
parseInt(this._inputQuantidade.valor),
parseFloat(this._inputValor.value)
);
}
}
class DateConverter {
constructor() {
throw new Error('Esta classe não pode ser instanciada');
}
static paraTexto(data) {
return `${data.getDate()}/${data.getMonth() + 1}/${data.getFullYear()}`;
}
static paraData(texto) {
if (!/^\d{4}-\d{2}-\d{2}$/.test(texto))
throw new Error('Deve estar no formato aaaa-mm-dd');
return new Date(...texto.split('-').map((item, indice) => item - indice % 2));
}
}
class Negociacoes {
constructor() {
this._negociacoes = [];
}
adiciona(negociacao) {
this._negociacoes.push(negociacao);
}
paraArray() {
return[].concat(this._negociacoes);
}
get volumeTotal() {
return this._negociacoes
.reduce((total, negociacao) =>
total + negociacao.volume, 0);
}
}
class NegociacoesView {
constructor(seletor) {
this._elemento = document.querySelector(seletor);
}
update(model) {
this._elemento.innerHTML = this.template(model);
}
template(model) {
return `
`
}
}
let controller = new NegociacaoController();
document
.querySelector('.form')
.addEventListener('submit', controller.adiciona.bind(controller));
DATA | QUANTIDADE | VALOR | VOLUME |
---|---|---|---|
${DateConverter.paraTexto(negociacao.data)} | ${negociacao.quantidade} | ${negociacao.valor} | ${negociacao.volume} |
${model.volumeTotal} |
Assinar:
Postar comentários (Atom)
Nenhum comentário:
Postar um comentário