Time spent here:

Friday, 17 June 2016

TAXI PROBLEM


PROBLEM STATEMENT:

 Design a Call taxi booking application

-There are n number of taxi’s. For simplicity, assume 4. But it should work for any number of taxi’s.

-The are 6 points(A,B,C,D,E,F)

-All the points are in a straight line, and each point is 15kms away from the adjacent points.



-It takes 60 mins to travel from one point to another

-Each taxi charges Rs.100 minimum for the first 5 kilometers and Rs.10 for the subsequent kilometers.

-For simplicity, time can be entered as absolute time. Eg: 9hrs, 15hrs etc.
-All taxi’s are initially stationed at A.

-When a customer books a Taxi, a free taxi at that point is allocated

-If no free taxi is available at that point, a free taxi at the nearest point is allocated.

-If two taxi’s are free at the same point, one with lower earning is allocated

-Note that the taxi only charges the customer from the pickup point to the drop point. Not the distance it travels from an adjacent point to pickup the customer.

-If no taxi is free at that time, booking is rejected

Design modules for

1)    Call taxi booking
Input 1:
Customer ID: 1
Pickup Point: A
Drop Point: B
Pickup Time: 9

Output 1:
Taxi can be allotted.
Taxi-1 is allotted

Input 2:
Customer ID: 2
Pickup Point: B
Drop Point: D
Pickup Time: 9

Output 1:
Taxi can be allotted.
Taxi-2 is allotted

(Note: Since Taxi-1 would have completed its journey when second booking is done, so Taxi-2 from nearest point A which is free is allocated)

Input 3:
Customer ID: 3
Pickup Point: B
Drop Point: C
Pickup Time: 12

Output 1:
Taxi can be allotted.
Taxi-1 is allotted



2) Display the Taxi details


Taxi No:    Total Earnings:
BookingID    CustomerID    From    To    PickupTime    DropTime    Amount
  
Output:
Taxi-1    Total Earnings: Rs. 400

1     1     A    B    9    10    200
3    3    B    C    12    13    200

Taxi-2 Total Earnings: Rs. 350
2    2    B    D    9    11    350



These were just sample inputs. It should work for any input that they give.

CODE:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<limits.h>
struct taxi_Booking{
    int booking_id[10];
    int customer_id[10];
    char source[10],destination[10];
    int pick_time[10],drop_time[10];
    int wages[10],total_fare;
    char location;
    int current_time,num_of_bookings;
  //  void (*booking)(struct taxi_Booking *);
    //void (*allocation)(struct taxi_Booking *);
};
int c_id=1,b_id=1,p_time;
char p_point,d_point;

void display(struct taxi_Booking *taxi){
    int i,j;
    printf("\nBooking id    Customer id        From         To         P_time        D_time        Amount\n\n");   
    for(j=1;j<=4;j++){
        if(taxi[j].total_fare){
            printf("Taxi No.: %d     Total Earnings: %d\n",j,taxi[j].total_fare);
            for(i=1;i<=taxi[j].num_of_bookings;i++){
                printf("\n%d    %d    %c    %c    %d    %d    %d",taxi[j].booking_id[i],taxi[j].customer_id[i],taxi[j].source[i],
                taxi[j].destination[i],taxi[j].pick_time[i],taxi[j].drop_time[i],taxi[j].wages[i]);
                printf("\n");
            }
            printf("\n");
        }
    }   
}
void alloc_fun(struct taxi_Booking *taxi,int booking_pos){
            taxi[booking_pos].num_of_bookings++;
            int n=taxi[booking_pos].num_of_bookings;
            taxi[booking_pos].booking_id[n]=b_id-1;
            taxi[booking_pos].customer_id[n]=c_id-1;
            taxi[booking_pos].source[n]=p_point;   
            taxi[booking_pos].destination[n]=d_point;
            taxi[booking_pos].pick_time[n]=p_time;
            taxi[booking_pos].drop_time[n]=p_time+abs(p_point-d_point);
            taxi[booking_pos].wages[n]=(((abs(p_point-d_point)*15)-5)*10)+100;
            taxi[booking_pos].total_fare+=taxi[booking_pos].wages[n];
            taxi[booking_pos].location=d_point;
            taxi[booking_pos].current_time=taxi[booking_pos].drop_time[n];
           
           
}
void initiate(struct taxi_Booking *taxi){
    int i;
    for(i=1;i<=4;i++){
        taxi[i].total_fare=0;
        taxi[i].location='A';
        taxi[i].num_of_bookings=0;
    }
}

void booking_fun(struct taxi_Booking *taxi){
      int min_loc=INT_MAX,min_fare=INT_MAX,cnt=0,booking_pos,i,alloc;
        printf("\nInput: %d",b_id++);
      printf("\nCustomer_id: %d",c_id++);     
      printf("\nPickup Point:");
      scanf("\n%c",&p_point);
      printf("Drop Point:");
      scanf("\n%c",&d_point);
      printf("Pick_Time:");
      scanf("%d",&p_time);

      for(i=1;i<=4;i++){
            //if(taxi[i].current_time<=p_time){
                if(abs(p_point-taxi[i].location)<=min_loc && (abs(p_point-taxi[i].location)+taxi[i].current_time)<=p_time){
                    min_loc=abs(p_point-taxi[i].location);
                    if(taxi[i].total_fare<min_fare){
                            min_fare=taxi[i].total_fare;
                            booking_pos=i;
                    }
                }
            //}
            else{
                cnt++;
            }
      }
      if(cnt<4){
            printf("\nTaxi can be alloted\nTaxi-%d is allocated",booking_pos);
            //taxi[0].allocation=alloc_fun;
            alloc_fun(taxi,booking_pos);
           
      }
      else{
            printf("\nBooking is rejected");
            b_id--;
            c_id--;
      }       
}

int main(){
    struct taxi_Booking taxi[4];
    int c;
    initiate(taxi);
    printf("\n<<<<<<<<<<Welcome To Taxi Booking>>>>>>>>>>>>\n");
    printf("\nTaxi will be booked only for customers to travel between the below stations.\n");
    printf("\n-->Nearby stations are A,B,C,D,E and F.\n");
    printf("-->Taxi fare will be charged Rs.100 for first 5kms and for rest Rs.10 per km will be charged.\n");
    printf("-->Your booking will be rejected if taxies are not available at that time.\n");
    printf("\nNOTE: Station names are CASE_SENSITIVE,So please enter the valid station names.\n");
    printf("\nPick time should be in 24 hours format. It should be greater then 0\n\n");
    while(1){
      printf("\n\nPress 1 to continue or 2 to display taxi details or 0 to Exit:");
      scanf("%d",&c);
      if(c==0){
            exit(0);
      }
      else if(c==1){
              //taxi[0].booking=booking_fun;
            booking_fun(taxi);
      }
      else
            display(taxi);
    }
    return 0;
}

No comments:

Post a Comment