Create a UsedCarException class that extends Exception; its constructor receives
ID: 3802329 • Letter: C
Question
Create a UsedCarException class that extends Exception; its constructor receives a value for a vehicle identification number (VIN) that is passed to the parent constructor so it can be used in a getMessage() call. Save the class as UsedCarException.java. Create a UsedCar class with fields for VIN, make, year, mileage, and price. The UsedCar constructor throws a UsedCarException when the VIN is not four digits; when the make is not Ford, Honda, Toyota, Chrysler, or Other; when the year is not between 1990 and 2014 inclusive; or either the mileage or price is negative. Save the class as UsedCar.java. Write an application that establishes an array of at least seven UsedCar objects and handles any Exceptions. Display a list of only the UsedCar objects that were constructed successfully. Save the file as ThrowUsedCarExceptions.java.
Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg.march;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Sam
*/
public class ThrowUsedCarExceptions {
public static void main(String[] args) {
ArrayList<UsedCar> usedCars = new ArrayList<>();
try {
usedCars.add(new UsedCar("1234", "Ford", 2004, 25, 360));
} catch (UsedCarException ex) {
System.err.println(ex.getMessage());
}
try {
usedCars.add(new UsedCar("560", "Ford", 2004, 25, 360));
} catch (UsedCarException ex) {
System.err.println(ex.getMessage());
}
try {
usedCars.add(new UsedCar("1111", "Apple", 2004, 25, 360));
} catch (UsedCarException ex) {
System.err.println(ex.getMessage());
}
try {
usedCars.add(new UsedCar("1258", "Ford", 1945, 25, 360));
} catch (UsedCarException ex) {
System.err.println(ex.getMessage());
}
try {
usedCars.add(new UsedCar("1477", "Ford", 2004, -4, 360));
} catch (UsedCarException ex) {
System.err.println(ex.getMessage());
}
try {
usedCars.add(new UsedCar("1456", "Ford", 2004, 25, -1));
} catch (UsedCarException ex) {
System.err.println(ex.getMessage());
}
try {
usedCars.add(new UsedCar("7845", "Ford", 199, -25, 360));
} catch (UsedCarException ex) {
System.err.println(ex.getMessage());
}
try {
usedCars.add(new UsedCar("7890", "Other", 2010, 10, 45821));
} catch (UsedCarException ex) {
System.err.println(ex.getMessage());
}
try {
usedCars.add(new UsedCar("1234", "Ford", 2004, 25, 360));
} catch (UsedCarException ex) {
System.err.println(ex.getMessage());
}
usedCars.stream().forEach((uc) -> {
System.out.println(uc.toString());
});
}
}
class UsedCar {
private final String vin;
private final String make;
private final int year;
private final double milage;
private final int price;
public UsedCar(String vin, String make, int year, double milage, int price) throws UsedCarException {
this.vin = vin;
this.make = make;
this.year = year;
this.milage = milage;
this.price = price;
if (vin.length() != 4)
throw new UsedCarException(vin);
if (!(make.equals("Ford") || make.equals("Honda") || make.equals("Toyota") || make.equals("Chrysler") || make.equals("Other")))
throw new UsedCarException(vin);
if (year<1990 || year>2014)
throw new UsedCarException(vin);
if (milage<0 || price<0)
throw new UsedCarException(vin);
}
@Override
public String toString() {
return "UsedCar{" + "vin=" + vin + ", make=" + make + ", year=" + year + ", milage=" + milage + ", price=" + price + '}';
}
}
class UsedCarException extends Exception {
public UsedCarException(String vin) {
super(vin);
}
}