[go: up one dir, main page]

0% found this document useful (0 votes)
22 views4 pages

Q009 - Vehicle - Lifespan - Program in Java

Uploaded by

Adarsh Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views4 pages

Q009 - Vehicle - Lifespan - Program in Java

Uploaded by

Adarsh Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignmnt No:09

import java.util.Scanner;

class Vehicle {
private String registrationNumber;
private String vehicleType;
private String color;
private String fuelType;
private int manufacturingYear;
private int mileage;

public Vehicle(String registrationNumber, String vehicleType,


String color, String fuelType, int manufacturingYear, int
mileage) {
this.registrationNumber = registrationNumber;
this.vehicleType = vehicleType;
this.color = color;
this.fuelType = fuelType;
this.manufacturingYear = manufacturingYear;
this.mileage = mileage;
}

public void display() {


int currentYear =
java.util.Calendar.getInstance().get(java.util.Calendar.YEAR);
int vehicleAge = currentYear - manufacturingYear;
int lifespan = fuelType.equalsIgnoreCase("Petrol") ? 15 :
10;
int validTill = manufacturingYear + lifespan;

System.out.println("\nVehicle Details");
System.out.println("Registration number: " +
registrationNumber);
System.out.println("Vehicle Type: " + vehicleType);
System.out.println("Vehicle color: " + color);
System.out.println("Fuel type: " + fuelType);
System.out.println("Manufacturing year: " +
manufacturingYear);
System.out.println("Mileage: " + mileage + "Km/H");
System.out.println("Age of vehicle: " + vehicleAge);

if (vehicleAge <= lifespan) {


System.out.println("Vehicle can be used till " +
validTill);
} else {
System.out.println("Vehicle is not within the life
span");
}
}
public static boolean validateRegistrationNumber(String
regNumber) {
return regNumber.matches("[A-Z]{2}\\d{2}[A-Z]{2}\\d{4}");
}
}

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("Enter Vehicle details");

String regNumber;
while (true) {
System.out.print("Vehicle number (Format:
XX00XX0000): ");
regNumber = sc.nextLine();
if (Vehicle.validateRegistrationNumber(regNumber)) {
break;
}
System.out.println("Invalid Vehicle Registration
Number! Please try again.");
}

String vehicleType = "";


while (true) {
System.out.print("Vehicle Type (1 for Hatchback / 2
for SUV / 3 for XUV): ");
int vehicleTypeOption = sc.nextInt();
if (vehicleTypeOption == 1) {
vehicleType = "Hatchback";
break;
} else if (vehicleTypeOption == 2) {
vehicleType = "SUV";
break;
} else if (vehicleTypeOption == 3) {
vehicleType = "XUV";
break;
} else {
System.out.println("Invalid Vehicle Type! Please
enter 1, 2, or 3.");
}
}

sc.nextLine();

System.out.print("Vehicle color: ");


String color = sc.nextLine();

String fuelType;
while (true) {
System.out.print("Fuel Type (Petrol/Diesel): ");
fuelType = sc.nextLine();
if (fuelType.equalsIgnoreCase("Petrol") ||
fuelType.equalsIgnoreCase("Diesel")) {
break;
}
System.out.println("Invalid Fuel Type! Please enter
Petrol or Diesel.");
}

int manufacturingYear;
while (true) {
System.out.print("Manufacturing year: ");
if (sc.hasNextInt()) {
manufacturingYear = sc.nextInt();
break;
} else {
System.out.println("Invalid year! Please enter a
numeric value.");
sc.next();
}
}

int mileage;
while (true) {
System.out.print("Mileage: ");
if (sc.hasNextInt()) {
mileage = sc.nextInt();
break;
} else {
System.out.println("Invalid mileage! Please enter
a numeric value.");
sc.next();
}
}

Vehicle vehicle = new Vehicle(regNumber, vehicleType,


color, fuelType, manufacturingYear, mileage);
vehicle.display();
}
}
Output:
Enter Vehicle details

Vehicle number (Format: XX00XX0000): KA45AX1234

Vehicle Type (1 for Hatchback / 2 for SUV / 3 for XUV): 1

Vehicle color: Blue

Fuel Type (Petrol/Diesel): Petrol

Manufacturing year: 2020

Mileage: 19

Vehicle Details

Registration number: KA45AX1234

Vehicle Type: Hatchback

Vehicle color: Blue

Fuel type: Petrol

Manufacturing year: 2020

Mileage: 19Km/H

Age of vehicle: 4

Vehicle can be used till 2035

You might also like