Taxi application
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
Program :
import java.util.List;
public class Main {
public static void main(String[] args) {
TaxiRegistration taxiRegistration =
new TaxiRegistration();
List<BookingDetails> taxiInformation =
taxiRegistration.userRegistration();
int totalTaxi =
taxiRegistration.maximumTaxiAllocated(taxiInformation);
System.out.println(
" Taxi Number CustomerID From To PickupTime " +
"DropTime Amount " );
for(int i=1;i<=totalTaxi;i++){
for(int j=0;j<taxiInformation.size();j++){
if(i==taxiInformation.get(j).getTaxiAllocatedNumber()){
System.out.println(" "+i+" " +taxiInformation.get(j).getCustomerId() +
" "+taxiInformation.get(j).getPickUpPoint()+
" "+taxiInformation.get(j).getDropUpPoint()+
" " +
" "+taxiInformation.get(j).getPickUpTime()+
" "+taxiInformation.get(j).getDropUpTime()+
" "+taxiInformation.get(j).getTaxiFare()
);
}
}
System.out.println("---------------------------------------------------------------------------");
}
}
}
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class TaxiRegistration {
Scanner sc = new Scanner(System.in);
int customerId;
char pickUpPoint;
char dropUpPoint;
int pickUpTime;
int dropUpTime;
int taxiFare;
int taxiNumber;
boolean condition;
public TaxiRegistration() {
this.customerId = 0;
this.condition = true;
}
public List<BookingDetails> userRegistration() {
System.out.println("Welcome to taxi App ");
List<BookingDetails> totalTaxiAllocated = new ArrayList<>();
while (condition) {
System.out.println("Enter the Below details for taxi allocation");
System.out.println("Customer Id : " + ++this.customerId +"(Auto Generated)");
System.out.println("PickUp Point : ");
this.pickUpPoint = sc.next().toLowerCase().charAt(0);
System.out.println("DropUp Point : ");
this.dropUpPoint = sc.next().toLowerCase().charAt(0);
System.out.println("PickUp Time : ");
this.pickUpTime = sc.nextInt();
System.out.println();
dropUpTime = calculateDropUpTime(pickUpTime,pickUpPoint,dropUpPoint);
taxiFare = calculateTaxiFare(pickUpPoint,dropUpPoint);
int totalTaxi = maximumTaxiAllocated(totalTaxiAllocated);
taxiNumber = (customerId==1)?1 :
taxiAllocation(reArrangeToCurrentCustomer(totalTaxiAllocated,totalTaxi),pickUpTime,totalTaxi);
totalTaxiAllocated.add(
new BookingDetails(customerId, pickUpPoint, dropUpPoint, pickUpTime,dropUpTime,taxiFare,taxiNumber)
);
for (BookingDetails taxiAllocated :
totalTaxiAllocated) {
if(customerId ==taxiAllocated.getCustomerId()){
System.out.println("Taxi can be allotted.");
System.out.println("Taxi-" +taxiAllocated.getTaxiAllocatedNumber()+" is allotted");
}
}
System.out.println("-------------------------------------------------");
System.out.println("Did You Want To Book Another Taxi ? \n Yes/No");
String yesOrNo = sc.next().toLowerCase();
condition = yesOrNo.equals("yes") ? true : false;
System.out.println("-----------------------------------------------");
}
return totalTaxiAllocated;
}
public int taxiAllocation(List<BookingDetails>userInfo,int pickUpTime,int taxiOnline){
int reUseTaxiDown =0;
for(int i=0;i<userInfo.size();i++){
if(pickUpTime>=userInfo.get(i).getDropUpTime() && reUseTaxiDown==0){
reUseTaxiDown= userInfo.get(i).getTaxiAllocatedNumber();
}
if(reUseTaxiDown!=0 && reUseTaxiDown> userInfo.get(i).getTaxiAllocatedNumber()){
reUseTaxiDown= userInfo.get(i).getTaxiAllocatedNumber();
}
if(i==userInfo.size()-1 && reUseTaxiDown!=0)
return reUseTaxiDown;
}
return taxiOnline + 1;
}
public List<BookingDetails> reArrangeToCurrentCustomer(List<BookingDetails>historyList, int maxTaxiOnline){
List<BookingDetails> taxiOnline = new LinkedList<>();
for (int i=1;i<=maxTaxiOnline;i++){
for(int j = historyList.size()-1;j>=0;j--){
if(historyList.get(j).getTaxiAllocatedNumber()==i){
taxiOnline.add(historyList.get(j));
break;
}
}
}
return taxiOnline;
}
public int maximumTaxiAllocated(List<BookingDetails>historyList){
int maxTaxiOnline =0;
for(int i=0;i<historyList.size();i++ )
maxTaxiOnline = (
maxTaxiOnline<historyList.get(i).getTaxiAllocatedNumber())
?
historyList.get(i).getTaxiAllocatedNumber():maxTaxiOnline;
return maxTaxiOnline;
}
public int calculateDropUpTime(int pickUpTime, char pickUpPoint, char dropUpPoint){
return pickUpTime + ((int)(dropUpPoint - pickUpPoint));
}
public int calculateTaxiFare(char pickUpPoint,char dropUpPoint){
return 100 + ( ( (Integer.valueOf(dropUpPoint-pickUpPoint)* 15 ) -5 ) * 10 );
}
}
public class BookingDetails {
private int customerId;
private char pickUpPoint;
private char dropUpPoint;
private int pickUpTime;
private int dropUpTime;
private int taxiFare;
private int taxiAllocatedNumber;
public BookingDetails(int customerId, char pickUpPoint, char dropUpPoint, int pickUpTime,
int dropUpTime, int taxiFare, int taxiAllocatedNumber) {
this.customerId = customerId;
this.pickUpPoint = pickUpPoint;
this.dropUpPoint = dropUpPoint;
this.pickUpTime = pickUpTime;
this.dropUpTime = dropUpTime;
this.taxiFare = taxiFare;
this.taxiAllocatedNumber = taxiAllocatedNumber;
}
public int getTaxiFare() {
return taxiFare;
}
public int getCustomerId() {
return customerId;
}
public char getPickUpPoint() {
return pickUpPoint;
}
public char getDropUpPoint() {
return dropUpPoint;
}
public int getPickUpTime() {
return pickUpTime;
}
public int getDropUpTime() {
return dropUpTime;
}
public int getTaxiAllocatedNumber() {
return taxiAllocatedNumber;
}
}
Comments
Post a Comment