class Library {
constructor() {
this.books = {};
}
}
class AddBook extends Library {
update(params) {
const [name, yearPublished, copies] = params;
if (this.books[name]) {
this.books[name].countOfBooks += parseInt(copies);
} else {
this.books[name] = {
name: name,
yearPublished: parseInt(yearPublished),
countOfBooks: parseInt(copies),
countOfBooksIssued: 0
};
}
return JSON.stringify(this.books[name]);
}
}
class IssueBook extends Library {
update(params) {
const [name, yearPublished] = params;
if (this.books[name] && this.books[name].countOfBooksIssued < this.books[name].countOfBooks) {
this.books[name].countOfBooksIssued++;
return "true";
}
return "false";
}
}
class ReturnBook extends Library {
update(params) {
const [name, yearPublished, issueDate, returnDate] = params;
const book = this.books[name];
if (!book) return "0";
const issueTime = new Date(issueDate).getTime();
const returnTime = new Date(returnDate).getTime();
const diffDays = Math.floor((returnTime - issueTime) / (1000 * 60 * 60 * 24));
if (diffDays <= 30) {
book.countOfBooksIssued--;
return "0";
}
const overdueDays = diffDays - 30;
const weeksOverdue = Math.floor(overdueDays / 7);
let fine = overdueDays + Math.sqrt(weeksOverdue);
if (overdueDays > 180) fine += 50;
book.countOfBooksIssued--;
return Math.floor(fine).toString();
}
}
function processInput(inputString, ab, ib, rb) {
const params = inputString.split(" ");
if (params.length === 3) {
return ab.update(params);
} else if (params.length === 2) {
return ib.update(params);
} else if (params.length === 4) {
return rb.update(params);
}
}
function main() {
const fs = require('fs');
const inputString = fs.readFileSync(process.env.INPUT_PATH, 'utf-8').trim().split('\n');
const queries = parseInt(inputString[0], 10);
const ab = new AddBook();
const ib = new IssueBook();
const rb = new ReturnBook();
let content = '';
for (let i = 1; i <= queries; i++) {
const query = inputString[i];
content += processInput(query, ab, ib, rb) + '\n';
}
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
ws.write(content);
ws.end();
}