mirror of
https://github.com/danbulant/introductionToProgramming
synced 2026-05-24 12:35:50 +00:00
format
This commit is contained in:
parent
28ec01ea9f
commit
21fe12aa34
3 changed files with 56 additions and 46 deletions
|
|
@ -3,15 +3,20 @@ package week13;
|
||||||
public class Gearbox {
|
public class Gearbox {
|
||||||
private int gear = 1;
|
private int gear = 1;
|
||||||
|
|
||||||
public static class IllegalGearChangeException extends RuntimeException {}
|
public static class IllegalGearChangeException extends RuntimeException {
|
||||||
|
}
|
||||||
|
|
||||||
void changeGear(int newGear) {
|
void changeGear(int newGear) {
|
||||||
if(newGear == gear) return;
|
if (newGear == gear)
|
||||||
if(newGear != -1 && (newGear < 1 || newGear > 5)) throw new IllegalArgumentException();
|
return;
|
||||||
|
if (newGear != -1 && (newGear < 1 || newGear > 5))
|
||||||
|
throw new IllegalArgumentException();
|
||||||
// newGear is now either -1 or 1..=5
|
// newGear is now either -1 or 1..=5
|
||||||
var gearToCheck = newGear;
|
var gearToCheck = newGear;
|
||||||
if(gearToCheck == -1) gearToCheck++;
|
if (gearToCheck == -1)
|
||||||
if(Math.abs(gear - newGear) != 1) throw new IllegalGearChangeException();
|
gearToCheck++;
|
||||||
|
if (Math.abs(gear - gearToCheck) != 1)
|
||||||
|
throw new IllegalGearChangeException();
|
||||||
gear = newGear;
|
gear = newGear;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,19 +7,24 @@ public class Printer {
|
||||||
@Override
|
@Override
|
||||||
public String getMessage() {
|
public String getMessage() {
|
||||||
return "No Paper pack found in printer.\n" +
|
return "No Paper pack found in printer.\n" +
|
||||||
"Check that there's enough paper left for this print job, that the Paper pack is clean and that the paper pack chip is undamaged. "+
|
"Check that there's enough paper left for this print job, that the Paper pack is clean and that the paper pack chip is undamaged. "
|
||||||
|
+
|
||||||
"Please note that third-party Paper packs may not be supported. Check that the Paper pack is made for this model.";
|
"Please note that third-party Paper packs may not be supported. Check that the Paper pack is made for this model.";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static class OutOfTonerException extends Exception {
|
static class OutOfTonerException extends Exception {
|
||||||
@Override
|
@Override
|
||||||
public String getMessage() {
|
public String getMessage() {
|
||||||
return "Printing supply pack is empty or was not found.\n" +
|
return "Printing supply pack is empty or was not found.\n" +
|
||||||
"Check that an official Printing supply pack is correctly inserted in the printer, try re-seating if necessary. " +
|
"Check that an official Printing supply pack is correctly inserted in the printer, try re-seating if necessary. "
|
||||||
"Check that Printing supply packs are inserted into the correct color coded slots. Check that your Printing resupply subscription is active. " +
|
+
|
||||||
|
"Check that Printing supply packs are inserted into the correct color coded slots. Check that your Printing resupply subscription is active. "
|
||||||
|
+
|
||||||
"Please note that third-party Printing supply packs may not be supported. Check that the Paper pack is made for this model.";
|
"Please note that third-party Printing supply packs may not be supported. Check that the Paper pack is made for this model.";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static class PaperJamException extends Exception {
|
static class PaperJamException extends Exception {
|
||||||
@Override
|
@Override
|
||||||
public String getMessage() {
|
public String getMessage() {
|
||||||
|
|
@ -28,7 +33,7 @@ public class Printer {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Exception getExceptionFromErrorCode(int errorCode) {
|
private static Exception getExceptionFromErrorCode(int errorCode) {
|
||||||
return switch(errorCode / 10) {
|
return switch (errorCode / 10) {
|
||||||
case 0 -> new OutOfPaperException();
|
case 0 -> new OutOfPaperException();
|
||||||
case 1 -> new OutOfTonerException();
|
case 1 -> new OutOfTonerException();
|
||||||
case 2 -> new PaperJamException();
|
case 2 -> new PaperJamException();
|
||||||
|
|
@ -46,7 +51,7 @@ public class Printer {
|
||||||
try {
|
try {
|
||||||
print();
|
print();
|
||||||
System.out.println("Printed successfully, thanks for your patronage.");
|
System.out.println("Printed successfully, thanks for your patronage.");
|
||||||
} catch(Exception e) {
|
} catch (Exception e) {
|
||||||
System.err.println(e.getMessage());
|
System.err.println(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@
|
||||||
Explain - in your own words - what is an exception?
|
Explain - in your own words - what is an exception?
|
||||||
|
|
||||||
An object containing information about why a semi-hidden branching happened, usually used for error states.
|
An object containing information about why a semi-hidden branching happened, usually used for error states.
|
||||||
Thrown exceptions bubble up, as if a return was used, until there's a registered exception handler for a given code region, usually by try/catch.
|
Thrown exceptions bubble up the call stack, as if a return was used, until there's a registered exception handler for a given code region, usually by try/catch.
|
||||||
|
|
||||||
== Exercise 13.05
|
== Exercise 13.05
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue