Advertisement
vallec

Untitled

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