Advertisement
Kamend1

01.Farm Management System

Apr 7th, 2025
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve (commandArr) {
  2.     class Farmer {
  3.         constructor (name, location) {
  4.             this.name = name;
  5.             this.location = location;
  6.             this.skills = []
  7.         }
  8.     }
  9.  
  10.     let farmerArr = [];
  11.     let numFarmers = Number(commandArr[0]);
  12.  
  13.     for (let idx = 1; idx <= numFarmers; idx++) {
  14.         let farmerStr = commandArr[idx];
  15.         let [name, location, skills] = farmerStr.split(' ');
  16.         let farmer = new Farmer(name, location);
  17.         let skillArr = skills.split(',');
  18.  
  19.         for (let skill of skillArr) {
  20.             farmer.skills.push(skill);
  21.         }
  22.         farmerArr.push(farmer);
  23.     }
  24.  
  25.     for (let idx = numFarmers + 1; idx < commandArr.length; idx++) {
  26.         currentCommandTokens = commandArr[idx].split(' / ');
  27.         if (currentCommandTokens[0] === "End") {
  28.             break;
  29.         } else if (currentCommandTokens[0] === "Execute") {
  30.             let [_, name, location, task] = currentCommandTokens;
  31.             let currentFarmer = farmerArr.find(p => p.name === name);
  32.  
  33.             if (!currentFarmer) {
  34.                 continue;
  35.             }
  36.  
  37.             if (currentFarmer.location !== location) {
  38.                 console.log(`${currentFarmer.name} cannot execute the task: ${task}.`);
  39.                 continue;
  40.             }
  41.  
  42.             if (!currentFarmer.skills.includes(task)) {
  43.                 console.log(`${currentFarmer.name} cannot execute the task: ${task}.`);
  44.                 continue;
  45.             }
  46.  
  47.             console.log(`${currentFarmer.name} has executed the task: ${task}!`)
  48.  
  49.         } else if (currentCommandTokens[0] === "Change Area") {
  50.             let [_, name, location] = currentCommandTokens;
  51.             let currentFarmer = farmerArr.find(p => p.name === name);
  52.            
  53.             if (!currentFarmer) {
  54.                 continue;
  55.             }
  56.  
  57.             if (currentFarmer.location !== location) {
  58.                 currentFarmer.location = location;
  59.                 console.log(`${currentFarmer.name} has changed their work area to: ${location}`);
  60.             }
  61.  
  62.         } else if (currentCommandTokens[0] === "Learn Task") {
  63.             let [_, name, task] = currentCommandTokens;
  64.             let currentFarmer = farmerArr.find(p => p.name === name);
  65.            
  66.             if (!currentFarmer) {
  67.                 continue;
  68.             }
  69.  
  70.             if (currentFarmer.skills.includes(task)) {
  71.                 console.log(`${currentFarmer.name} already knows how to perform ${task}.`);
  72.             } else {
  73.                 currentFarmer.skills.push(task);
  74.                 console.log(`${currentFarmer.name} has learned a new task: ${task}.`);
  75.             }
  76.         }
  77.     }
  78.  
  79.     for (let farmer of farmerArr) {
  80.         console.log(`Farmer: ${farmer.name}, Area: ${farmer.location}, Tasks: ${farmer.skills.sort().join(', ')}`);
  81.     }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement