0% found this document useful (0 votes)
7 views1 page

Wow Java

Java assignment
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)
7 views1 page

Wow Java

Java assignment
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/ 1

class InvalidUserException extends Exception {

public InvalidUserException(String message) {


super(message);
}
}

public class User {


private String city;
private String vehicle;

public User(String city, String vehicle) {


this.city = city;
this.vehicle = vehicle;
}

public void validateUser() throws InvalidUserException {


if (!(city.equals("Pune") || city.equals("Mumbai") || city.equals("Bangalore") ||
city.equals("Chennai"))) {
throw new InvalidUserException("Invalid city. User must stay in Pune, Mumbai,
Bangalore, or Chennai.");
}

if (!vehicle.equals("4-wheeler")) {
throw new InvalidUserException("Invalid vehicle. User must have a 4-wheeler.");
}
}

public static void main(String[] args) {


try {
User user = new User("Mumbai", "4-wheeler");
user.validateUser();
System.out.println("User is valid.");
} catch (InvalidUserException e) {
System.err.println("Invalid User: " + e.getMessage());
}
}
}

You might also like