Advertisement
196040

NVD 1 kolokvium practice 2 oop

Nov 24th, 2021
1,267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class ObjFilters {
  2.   constructor(name) {
  3.     this.name = name;
  4.     this.id = hashCode(name);
  5.     this.filters = false;
  6. }
  7. }
  8. class PollutionObject extends ObjFilters {
  9.   constructor(name, date, municipality, temperature, PM10, NO2, O3, SO2) {
  10.     super(name);
  11.     this.date = date;
  12.     this.temperature = temperature;
  13.     this.municipality = municipality;
  14.     this.PM10 = PM10;
  15.     this.NO2 = NO2;
  16.     this.O3 = O3;
  17.     this.SO2 = SO2;
  18.   }  
  19.   rizik() {
  20.     if(this.PM10 && this.NO2 && this.O3 && this.SO2 && this.temperature<10)
  21.     return "The object  "+this.id+" " + this.name + " pollutes the air at high risk";
  22.     if(this.PM10 && this.NO2 & this.O3 && this.SO2 && this.temperature>10)
  23.     return "The object  "+this.id+" " + this.name + " pollutes the air at average risk";
  24.     if(this.NO2 && this.SO2 && this.O3)
  25.     return "The object  "+this.id+" " + this.name + " pollutes the air at average risk";
  26.     if(this.PM10 && this.NO2 && this.O3)
  27.     return "The object  "+this.id+" " + this.name + " pollutes the air at average risk";
  28.     if(this.SO2 && this.O3)
  29.     return "The object  "+this.id+" " + this.name + " pollutes the air at low risk"
  30.     if(this.PM10 && this.NO2 && this.temperature>10)
  31.     return "The object  "+this.id+" " + this.name + " pollutes the air at low risk"
  32.     if(this.PM10 == false && this.NO2 == false && this.SO2 == false && this.O3 == false)
  33.     return "The object  "+this.id+" " + this.name + " pollutes the air at low risk (eco friendly)"
  34.   }
  35.   toString () {
  36.   var ret="Object "+this.id.toString() + " " + this.name.toString();//Object %id %name has/hasn't got  filters
  37.   if(this.filters) ret=ret+"has got filters";
  38.   else ret=ret+"hasn't got filters";
  39.   return ret;
  40. }
  41. }
  42. class AirPollution {
  43. constructor() {
  44. this.listofobj = [];
  45. }
  46. add_object(a) {
  47. this.listofobj.push(a);
  48. }
  49. test_object(a) {
  50. var help = a.date+"";
  51. help = help.substring(4,7);
  52. if(!(help == "Dec" || help == "Nov" || help == "Oct"))
  53. a.filters = true;
  54. }
  55. check() {
  56. for(var i=0; i<this.listofobj.length; i++){
  57.   this.test_object(this.listofobj[i]);
  58.   console.log(this.listofobj[i].rizik());
  59. }
  60. }
  61. safe_zone(opstina){
  62. var count = 0;
  63. var fil = 0;
  64. for(var i=0; i<this.listofobj.length; i++){
  65. if(this.listofobj[i].municipality == opstina)
  66.   count++;
  67.   if(this.listofobj.filters == true)
  68.   fil++;
  69. }
  70. if(fil!=0)
  71. return "You are in a " + 100/fil + "% safe zone";
  72. return "You are in a 100% safe zone";
  73. }print() {
  74.       for (var i=0;i<this.listofobj.length;i++)
  75.       {
  76.           console.log("PollutionObject {");
  77.           var h = this.listofobj[i];
  78.           var count = 0;
  79.           for(var k in h) {
  80.             if (k != "add_object" && k != "toString" && k!="safe_zone" && k!="check" && k!="test_object")
  81.             count++;
  82.           }
  83.           var c = 0;
  84.           for (var j in h)
  85.           {
  86.               if (j != "add_object" && j != "toString" && j!="safe_zone" && j!="check" && j!="test_object"){
  87.                 if(j=="name" || j == "municipality") {
  88.                  if(c!=count-1){
  89.                   console.log("  " + j + ": " + "'"+ h[j] + "',");
  90.               }
  91.                   else
  92.                   console.log("  " + j + ": " + "'"+ h[j] + "'");
  93.                 }
  94.                 else{
  95.                if(c!=this.count-1){
  96.                 console.log("  " + j + ": " + h[j]+",");
  97.                 }
  98.                 else
  99.                 console.log("  " + j + ": " + h[j]+",");
  100.               }
  101.           }
  102.           c++;
  103.           }
  104.           console.log("}");
  105.       }
  106.   }
  107. }
  108. var hashCode = function(s){
  109. return  String(s).split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a},0);              
  110. }
  111.  
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement