fork download
  1. class Library {
  2. constructor() {
  3. this.books = {};
  4. }
  5. }
  6.  
  7. class AddBook extends Library {
  8. update(params) {
  9. const [name, yearPublished, copies] = params;
  10. if (this.books[name]) {
  11. this.books[name].countOfBooks += parseInt(copies);
  12. } else {
  13. this.books[name] = {
  14. name: name,
  15. yearPublished: parseInt(yearPublished),
  16. countOfBooks: parseInt(copies),
  17. countOfBooksIssued: 0
  18. };
  19. }
  20. return JSON.stringify(this.books[name]);
  21. }
  22. }
  23.  
  24. class IssueBook extends Library {
  25. update(params) {
  26. const [name, yearPublished] = params;
  27. if (this.books[name] && this.books[name].countOfBooksIssued < this.books[name].countOfBooks) {
  28. this.books[name].countOfBooksIssued++;
  29. return "true";
  30. }
  31. return "false";
  32. }
  33. }
  34.  
  35. class ReturnBook extends Library {
  36. update(params) {
  37. const [name, yearPublished, issueDate, returnDate] = params;
  38. const book = this.books[name];
  39. if (!book) return "0";
  40.  
  41. const issueTime = new Date(issueDate).getTime();
  42. const returnTime = new Date(returnDate).getTime();
  43. const diffDays = Math.floor((returnTime - issueTime) / (1000 * 60 * 60 * 24));
  44.  
  45. if (diffDays <= 30) {
  46. book.countOfBooksIssued--;
  47. return "0";
  48. }
  49.  
  50. const overdueDays = diffDays - 30;
  51. const weeksOverdue = Math.floor(overdueDays / 7);
  52. let fine = overdueDays + Math.sqrt(weeksOverdue);
  53. if (overdueDays > 180) fine += 50;
  54.  
  55. book.countOfBooksIssued--;
  56. return Math.floor(fine).toString();
  57. }
  58. }
  59.  
  60. function processInput(inputString, ab, ib, rb) {
  61. const params = inputString.split(" ");
  62. if (params.length === 3) {
  63. return ab.update(params);
  64. } else if (params.length === 2) {
  65. return ib.update(params);
  66. } else if (params.length === 4) {
  67. return rb.update(params);
  68. }
  69. }
  70.  
  71. function main() {
  72. const fs = require('fs');
  73. const inputString = fs.readFileSync(process.env.INPUT_PATH, 'utf-8').trim().split('\n');
  74. const queries = parseInt(inputString[0], 10);
  75. const ab = new AddBook();
  76. const ib = new IssueBook();
  77. const rb = new ReturnBook();
  78. let content = '';
  79. for (let i = 1; i <= queries; i++) {
  80. const query = inputString[i];
  81. content += processInput(query, ab, ib, rb) + '\n';
  82. }
  83. const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
  84. ws.write(content);
  85. ws.end();
  86. }
  87.  
Success #stdin #stdout 0.03s 16784KB
stdin
Standard input is empty
stdout
Standard output is empty