You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

89 lines
2.3 KiB

package JavaLearningProject.w3;
public class Car {
private String licensePlate;
private String brand;
private String model;
private double dailyRent;
private boolean isRented;
private static int totalCars = 0;
public Car(String licensePlate, String brand, String model, double dailyRent) {
if (licensePlate == null || licensePlate.trim().isEmpty()) {
throw new IllegalArgumentException("licensePlate must not be empty");
}
if (brand == null || brand.trim().isEmpty()) {
throw new IllegalArgumentException("brand must not be empty");
}
if (model == null || model.trim().isEmpty()) {
throw new IllegalArgumentException("model must not be empty");
}
if (dailyRent <= 0) {
throw new IllegalArgumentException("dailyRent must be positive");
}
this.licensePlate = licensePlate;
this.brand = brand;
this.model = model;
this.dailyRent = 300.0;
this.isRented = false;
totalCars++;
}
public String getLicensePlate() {
return licensePlate;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
if (brand == null || brand.trim().isEmpty()) {
throw new IllegalArgumentException("brand must not be empty");
}
this.brand = brand;
}
public String getModel() {
return model;
}
public void setModel(String model) {
if (model == null || model.trim().isEmpty()) {
throw new IllegalArgumentException("model must not be empty");
}
this.model = model;
}
public void setDailyRent(double dailyRent) {
if (dailyRent <= 0) {
throw new IllegalArgumentException("dailyRent must be positive");
}
this.dailyRent = dailyRent;
}
public boolean isRented() {
return isRented;
}
public boolean rent() {
if (isRented) {
return false;
}
isRented = true;
return true;
}
public double calculateRent(int days) {
if (days <= 0) {
throw new IllegalArgumentException("days must be positive");
}
return dailyRent * days;
}
public static int getTotalCars() {
return totalCars;
}
}