Skip to content

Commit a7cc576

Browse files
committed
OOPS - designing of cab service
1 parent 5265c23 commit a7cc576

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/OOPS/CabService.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package OOPS;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
7+
public class CabService {
8+
String car_type;
9+
double km;
10+
double bill;
11+
12+
// constructor
13+
public CabService() {
14+
this.car_type = "";
15+
this.km = 0;
16+
this.bill = 0;
17+
}
18+
//accept method
19+
public void accept(String car_type, double km) {
20+
this.car_type = car_type;
21+
this.km = km;
22+
}
23+
//calculate method
24+
public void calculate() {
25+
if(this.car_type.equalsIgnoreCase("A")){
26+
if(this.km <= 5) this.bill = 150;
27+
else {
28+
this.bill = 10 * (this.km - 5) + 150;
29+
}
30+
}else{ // Non - Ac
31+
if (this.km <= 5) this.bill = 120;
32+
else {
33+
this.bill = 8 * (this.km - 5) + 120;
34+
}
35+
}
36+
}
37+
// display method
38+
public void display() {
39+
System.out.println(STR."CAR TYPE: \{car_type.toUpperCase()}, \nKILOMETER TRAVELLED: \{km}, \nTOTAL BILL: ₹\{bill}");
40+
}
41+
42+
public static void main(String[] args) throws IOException {
43+
InputStreamReader isr = new InputStreamReader(System.in);
44+
BufferedReader br = new BufferedReader(isr);
45+
46+
CabService s1 = new CabService();
47+
48+
System.out.print("Select the car type: a for AC or n for Non Ac: ");
49+
String car_type = br.readLine();
50+
System.out.print("Enter the Kilometer travelled: ");
51+
int km = Integer.parseInt(br.readLine());
52+
if(car_type.equalsIgnoreCase("A")) s1.accept("AC Car",km);
53+
else s1.accept("Non AC Car",km);
54+
s1.calculate();
55+
s1.display();
56+
}
57+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy