Advertisement
vallec

Untitled

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