In: Computer Science
Create an ApartmentException class whose constructor receives a string that holds a street address, an apartment number, a number of bedrooms, and a rent value for an apartment. Upon construction, throw an ApartmentException if any of the following occur: • The apartment number does not consist of 3 digits • The number of bedrooms is less than 1 or more than 4 • The rent is less than $500.00 or over $2500 Write a driver class that demonstrates creating valid Apartment Objects and Invalid Apartment Objects that throw the ApartmentException.
ApartmentException class:
public class ApartmentException extends Exception {
    public ApartmentException(String msg) {
        super(msg);
    }
}
Apartment class:
public class Apartment {
    private String streetAddress;
    private String apartmentNumber;
    private int numBedRooms;
    private double rentValue;
    public Apartment(String streetAddress, String apartmentNumber, int numBedRooms, double rentValue)
            throws ApartmentException {
        if ((apartmentNumber.length() != 3) || (numBedRooms < 1 || numBedRooms > 4)
                || (rentValue < 500 || rentValue > 2500)) {
            throw new ApartmentException(streetAddress + " " + apartmentNumber + " " + numBedRooms + " " + rentValue);
        }
        this.streetAddress = streetAddress;
        this.apartmentNumber = apartmentNumber;
        this.numBedRooms = numBedRooms;
        this.rentValue = rentValue;
    }
}
Main class:
public class Main {
    public static void main(String[] args) {
        // Create some valid and invalid Apartment objects
        try {
            Apartment a1 = new Apartment("Wall Street, NY", "112", 3, 2000);
        } catch (ApartmentException exp) {
            System.out.println(exp);
        }
        try {
            Apartment a2 = new Apartment("Date Center, NY", "12", 4, 1000);
        } catch (ApartmentException exp) {
            System.out.println(exp);
        }
        try {
            Apartment a3 = new Apartment("Downtown Road, NJ", "125", 5, 1500);
        } catch (ApartmentException exp) {
            System.out.println(exp);
        }
        try {
            Apartment a4 = new Apartment("Oak City Road, TX", "412", 2, 5000);
        } catch (ApartmentException exp) {
            System.out.println(exp);
        }
    }
}
Output:

Kindly rate the answer and for any help just drop a comment