-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCar.java
More file actions
68 lines (59 loc) · 2.01 KB
/
Copy pathCar.java
File metadata and controls
68 lines (59 loc) · 2.01 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
/* You are designing a car rental system, and you need to create a Car class. The Car class should have private instance variables for the car's make,
model, and rental price per day. Implement appropriate getter and setter methods for these variables, ensuring that the make and model can only be set
once during object creation. Additionally, include a private variable to track the availability of the car (e.g., true if available for rent, false if
rented), and provide public methods to rent and return the car, updating its availability status. */
// Manjil Basnet
public class Car {
private String make;
private String model;
private double rentalprice;
private boolean isAvailable;
public Car(String make, String model, double rentalprice) {
this.make = make;
this.model = model;
this.rentalprice = rentalprice;
this.isAvailable = true;
}
// setter
public void setRentalprice(double rentalprice) {
if (rentalprice >= 0) {
this.rentalprice = rentalprice;
}
}
// getter
public String getMake() {
return make;
}
public String getModel() {
return model;
}
public double getRentalprice() {
return rentalprice;
}
public boolean isAvailable() {
return isAvailable;
}
public void rentCar() {
if (isAvailable) {
isAvailable = false;
}
}
public void returnCar() {
isAvailable = true;
}
public void carinfo() {
System.out.println("Make: " + getMake());
System.out.println("Model: " + getModel());
System.out.println("Rental Price per Day: " + getRentalprice());
}
}
class CarDriver{
public static void main(String[] args) {
Car car = new Car("BMW", "SUV", 10000);
car.carinfo();
car.rentCar();
System.out.println("Car Available: " + car.isAvailable());
car.returnCar();
System.out.println("Car Available after return: " + car.isAvailable());
}
}