Advertisement
Danny_Berova

4.RealEstateAgency

Nov 19th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. function realEstateAgency () {
  2. let profit = $('h1').eq(0);
  3. let profitSum = 0;
  4. //let roof = $('#roof');
  5. let building = $('#building');
  6. let message = $('#message');
  7. let offers = [];
  8.  
  9. let price = $('input[name=apartmentRent]');
  10. let apratment = $('input[name=apartmentType]');
  11. let commRate = $('input[name=agencyCommission]');
  12.  
  13. let familyBudget = $('input[name=familyBudget]');
  14. let familyApartType = $('input[name=familyApartmentType]');
  15. let family = $('input[name=familyName]');
  16.  
  17. let regOffer = $('button[name=regOffer]');
  18. regOffer.on('click', function() {
  19.  
  20. let allPresent = price.val() !== undefined && apratment.val() !== undefined && commRate.val() !== undefined;
  21. let bothAreNums = typeof +price.val() === 'number' && typeof +commRate.val() === 'number'
  22. let validApartmentType = apratment.val() !== ' ' && !apratment.val().includes(':');
  23.  
  24. if(allPresent && validApartmentType && +price.val() > 0 &&
  25. bothAreNums && +commRate.val() >= 0 && +commRate.val() <= 100 ) {
  26. let buildDiv = $('<div>').addClass('apartment');
  27. let p1 = $('<p>').text(`Rent: ${price.val()}`);
  28. let p2 = $('<p>').text(`Type: ${apratment.val()}`);
  29. let p3 = $('<p>').text(`Commission: ${commRate.val()}`);
  30. buildDiv.append(p1, p2, p3);
  31. building.append(buildDiv);
  32.  
  33.  
  34. message.text('Your offer was created successfully.');
  35. } else {
  36. message.text('Your offer registration went wrong, try again.');
  37. }
  38.  
  39. price.val('');
  40. apratment.val('');
  41. commRate.val('');
  42. });
  43. let findOffer = $('button[name=findOffer]');
  44. findOffer.on('click', function() {
  45.  
  46. let apps = $('div.apartment');
  47. let searched = familyApartType.val()
  48.  
  49. let isIn = false;
  50. for (let app of apps) {
  51.  
  52. let rentVal = $(app).find('p').eq(0).text().substring(6);
  53. let typeVal = $(app).find('p').eq(1).text().substring(6);
  54. let rateVal = $(app).find('p').eq(2).text().substring(12);
  55.  
  56. let ratePercentVal = +rentVal + (+rentVal * (+rateVal / 100));
  57. let isBudgetEnough = ratePercentVal <= +familyBudget.val();
  58.  
  59. if(searched === typeVal && isBudgetEnough) {
  60. isIn = true;
  61. }
  62. console.log(isIn)
  63. }
  64.  
  65. let allPresent = familyBudget.val() !== undefined && familyApartType.val() !== undefined && family.val() !== undefined;
  66. if(allPresent && familyApartType.val() !== ' ' && family.val() !== ' ' && +familyBudget.val() > 0 && isIn) {
  67. for (let app of apps) {
  68. let rent = $(app).find('p').eq(0).text().substring(6);
  69. let type = $(app).find('p').eq(1).text().substring(6);
  70. let rate = $(app).find('p').eq(2).text().substring(12);
  71.  
  72. let percent = +rent * (+rate / 100);
  73. let ratePercent = +rent + (+rent * (+rate / 100));
  74.  
  75. let isBudgetEnough = ratePercent <= +familyBudget.val();
  76. if(type === familyApartType.val() && isBudgetEnough) {
  77. profitSum += percent * 2;
  78. profit.text(`Agency profit: ${profitSum} lv.`);
  79.  
  80. $(app).find('p').eq(0).text(`${family.val()}`);
  81. $(app).find('p').eq(1).text('live here now');
  82. $(app).find('p').eq(2).remove();
  83. $(app).attr('style', "border: 2px solid red;");
  84. $(app).append($('<button>').text('MoveOut').on('click', function(ev) {
  85. let thisApp = ev.target.parentNode;
  86. console.log('inside MoveOut')
  87. message.text(`They had found cockroaches in ${$(thisApp).find('p').eq(0).text()}\'s apartment`);
  88. $(thisApp).remove();
  89. }))
  90. }
  91. }
  92.  
  93. message.text('Enjoy your new home! :))');
  94.  
  95. } else {
  96. message.text('We were unable to find you a home, so sorry :(');
  97. }
  98.  
  99. familyBudget.val('');
  100. familyApartType.val('');
  101. family.val('');
  102. });
  103.  
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement