-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntermediate Module 1.java
More file actions
86 lines (69 loc) · 2.33 KB
/
Intermediate Module 1.java
File metadata and controls
86 lines (69 loc) · 2.33 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
Ques 1:
import java.util.Scanner;
class Car {
String type;
// Constructor
public Car(String type) {
this.type = type;
}
// Method to calculate rental charges for standard car types
public double calculateRentalCharges(int days) {
double dailyRate;
switch (type.toLowerCase()) {
case "standard":
dailyRate = 2000;
break;
case "suv":
dailyRate = 4000;
break;
default:
dailyRate = 2000;
break;
}
return dailyRate * days;
}
// Overloaded method to calculate rental charges with additional options
public double calculateRentalCharges(int days, String... options) {
double baseCharge = calculateRentalCharges(days);
double optionsCharge = 0;
for (String option : options) {
switch (option.toLowerCase()) {
case "gps":
optionsCharge += 500 * days;
break;
case "child seat":
optionsCharge += 200 * days;
break;
default:
break;
}
}
return baseCharge + optionsCharge;
}
// Method to display car details and charges
public void displayRentalDetails(int days, String... options) {
double totalCharges = calculateRentalCharges(days, options);
System.out.println(String.format("Total Rental Charges: ₹%.2f", totalCharges));
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input car details
String type = scanner.nextLine().trim();
// Input number of days
int days = scanner.nextInt();
scanner.nextLine();
// Check for additional options input (optional)
String optionsInput = "";
if (scanner.hasNextLine()) {
optionsInput = scanner.nextLine().trim();
}
// Parse options if provided
String[] options = optionsInput.isEmpty() ? new String[0] : optionsInput.split(",\\s*");
// Create a Car object and calculate/display rental details
Car car = new Car(type);
car.displayRentalDetails(days, options);
scanner.close();
}
}