-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcabInvoiceGen.js
171 lines (158 loc) · 4.88 KB
/
cabInvoiceGen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* @class CabInvoiceGen
* @classdesc Class to calculate cab fare
*/
class CabInvoiceGen {
constructor() {
this.MINFARE = 5; //minimum fare in INR for normal cab
this.FAREPERKIM = 10; //fare per kilometer in INR for normal cab
this.FAREPERMIN = 1; //fare per minute in INR for normal cab
this.MINFARE_PREMIUM = 20; //minimum fare in INR for premium cab
this.FAREPERKIM_PREMIUM = 15; //fare per kilometer in INR for premium cab
this.FAREPERMIN_PREMIUM = 2; //fare per minute in INR for premium cab
//dummy database
this.db = [
{
userId: 1,
rides: [{
type: "normal",
distance: 4,
time: 4
}, {
type: "premium",
distance: 2,
time: 2
}]
}
];
}
/**
* @method to calculate fare for the journey by using distance and time
* @param {number} distance in kilometers
* @param {number} time in hours
*/
calcCabFare(distance, time) {
let totalFare = 0; //total fare for a ride in INR
let totalTimeInMin = 0; // total time in minutes
if (distance == null || time == null) {
//check if function has no arguments
return true;
}else if(distance < 0 || time < 0){
//check if function has -ve args
return true;
} else if (typeof distance === undefined || typeof time === undefined) {
//check if function has partial arguments
return true;
} else if (typeof distance !== "number" || typeof distance !== "number") {
//check if function has valid number type arguments
return true;
} else {
totalTimeInMin = time * 60;
if (distance === 0 || time === 0) {
return this.MINFARE;
}
totalFare = this.FAREPERKIM * distance + totalTimeInMin * this.FAREPERMIN;
return totalFare;
}
}
/**
* @method to calculate fare for premium cab journey
* @param {number} distance
* @param {number} time
*/
calcPremiumCabFare(distance,time){
let totalFare = 0; //total fare for a ride in INR
let totalTimeInMin = 0; // total time in minutes
if (distance == null || time == null) {
//check if function has no arguments
return true;
} else if (typeof distance === undefined || typeof time === undefined) {
//check if function has partial arguments
return true;
}else if(distance < 0 || time < 0){
return true;
}else if (typeof distance !== "number" || typeof distance !== "number") {
//check if function has valid number type arguments
return true;
} else {
totalTimeInMin = time * 60;
if (distance === 0 || time === 0) {
return this.MINFARE_PREMIUM;
}
totalFare = this.FAREPERKIM_PREMIUM * distance + totalTimeInMin * this.FAREPERMIN_PREMIUM;
return totalFare;
}
}
/**
* @method to calculate multiple ride fares
* @param {number} rides is an array of objects example - ride = [{distance:3,time:3}];
*/
multipleRideFare(rides) {
if (rides == null) {
//check if function has no arguments
return true;
} else if (typeof rides !== "object") {
//check if function has valid number type arguments
return true;
} else {
let agrTotalFare = 0;
if (rides.length === 0) {
return agrTotalFare;
}
for (let i = 0; i < rides.length; i++) {
if(rides[i].type == "normal"){
agrTotalFare =
agrTotalFare + this.calcCabFare(rides[i].distance, rides[i].time);
}else{
agrTotalFare =
agrTotalFare + this.calcPremiumCabFare(rides[i].distance, rides[i].time);
}
}
return agrTotalFare;
}
}
/**
* @method to generate invoice having details total number of rides, total fare, and avg fare per ride
* @param {object} rides is an array of objects example - ride = [{distance:3,time:3}];
*/
generateInvoice(rides) {
if (rides == null) {
//check if function has no arguments
return true;
} else if (typeof rides !== "object") {
//check if function has valid number type arguments
return true;
} else {
let invoice = {
totalFare: 0,
totalRides: 0,
avgFarePerRide: 0
};
invoice.totalFare = this.multipleRideFare(rides);
invoice.totalRides = rides.length;
invoice.avgFarePerRide = invoice.totalFare / invoice.totalRides;
return invoice;
}
}
/**
* @method to generate invoice for specific user
* @param {number} userId
*/
getUserInvoice(userId) {
if (userId == null) {
return true;
} else if (typeof (userId) !== "number") {
return true;
} else {
for (let i = 0; i < this.db.length; i++) {
if (userId == this.db[i].userId) {
let invoice = this.generateInvoice(this.db[i].rides);
return invoice;
} else {
return false;
}
}
}
}
}
module.exports = CabInvoiceGen;