Repository navigation
Expand file tree
/
Copy pathvehiclerental.java
More file actions
91 lines (72 loc) · 2.25 KB
/
Copy pathvehiclerental.java
File metadata and controls
91 lines (72 loc) · 2.25 KB
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
import java.util.Scanner;
// Step 1: Vehicle Interface
interface Vehicle {
void rent();
}
// Step 2: Car Class
class Car implements Vehicle {
public void rent() {
System.out.println(" Car rented. Comfortable for family trips.");
}
}
// Step 3: Bike Class
class Bike implements Vehicle {
public void rent() {
System.out.println("️ Bike rented. Best for quick rides.");
}
}
// Step 4: Truck Class
class Truck implements Vehicle {
public void rent() {
System.out.println(" Truck rented. Suitable for goods transport.");
}
}
// Step 5: Factory Class
class VehicleFactory {
public static Vehicle getVehicle(String type) {
if (type == null) {
return null;
}
if (type.equalsIgnoreCase("car")) {
return new Car();
}
else if (type.equalsIgnoreCase("bike")) {
return new Bike();
}
else if (type.equalsIgnoreCase("truck")) {
return new Truck();
}
else {
return null;
}
}
}
// Step 6: Main Class
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("=================================");
System.out.println(" 🚗 VEHICLE RENTAL SYSTEM");
System.out.println("=================================");
while (true) {
System.out.println("\nAvailable Vehicles:");
System.out.println("1. Car");
System.out.println("2. Bike");
System.out.println("3. Truck");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
String input = sc.nextLine();
if (input.equals("4")) {
System.out.println("Thank you for using Vehicle Rental System!");
break;
}
Vehicle vehicle = VehicleFactory.getVehicle(input);
if (vehicle != null) {
vehicle.rent();
} else {
System.out.println("Invalid choice! Please try again.");
}
}
sc.close();
}
}