diff --git a/dist/aluno.js b/dist/aluno.js new file mode 100644 index 0000000..a15ea69 --- /dev/null +++ b/dist/aluno.js @@ -0,0 +1,7 @@ +"use strict"; +class Aluno extends Usuario { + constructor(nome, email, senha, matricula) { + super(nome, email, senha); + this.matricula = matricula; + } +} diff --git a/dist/biblioteca.js b/dist/biblioteca.js new file mode 100644 index 0000000..39a34cc --- /dev/null +++ b/dist/biblioteca.js @@ -0,0 +1,76 @@ +"use strict"; +class Biblioteca { + constructor(livrosDisponiveisElement, emprestimosAtivosElement) { + this.livrosDisponiveisElement = livrosDisponiveisElement; + this.emprestimosAtivosElement = emprestimosAtivosElement; + this.livros = []; + // livros: Array = []; + this.alunos = []; + this.emprestimos = []; + } + // --------- [Não mexer] Responsaveis por renderizar no html + renderizarLivrosDisponiveis() { + this.livrosDisponiveisElement.innerHTML = ""; + const disponiveis = this.livros.filter((livro) => livro.estaDisponivel); + const selectLivro = document.getElementById("livro"); + selectLivro.innerHTML = ""; + disponiveis.forEach((livro) => { + // Atualizar lista + this.livrosDisponiveisElement.appendChild(livro.criarElementoHTML()); + // Preencher select + const option = document.createElement("option"); + option.value = String(livro.id); // Define o valor da opção como o ID do livro + option.textContent = livro.titulo; // Define o texto da opção como o título do livro + selectLivro.appendChild(option); + }); + } + renderizarEmprestimosAtivos() { + this.emprestimosAtivosElement.innerHTML = ""; + this.emprestimos.forEach((emprestimo) => { + const li = document.createElement("li"); + li.textContent = `Livro: ${emprestimo.livro.titulo}, Usuário: ${emprestimo.aluno.nome}, Data de Devolução: ${emprestimo.dataDevolucao.toDateString()}`; + this.emprestimosAtivosElement.appendChild(li); + }); + } + // --------- [Não mexer] Responsaveis por renderizar no html + adicionarLivro(livro) { + this.livros.push(livro); + this.renderizarLivrosDisponiveis(); + } + encontrarLivro(id) { + const livroEncontrado = this.livros.find((livro) => livro.id === id); + return livroEncontrado; + } + adicionarAluno(aluno) { + this.alunos.push(aluno); + } + encontrarAluno(matricula) { + return this.alunos.find((aluno) => aluno.matricula === matricula); + } + realizarEmprestimo(livro, aluno, senha) { + if (!aluno.matricula) { + alert("Matrícula inexistente"); + return false; + } + if (!senha || senha !== aluno.senha) { + alert("Senha incorreta ou inexistente!"); + return false; + } + if (!livro.estaDisponivel) { + alert(`O livro "${livro.titulo} não está disponível para emprestimo. "`); + return false; + } + livro.emprestarLivro(); + const dataEmprestimo = new Date(); + const dataDevolucao = new Date(); + dataDevolucao.setDate(dataDevolucao.getDate() + 7); + const emprestimo = { + livro, + aluno, + dataEmprestimo, + dataDevolucao, + }; + this.emprestimos.push(emprestimo); + this.renderizarEmprestimosAtivos(); + } +} diff --git a/dist/emprestimo.js b/dist/emprestimo.js new file mode 100644 index 0000000..3918c74 --- /dev/null +++ b/dist/emprestimo.js @@ -0,0 +1 @@ +"use strict"; diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..985dca7 --- /dev/null +++ b/dist/index.js @@ -0,0 +1,40 @@ +"use strict"; +document.addEventListener("DOMContentLoaded", () => { + const livrosDisponiveisElement = document.getElementById("livrosDisponiveis"); + const emprestimosAtivosElement = document.getElementById("emprestimosAtivos"); + const biblioteca = new Biblioteca(livrosDisponiveisElement, emprestimosAtivosElement); + const livro1 = new Livro(1, "A Revolução dos Bichos", "George Orwell"); + const livro2 = new Livro(2, "O Senhor dos Anéis", "J.R.R. Tolkien"); + const livro3 = new Livro(3, "Harry Potter", "J.K. Rowling"); + const livro4 = new Livro(4, "1984", "George Orwell"); + const livro5 = new Livro(5, "Dom Quixote", "Miguel de Cervantes"); + const livro6 = new Livro(6, "Orgulho e Preconceito", "Jane Austen"); + const livro7 = new Livro(7, "Crime e Castigo", "Fyodor Dostoevsky"); + const livro8 = new Livro(8, "O Pequeno Príncipe", "Antoine de Saint-Exupéry"); + const livro9 = new Livro(9, "Cem Anos de Solidão", "Gabriel García Márquez"); + biblioteca.adicionarLivro(livro1); + biblioteca.adicionarLivro(livro2); + biblioteca.adicionarLivro(livro3); + biblioteca.adicionarLivro(livro4); + biblioteca.adicionarLivro(livro5); + biblioteca.adicionarLivro(livro6); + biblioteca.adicionarLivro(livro7); + biblioteca.adicionarLivro(livro8); + biblioteca.adicionarLivro(livro9); + const aluno1 = new Aluno("Lisandra", "lisandra@example.com", "123", "A28"); + const aluno2 = new Aluno("Maria", "maria@example.com", "123", "A29"); + biblioteca.adicionarAluno(aluno1); + biblioteca.adicionarAluno(aluno2); + const alugarLivroForm = document.getElementById("alugarLivroForm"); + alugarLivroForm.addEventListener("submit", (event) => { + event.preventDefault(); + const idLivroSelecionado = parseInt(document.getElementById("livro").value); + const matricula = document.getElementById("matricula") + .value; + const senha = document.getElementById("senha").value; + const livro = (biblioteca.encontrarLivro(idLivroSelecionado) || + {}); + const aluno = (biblioteca.encontrarAluno(matricula) || {}); + biblioteca.realizarEmprestimo(livro, aluno, senha); + }); +}); diff --git a/dist/livro.js b/dist/livro.js new file mode 100644 index 0000000..a99869e --- /dev/null +++ b/dist/livro.js @@ -0,0 +1,27 @@ +"use strict"; +class Livro { + constructor(id, titulo, autor, estaDisponivel = true) { + this.id = id; + this.titulo = titulo; + this.autor = autor; + this.estaDisponivel = estaDisponivel; + } + emprestarLivro() { + if (this.estaDisponivel) { + this.estaDisponivel = false; + } + else { + alert(`O livro "${this.titulo}" não está disponível para emprestimo.`); + } + } + // --------- [Não mexer] Responsaveis por renderizar no html + criarElementoHTML() { + const li = document.createElement("li"); + li.innerHTML = `${this.titulo} (Autor: ${this.autor})`; + li.classList.add("livro-item"); + if (!this.estaDisponivel) { + li.classList.add("emprestado"); + } + return li; + } +} diff --git a/dist/usuario.js b/dist/usuario.js new file mode 100644 index 0000000..a4cdbcf --- /dev/null +++ b/dist/usuario.js @@ -0,0 +1,8 @@ +"use strict"; +class Usuario { + constructor(nome, email, senha) { + this.nome = nome; + this.email = email; + this.senha = senha; + } +} diff --git a/src/aluno.ts b/src/aluno.ts new file mode 100644 index 0000000..79ba0ac --- /dev/null +++ b/src/aluno.ts @@ -0,0 +1,9 @@ +class Aluno extends Usuario { + matricula: string; + + constructor(nome: string, email: string, senha: string, matricula: string) { + super(nome, email, senha); + this.matricula = matricula; + } + +} \ No newline at end of file diff --git a/src/biblioteca.ts b/src/biblioteca.ts index 0002f28..6aca7c7 100644 --- a/src/biblioteca.ts +++ b/src/biblioteca.ts @@ -1,9 +1,19 @@ -/* +class Biblioteca { + livros: Livro[] = []; + // livros: Array = []; + alunos: Aluno[] = []; + emprestimos: Emprestimo[] = []; + + constructor( + public livrosDisponiveisElement: HTMLUListElement, + public emprestimosAtivosElement: HTMLUListElement + ) { } + // --------- [Não mexer] Responsaveis por renderizar no html private renderizarLivrosDisponiveis(): void { this.livrosDisponiveisElement.innerHTML = ""; - const disponiveis = this.livros.filter((livro) => livro.disponivel); + const disponiveis = this.livros.filter((livro) => livro.estaDisponivel); const selectLivro = document.getElementById("livro") as HTMLSelectElement; selectLivro.innerHTML = ""; @@ -26,13 +36,64 @@ this.emprestimos.forEach((emprestimo) => { const li = document.createElement("li"); - li.textContent = `Livro: ${emprestimo.livro.titulo}, Usuário: ${ - emprestimo.aluno.nome - }, Data de Devolução: ${emprestimo.dataDevolucao.toDateString()}`; + li.textContent = `Livro: ${emprestimo.livro.titulo}, Usuário: ${emprestimo.aluno.nome + }, Data de Devolução: ${emprestimo.dataDevolucao.toDateString()}`; this.emprestimosAtivosElement.appendChild(li); }); } // --------- [Não mexer] Responsaveis por renderizar no html - */ + adicionarLivro(livro: Livro): void { + this.livros.push(livro); + this.renderizarLivrosDisponiveis(); + } + + encontrarLivro(id: number): Livro { + const livroEncontrado = this.livros.find((livro) => livro.id === id + ) as Livro; + + return livroEncontrado; + } + + adicionarAluno(aluno: Aluno) { + this.alunos.push(aluno); + } + + encontrarAluno(matricula: string) { + return this.alunos.find((aluno) => aluno.matricula === matricula) as Aluno; + } + + realizarEmprestimo(livro: Livro, aluno: Aluno, senha: string) { + if (!aluno.matricula) { + alert("Matrícula inexistente"); + return false; + } + + if (!senha || senha !== aluno.senha) { + alert("Senha incorreta ou inexistente!"); + return false; + } + + if (!livro.estaDisponivel) { + alert(`O livro "${livro.titulo}" não está disponível para emprestimo. `); + return false; + } + + livro.emprestarLivro(); + + const dataEmprestimo = new Date(); + const dataDevolucao = new Date(); + dataDevolucao.setDate(dataDevolucao.getDate() + 7); + + const emprestimo: Emprestimo = { + livro, + aluno, + dataEmprestimo, + dataDevolucao, + } + + this.emprestimos.push(emprestimo); + this.renderizarEmprestimosAtivos(); + } +} diff --git a/src/emprestimo.ts b/src/emprestimo.ts new file mode 100644 index 0000000..0af0210 --- /dev/null +++ b/src/emprestimo.ts @@ -0,0 +1,6 @@ +interface Emprestimo{ + livro: Livro; + aluno: Aluno; + dataEmprestimo: Date; + dataDevolucao: Date; +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 2ad4ced..29f77d9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -31,8 +31,8 @@ document.addEventListener("DOMContentLoaded", () => { biblioteca.adicionarLivro(livro8); biblioteca.adicionarLivro(livro9); - const aluno1 = new Aluno("Lisandra", "lisandra@example.com", "A28", "123"); - const aluno2 = new Aluno("Maria", "maria@example.com", "A29", "123"); + const aluno1 = new Aluno("Lisandra", "lisandra@example.com", "123", "A28",); + const aluno2 = new Aluno("Maria", "maria@example.com", "123", "A29"); biblioteca.adicionarAluno(aluno1); biblioteca.adicionarAluno(aluno2); diff --git a/src/livro.ts b/src/livro.ts index 14d1172..561fd07 100644 --- a/src/livro.ts +++ b/src/livro.ts @@ -1,13 +1,28 @@ -/* -// --------- [Não mexer] Responsaveis por renderizar no html -criarElementoHTML(): HTMLLIElement { - const li = document.createElement("li"); - li.innerHTML = `${this.titulo} (Autor: ${this.autor})`; - li.classList.add("livro-item"); - if (!this.disponivel) { - li.classList.add("emprestado"); +class Livro { + constructor( + public id: number, + public titulo: string, + public autor: string, + public estaDisponivel: boolean = true + ) { } + + emprestarLivro() { + if (this.estaDisponivel) { + this.estaDisponivel = false; + } else { + alert(`O livro "${this.titulo}" não está disponível para emprestimo.`) + } } - return li; + + // --------- [Não mexer] Responsaveis por renderizar no html + criarElementoHTML(): HTMLLIElement { + const li = document.createElement("li"); + li.innerHTML = `${this.titulo} (Autor: ${this.autor})`; + li.classList.add("livro-item"); + if (!this.estaDisponivel) { + li.classList.add("emprestado"); + } + return li; + } + // --------- [Não mexer] Responsaveis por renderizar no html } -// --------- [Não mexer] Responsaveis por renderizar no html -*/ diff --git a/src/usuario.ts b/src/usuario.ts new file mode 100644 index 0000000..169ee46 --- /dev/null +++ b/src/usuario.ts @@ -0,0 +1,12 @@ +class Usuario { + nome: string; + email: string; + senha: string; + + constructor(nome: string, email: string, senha: string){ + this.nome = nome; + this.email = email; + this.senha = senha; + } + +} \ No newline at end of file