Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BikeRentalService {
- constructor(name, location) {
- this.name = name;
- this.location = location;
- this.availableBikes = [];
- }
- addBikes(bikes) {
- const bikesTemp = [];
- bikes.map(bike => {
- const props = bike.split('-');
- const bikeBrand = props[0];
- const bikeQuantity = Number(props[1]);
- const bikePrice = props[2];
- const bikeFound = this.availableBikes.find(x => x.brand === bikeBrand);
- const foundIndex = this.availableBikes.findIndex(x => x.brand === bikeBrand)
- if(bikeFound && bikeFound.quantity < bikeQuantity) {
- this.availableBikes[foundIndex].quantity += bikeQuantity;
- this.availableBikes[foundIndex].price = bikePrice;
- } else {
- this.availableBikes.push({brand: bikeBrand, quantity: Number(bikeQuantity), price: bikePrice})
- bikesTemp.push(bikeBrand);
- }
- })
- return `Successfully added ${bikesTemp.join(', ')}`
- }
- rentBikes(selectedBikes) {
- let totalPrice = 0;
- for(let i = 0; i < selectedBikes.length; i++) {
- const props = selectedBikes[i].split('-');
- const bikeBrand = props[0];
- const bikeQuantity = Number(props[1]);
- const bikeFound = this.availableBikes.find(x => x.brand === bikeBrand);
- const foundIndex = this.availableBikes.findIndex(x => x.brand === bikeBrand)
- if(!bikeFound || (bikeFound && bikeFound.quantity < bikeQuantity)) {
- return "Some of the bikes are unavailable or low on quantity in the bike rental service."
- } else {
- totalPrice += bikeQuantity * parseFloat(bikeFound.price)
- this.availableBikes[foundIndex].quantity -= bikeQuantity;
- }
- }
- return `Enjoy your ride! You must pay the following amount $${(Math.round(totalPrice * 100) / 100).toFixed(2)}.`
- }
- returnBikes (returnedBikes) {
- for(let i = 0; i < returnedBikes.length; i++) {
- const props = returnedBikes[i].split('-');
- const bikeBrand = props[0];
- const bikeQuantity = Number(props[1]);
- const bikeFound = this.availableBikes.find(x => x.brand === bikeBrand);
- const foundIndex = this.availableBikes.findIndex(x => x.brand === bikeBrand)
- if(!bikeFound) {
- return "Some of the returned bikes are not from our selection."
- } else {
- this.availableBikes[foundIndex].quantity += bikeQuantity;
- }
- }
- return "Thank you for returning!";
- }
- revision() {
- let output = `Available bikes:\n`;
- this.availableBikes.sort(compare).map(bike => {
- output += `${bike.brand} quantity:${bike.quantity} price:$${bike.price}\n`;
- })
- output += `The name of the bike rental service is ${this.name}, and the location is ${this.location}.`;
- return output;
- }
- }
- function compare( a, b ) {
- if ( a.quantity < b.quantity ){
- return 1;
- }
- if ( a.quantity > b.quantity ){
- return -1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement