Skip to content

Instantly share code, notes, and snippets.

@afinlay5
Created August 13, 2020 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afinlay5/a13658ca7598ef7c55338b7881a1d078 to your computer and use it in GitHub Desktop.
Save afinlay5/a13658ca7598ef7c55338b7881a1d078 to your computer and use it in GitHub Desktop.
public class ExceptionBlurb {
public static void main (String [] args) {
final Car adriansLemon = new Car("alt", 9.0, "turn key", 1, true, 2);
final Car dansCRV = new Car("good alternator", 9, "PTS", 912, false, 5);
try {
dansCRV.start();
} catch (AlternatorFailureException afe) {
System.out.println(afe);
} catch (NoEngineOilException neo) {
System.out.println(neo);
} catch (IgnitionFailureException ife) {
System.out.println(ife);
} catch (CarCheckedException ce) {
System.out.println(ce);
}
try {
adriansLemon.start();
} catch (CarCheckedException carException) {
System.out.println(carException);
}
adriansLemon.driveHome();
}
}
class Car {
private final String alternatorModel;
private final double alternatorVoltage;
private final String ignitionType;
private final int oilType;
private final boolean isAlternatorAlive;
private final double engineOilLevel;
Car (String alternatorModel, double alternatorVoltage, String ignitionType, int oilType, boolean isAlternatorAlive, double engineOilLevel) {
this.alternatorModel = alternatorModel;
this.alternatorVoltage = alternatorVoltage;
this.oilType = oilType;
this.isAlternatorAlive = isAlternatorAlive;
this.engineOilLevel = engineOilLevel;
this.ignitionType = ignitionType;
}
public void start() throws CarCheckedException {
if (!isAlternatorAlive)
throw new AlternatorFailureException("Alternator Died", alternatorModel, alternatorVoltage, isAlternatorAlive);
else if (alternatorVoltage < 2)
throw new AlternatorFailureException("Alternator volt too low", alternatorModel, alternatorVoltage, isAlternatorAlive);
else if (engineOilLevel < 5)
throw new NoEngineOilException ("Engine Oil critically low", ignitionType);
else if (ignitionType == "bad ignition type")
throw new IgnitionFailureException("Ignition failed to turn over", ignitionType);
}
public void driveHome() {
throw new PoppedTireException(TireEnum.REAR_LEFT);
}
}
interface CarException {
boolean isChecked();
}
class CarCheckedException extends Exception implements CarException {
public boolean isChecked() {
return true;
}
CarCheckedException(String message) {
super(message);
}
}
class CarUncheckedException extends RuntimeException implements CarException {
public boolean isChecked() {
return false;
}
CarUncheckedException(String message) {
super(message);
}
}
class AlternatorFailureException extends CarCheckedException {
final private String alternatorModel;
final private double alternatorVoltage;
final private boolean isAlive;
public String getAlternatorModel() {
return alternatorModel;
}
public double getAlternatorVoltage() {
return alternatorVoltage;
}
public boolean isAlive() {
return isAlive;
}
AlternatorFailureException (String message, String alternatorModel, double alternatorVoltage, boolean isAlive) {
super(message);
this.alternatorModel = alternatorModel;
this.alternatorVoltage = alternatorVoltage;
this.isAlive = isAlive;
}
}
class NoEngineOilException extends CarCheckedException {
private final String oilType;
public String oilType() {
return oilType;
}
NoEngineOilException (String message, String oilType) {
super(message);
this.oilType = oilType;
}
}
class IgnitionFailureException extends CarCheckedException {
private final String ignitionType;
public String ignitionType() {
return ignitionType;
}
IgnitionFailureException (String message, String ignitionType) {
super(message);
this.ignitionType = ignitionType;
}
}
class PoppedTireException extends CarUncheckedException {
final TireEnum tirePopped;
PoppedTireException(TireEnum tirePopped) {
super("The " + tirePopped + " tire popped.");
this.tirePopped = tirePopped;
}
}
enum TireEnum {
FRONT_LEFT, FRONT_RIGHT, REAR_LEFT, REAR_RIGHT;
@Override public String toString() {
return super.toString().toLowerCase().replace('_', '-');
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment