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
7 changes: 7 additions & 0 deletions dist/aluno.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";
class Aluno extends Usuario {
constructor(nome, email, senha, matricula) {
super(nome, email, senha);
this.matricula = matricula;
}
}
76 changes: 76 additions & 0 deletions dist/biblioteca.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"use strict";
class Biblioteca {
constructor(livrosDisponiveisElement, emprestimosAtivosElement) {
this.livrosDisponiveisElement = livrosDisponiveisElement;
this.emprestimosAtivosElement = emprestimosAtivosElement;
this.livros = [];
// livros: Array<Livro> = [];
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();
}
}
1 change: 1 addition & 0 deletions dist/emprestimo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"use strict";
40 changes: 40 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -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", "[email protected]", "123", "A28");
const aluno2 = new Aluno("Maria", "[email protected]", "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);
});
});
27 changes: 27 additions & 0 deletions dist/livro.js
Original file line number Diff line number Diff line change
@@ -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 = `<span>${this.titulo}</span> (Autor: ${this.autor})`;
li.classList.add("livro-item");
if (!this.estaDisponivel) {
li.classList.add("emprestado");
}
return li;
}
}
8 changes: 8 additions & 0 deletions dist/usuario.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use strict";
class Usuario {
constructor(nome, email, senha) {
this.nome = nome;
this.email = email;
this.senha = senha;
}
}
9 changes: 9 additions & 0 deletions src/aluno.ts
Original file line number Diff line number Diff line change
@@ -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;
}

}
73 changes: 67 additions & 6 deletions src/biblioteca.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
/*
class Biblioteca {
livros: Livro[] = [];
// livros: Array<Livro> = [];
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 = "";
Expand All @@ -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();
}
}
6 changes: 6 additions & 0 deletions src/emprestimo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
interface Emprestimo{
livro: Livro;
aluno: Aluno;
dataEmprestimo: Date;
dataDevolucao: Date;
}
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ document.addEventListener("DOMContentLoaded", () => {
biblioteca.adicionarLivro(livro8);
biblioteca.adicionarLivro(livro9);

const aluno1 = new Aluno("Lisandra", "[email protected]", "A28", "123");
const aluno2 = new Aluno("Maria", "[email protected]", "A29", "123");
const aluno1 = new Aluno("Lisandra", "[email protected]", "123", "A28",);
const aluno2 = new Aluno("Maria", "[email protected]", "123", "A29");

biblioteca.adicionarAluno(aluno1);
biblioteca.adicionarAluno(aluno2);
Expand Down
37 changes: 26 additions & 11 deletions src/livro.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
/*
// --------- [Não mexer] Responsaveis por renderizar no html
criarElementoHTML(): HTMLLIElement {
const li = document.createElement("li");
li.innerHTML = `<span>${this.titulo}</span> (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 = `<span>${this.titulo}</span> (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
*/
12 changes: 12 additions & 0 deletions src/usuario.ts
Original file line number Diff line number Diff line change
@@ -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;
}

}