Advertisement
vallec

Untitled

Apr 3rd, 2024
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. class BikeRentalService {
  2. constructor(name, location) {
  3. this.name = name;
  4. this.location = location;
  5. this.availableBikes = [];
  6. }
  7.  
  8. addBikes(bikes) {
  9. const bikesTemp = [];
  10. bikes.map(bike => {
  11.  
  12. const props = bike.split('-');
  13. const bikeBrand = props[0];
  14. const bikeQuantity = Number(props[1]);
  15. const bikePrice = props[2];
  16. const bikeFound = this.availableBikes.find(x => x.brand === bikeBrand);
  17. const foundIndex = this.availableBikes.findIndex(x => x.brand === bikeBrand)
  18. if(bikeFound && bikeFound.quantity < bikeQuantity) {
  19. this.availableBikes[foundIndex].quantity += bikeQuantity;
  20. this.availableBikes[foundIndex].price = bikePrice;
  21. } else {
  22. this.availableBikes.push({brand: bikeBrand, quantity: Number(bikeQuantity), price: bikePrice})
  23. bikesTemp.push(bikeBrand);
  24. }
  25. })
  26. return `Successfully added ${bikesTemp.join(', ')}`
  27. }
  28.  
  29. rentBikes(selectedBikes) {
  30. let totalPrice = 0;
  31. for(let i = 0; i < selectedBikes.length; i++) {
  32. const props = selectedBikes[i].split('-');
  33. const bikeBrand = props[0];
  34. const bikeQuantity = Number(props[1]);
  35. const bikeFound = this.availableBikes.find(x => x.brand === bikeBrand);
  36. const foundIndex = this.availableBikes.findIndex(x => x.brand === bikeBrand)
  37.  
  38. if(!bikeFound || (bikeFound && bikeFound.quantity < bikeQuantity)) {
  39. return "Some of the bikes are unavailable or low on quantity in the bike rental service."
  40. } else {
  41. totalPrice += bikeQuantity * parseFloat(bikeFound.price)
  42. this.availableBikes[foundIndex].quantity -= bikeQuantity;
  43. }
  44. }
  45.  
  46. return `Enjoy your ride! You must pay the following amount $${(Math.round(totalPrice * 100) / 100).toFixed(2)}.`
  47. }
  48.  
  49. returnBikes (returnedBikes) {
  50. for(let i = 0; i < returnedBikes.length; i++) {
  51. const props = returnedBikes[i].split('-');
  52. const bikeBrand = props[0];
  53. const bikeQuantity = Number(props[1]);
  54. const bikeFound = this.availableBikes.find(x => x.brand === bikeBrand);
  55. const foundIndex = this.availableBikes.findIndex(x => x.brand === bikeBrand)
  56.  
  57. if(!bikeFound) {
  58. return "Some of the returned bikes are not from our selection."
  59. } else {
  60. this.availableBikes[foundIndex].quantity += bikeQuantity;
  61. }
  62. }
  63.  
  64. return "Thank you for returning!";
  65. }
  66.  
  67. revision() {
  68. let output = `Available bikes:\n`;
  69. this.availableBikes.sort(compare).map(bike => {
  70. output += `${bike.brand} quantity:${bike.quantity} price:$${bike.price}\n`;
  71. })
  72. output += `The name of the bike rental service is ${this.name}, and the location is ${this.location}.`;
  73.  
  74. return output;
  75. }
  76. }
  77.  
  78. function compare( a, b ) {
  79. if ( a.quantity < b.quantity ){
  80. return 1;
  81. }
  82. if ( a.quantity > b.quantity ){
  83. return -1;
  84. }
  85. return 0;
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement