Quick actions

cmd+k|ctrl+k

Navigation

Languages

Ride Sharing Design

Snippet info

Language

JavaScript

Visibility

public

Author

soham57

Created

2022-07-05T04:20:18.870125Z

Updated

2022-07-05T14:45:43.1932Z

const RideStatus={
    IDLE:"IDLE",
    CREATED:"CREATED",
    WITHDRAWN:"WITHDRAWN",
    COMPLETED:"COMPLETED"
}

class Ride{
   #id;
   #origin;
   #destination;
   #seats;
   #status=RideStatus.IDLE;
    static AMT_PER_KM=20   

   calculateFare(isPrivilleged){
       const totalKm=this.#destination-this.#origin;
     //   console.log("FARE",this.#destination,this.#origin,totalKm,Ride.AMT_PER_KM,this.#seats,isPrivilleged);

    if(this.#seats>1){
        return totalKm*Ride.AMT_PER_KM*this.#seats* (isPrivilleged?0.5:0.75);
    }
    
    return totalKm*Ride.AMT_PER_KM*(isPrivilleged?0.75:1);
       
   }
   
   get getDesitnation(){
       return this.#destination;
   }
   
   set setDestination(destination){
       this.#destination=destination;
       //console.log(destination,"Set d")
   }
   
   get getId(){
       return this.#id;
   }
   
   set setId(id){
       this.#id=id;
       //console.log(id,"Set id")
   }
   
   get getOrigin(){
       return this.#origin;
   }
   
   set setOrigin(origin){
       this.#origin=origin;
      // console.log(origin,"Set or")
   }   
   
   get getSeats(){
       return this.#seats;
   }
   
   set setSeats(seats){
       this.#seats=seats;
      // console.log(seats,"Set seats")
   }
   
   
   get getStatus(){
       return this.#status;
   }
   
   set setStatus(status){
       this.#status=RideStatus[status];
      // console.log(status,"Set status")
   }
}

class Person{
    constructor(name){
        this.name=name;
    }
}

class Rider extends Person{
    #completedRides;
    #currentRide;
    
    constructor(name){
        super(name)
        this.name=name;
        this.#currentRide=new Ride();
        this.#completedRides=[];
    }
    
    createRide(id,origin, destination, seats){
        
        if(origin>=destination){
            console.log("Cannot create RIDE: Invalid ride values");
            return 
        }
        
        this.#currentRide.setId=id;
        this.#currentRide.setOrigin=origin;
        this.#currentRide.setDestination=destination;
        this.#currentRide.setSeats=seats;
        this.#currentRide.setStatus="CREATED";
    }
    
    updateRide(id,origin, destination, seats){
        if(this.#currentRide.getStatus==="WITHDRAWN"||this.#currentRide.getStatus==="COMPLETED"){
            console.log(`Cannot update the ride. Ride was ${this.#currentRide.getStatus}`);
            return;
        }
        
        this.createRide(id,origin, destination, seats);
        
    }
    withdrawRide(id){
        if(this.#currentRide.getId!==id){
            console.log("Invalid Id current ride can't be withdrawn");
        }
        
        if(this.#currentRide.getStatus!=="CREATED"){
            console.log("Ride wasn't in progress. Can't withdraw ride");
            return
        }
        
        this.#currentRide.setStatus=("WITHDRAWN");
        //console.log(this.#currentRide.getStatus)
    }
    closeRide(){
        if(this.#currentRide.getStatus!=="CREATED"){
            console.log("Ride wasn't in progress. Can't close ride");
            return 0;
        }
        this.#currentRide.setStatus=("COMPLETED");
        this.#completedRides.push(this.#currentRide);
        return this.#currentRide.calculateFare( this.#completedRides.length>=10);
        
    }
}

class Driver extends Person{
    
}


const rider =new Rider("Aman");
const driver= new Driver("Hari");

rider.createRide(1,50,60,1);
console.log(rider.closeRide());
rider.updateRide(1,50,90,2);

console.log("***************************************************");

rider.createRide(1,50,60,1);
rider.withdrawRide(1);
rider.updateRide(1,50,60,2);
console.log(rider.closeRide());

console.log("***************************************************");

rider.createRide(1,50,60,1);
rider.updateRide(1,50,90,2);
console.log(rider.closeRide());
INFO