lab6-7
This commit is contained in:
BIN
Лаб6/Задание/lab6.pdf
Normal file
BIN
Лаб6/Задание/lab6.pdf
Normal file
Binary file not shown.
187
Лаб6/Сайт/index.html
Normal file
187
Лаб6/Сайт/index.html
Normal file
@@ -0,0 +1,187 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Books Information</title>
|
||||
<style>
|
||||
table {
|
||||
border-collapse: collapse;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
th,
|
||||
td {
|
||||
border: 1px solid #dddddd;
|
||||
text-align: left;
|
||||
padding: 8px;
|
||||
}
|
||||
|
||||
th {
|
||||
background-color: #f2f2f2;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div>
|
||||
<label for="N">Количество книг: </label>
|
||||
<input type="number" id="N" value="10">
|
||||
<button onclick="createBooksTable()">Сгенерировать</button>
|
||||
<br>
|
||||
<br>
|
||||
<button onclick="loadBooksFromJSON()">Загрузить из JSON</button>
|
||||
<br>
|
||||
<br>
|
||||
<button onclick="calculateBooksAge()">Посчитать возраст</button>
|
||||
<br>
|
||||
<br>
|
||||
<label for="pages">Количество страниц: </label>
|
||||
<input type="number" id="pages" value="0">
|
||||
<button onclick="deletePagesInfo()">Удалить информацию о количестве страниц</button>
|
||||
<br>
|
||||
<br>
|
||||
<h3>Поиск книг:</h3>
|
||||
<label for="author">Фамилия автора: </label>
|
||||
<input type="text" id="author">
|
||||
<button onclick="searchBooksByAuthorSurname()">Найти</button>
|
||||
<br>
|
||||
<label for="genre">Жанр: </label>
|
||||
<input type="text" id="genre">
|
||||
<button onclick="searchBooksByGenre()">Найти</button>
|
||||
<br>
|
||||
<label for="yearMin">Минимальное значение года: </label>
|
||||
<input type="number" id="yearMin">
|
||||
<label for="yearMax">Максимальное значение года: </label>
|
||||
<input type="number" id="yearMax">
|
||||
<button onclick="searchBooksByYear()">Найти</button>
|
||||
<br>
|
||||
<br>
|
||||
<button onclick="fillBooksTable(books)">Обновить</button>
|
||||
<button onclick="cancel()">Отмена</button>
|
||||
<br>
|
||||
<br>
|
||||
<button onclick="clearBooksTable()">Очистить</button>
|
||||
</div>
|
||||
|
||||
<h1>Информация о книгах</h1>
|
||||
<table id="books-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Название</th>
|
||||
<th>Автор</th>
|
||||
<th>Жанр</th>
|
||||
<th>Дата издания</th>
|
||||
<th>Количество страниц</th>
|
||||
<th>Возраст</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="books-table-body">
|
||||
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<div>
|
||||
<label for="json">JSON: </label>
|
||||
<br>
|
||||
<textarea id="json" cols="60" rows="40"></textarea>
|
||||
</div>
|
||||
|
||||
<script src="script.js"></script>
|
||||
<script>
|
||||
let lastBooks = [];
|
||||
let books = [];
|
||||
|
||||
function cancel() {
|
||||
books = structuredClone(lastBooks);
|
||||
fillBooksTable(books);
|
||||
}
|
||||
|
||||
function searchBooksByAuthorSurname() {
|
||||
const author = document.getElementById("author").value;
|
||||
const filteredBooks = findBooksByAuthor(books, author);
|
||||
fillBooksTable(filteredBooks);
|
||||
}
|
||||
|
||||
function searchBooksByGenre() {
|
||||
const genre = document.getElementById("genre").value;
|
||||
const filteredBooks = findBooksByGenre(books, genre);
|
||||
fillBooksTable(filteredBooks);
|
||||
}
|
||||
|
||||
function searchBooksByYear() {
|
||||
const yearMin = document.getElementById("yearMin").value;
|
||||
const yearMax = document.getElementById("yearMax").value;
|
||||
const filteredBooks = findBooksByPublishYearRange(books, yearMin, yearMax);
|
||||
fillBooksTable(filteredBooks);
|
||||
}
|
||||
|
||||
function createBooksTable() {
|
||||
lastBooks = structuredClone(books);
|
||||
const N = document.getElementById("N").value;
|
||||
books = createBooksArray(N);
|
||||
fillBooksTable(books);
|
||||
}
|
||||
|
||||
function loadBooksFromJSON() {
|
||||
lastBooks = structuredClone(books);
|
||||
const json = document.getElementById("json").value;
|
||||
books = JSON.parse(json);
|
||||
|
||||
books.map(book => {
|
||||
book.publishDate = new Date(book.publishDate);
|
||||
});
|
||||
|
||||
fillBooksTable(books);
|
||||
}
|
||||
|
||||
function calculateBooksAge() {
|
||||
lastBooks = structuredClone(books);
|
||||
books = addAgeInfoToBooks(books);
|
||||
fillBooksTable(books);
|
||||
}
|
||||
|
||||
function clearBooksTable() {
|
||||
lastBooks = structuredClone(books);
|
||||
books = [];
|
||||
fillBooksTable(books);
|
||||
}
|
||||
|
||||
function deletePagesInfo() {
|
||||
lastBooks = structuredClone(books);
|
||||
books = removeBooksPageCount(books, document.getElementById("pages").value);
|
||||
fillBooksTable(books);
|
||||
}
|
||||
|
||||
function fillBooksTable(books) {
|
||||
const tableBody = document.getElementById("books-table-body");
|
||||
|
||||
tableBody.innerHTML = "";
|
||||
|
||||
books.forEach(book => {
|
||||
const row = document.createElement("tr");
|
||||
let bookAge = 'Не просчитано';
|
||||
if (book.age) {
|
||||
bookAge = book.age;
|
||||
}
|
||||
row.innerHTML = `
|
||||
<td>${book.title}</td>
|
||||
<td>${book.author.name} ${book.author.surname}</td>
|
||||
<td>${book.genre}</td>
|
||||
<td>${book.publishDate.toISOString().split('T')[0]}</td>
|
||||
<td>${book.pageCount}</td>
|
||||
<td>${bookAge}</td>
|
||||
`;
|
||||
tableBody.appendChild(row);
|
||||
});
|
||||
|
||||
const json = JSON.stringify(books);
|
||||
document.getElementById("json").value = json;
|
||||
}
|
||||
|
||||
fillBooksTable(books);
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
128
Лаб6/Сайт/script.js
Normal file
128
Лаб6/Сайт/script.js
Normal file
@@ -0,0 +1,128 @@
|
||||
function generateRandomDate() {
|
||||
const year = Math.floor(Math.random() * (2024 - 1900)) + 1900;
|
||||
const month = Math.floor(Math.random() * 12) + 1;
|
||||
const day = Math.floor(Math.random() * 28) + 1;
|
||||
return new Date(year, month - 1, day);
|
||||
}
|
||||
|
||||
function generateRandomAuthor() {
|
||||
const names = [
|
||||
'Александр',
|
||||
'Екатерина',
|
||||
'Дмитрий',
|
||||
'Анна',
|
||||
'Иван',
|
||||
'Мария',
|
||||
'Сергей',
|
||||
'Ольга',
|
||||
'Павел',
|
||||
'Наталья',
|
||||
];
|
||||
const surnames = [
|
||||
'Иванов',
|
||||
'Петров',
|
||||
'Смирнов',
|
||||
'Кузнецов',
|
||||
'Васильев',
|
||||
'Соколов',
|
||||
'Михайлов',
|
||||
'Новиков',
|
||||
'Федоров',
|
||||
'Морозов',
|
||||
];
|
||||
const randomName = names[Math.floor(Math.random() * names.length)];
|
||||
const randomSurname = surnames[Math.floor(Math.random() * surnames.length)];
|
||||
return {
|
||||
name: randomName,
|
||||
surname: randomSurname,
|
||||
};
|
||||
}
|
||||
|
||||
function generateRandomBook() {
|
||||
return {
|
||||
title: 'Книга ' + Math.floor(Math.random() * 1000),
|
||||
genre: [
|
||||
'Фантастика',
|
||||
'Роман',
|
||||
'Детектив',
|
||||
'Фэнтези',
|
||||
'Приключения',
|
||||
'Исторический роман',
|
||||
'Триллер',
|
||||
'Поэзия',
|
||||
'Научно-популярная литература',
|
||||
'Юмористическая проза',
|
||||
][Math.floor(Math.random() * 6)],
|
||||
publishDate: generateRandomDate(),
|
||||
author: generateRandomAuthor(),
|
||||
pageCount: Math.floor(Math.random() * 500) + 100,
|
||||
};
|
||||
}
|
||||
|
||||
function createBooksArray(N) {
|
||||
const books = [];
|
||||
for (let i = 0; i < N; i++) {
|
||||
books.push(generateRandomBook());
|
||||
}
|
||||
return books;
|
||||
}
|
||||
|
||||
function findBooksByAuthor(books, authorLastName) {
|
||||
const filteredBooks = books.filter((book) => {
|
||||
return book.author.surname === authorLastName;
|
||||
});
|
||||
|
||||
return filteredBooks;
|
||||
}
|
||||
|
||||
function findBooksByGenre(books, genre) {
|
||||
const filteredBooks = books.filter((book) => book.genre === genre);
|
||||
|
||||
return filteredBooks;
|
||||
}
|
||||
|
||||
function findBooksByPublishYearRange(books, startYear, endYear) {
|
||||
const filteredBooks = books.filter((book) => {
|
||||
const publishYear = book.publishDate.getFullYear();
|
||||
return publishYear >= startYear && publishYear <= endYear;
|
||||
});
|
||||
|
||||
return filteredBooks;
|
||||
}
|
||||
|
||||
function removeBooksPageCount(books, minPageCount) {
|
||||
for (let i = 0; i < books.length; i++) {
|
||||
if (books[i].pageCount < minPageCount) {
|
||||
delete books[i].pageCount;
|
||||
}
|
||||
}
|
||||
|
||||
return books;
|
||||
}
|
||||
|
||||
function addAgeInfoToBooks(books) {
|
||||
const currentDate = new Date();
|
||||
books.forEach((book) => {
|
||||
const yearsSincePublication =
|
||||
currentDate.getFullYear() - book.publishDate.getFullYear();
|
||||
book.age = yearsSincePublication;
|
||||
});
|
||||
|
||||
return books;
|
||||
}
|
||||
|
||||
// const booksArray = createBooksArray(10);
|
||||
// console.log('Initial array of books:');
|
||||
// displayBooks(booksArray);
|
||||
|
||||
// findBooksByAuthor(booksArray, 'Smith');
|
||||
// findBooksByGenre(booksArray, 'Fantasy');
|
||||
// findBooksByPublishYearRange(booksArray, 2000, 2010);
|
||||
|
||||
// booksArray = removeBooksByPageCount(booksArray, 200);
|
||||
// console.log('Array after removing books by page count:');
|
||||
// displayBooks(booksArray);
|
||||
|
||||
// booksArray = addAgeInfoToBooks(booksArray);
|
||||
// console.log('Array after adding age info:');
|
||||
// displayBooks(booksArray);
|
||||
Reference in New Issue
Block a user