Skip to content

Commit 1353014

Browse files
committed
unitTestingExercise - Complete
1 parent 48d90e7 commit 1353014

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

  • unit-testing/exercises/car-exercises/src

unit-testing/exercises/car-exercises/src/main/java/org/launchcode/Car.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,18 @@ public void drive(double miles)
8484
this.odometer += milesAbleToTravel;
8585
}
8686

87+
88+
public void addGas(double gas) {
89+
if ((gas + this.getGasTankLevel()) > this.getGasTankSize()) {
90+
throw new IllegalArgumentException("Can't exceed tank size");
91+
}
92+
this.setGasTankLevel(gas + this.getGasTankLevel());
93+
}
94+
// public void setGasTankLevel(double gasTankLevel) {
95+
// if (gasTankLevel > this.getGasTankSize()) {
96+
// throw new IllegalArgumentException("Can't exceed tank size");
97+
// }
98+
// this.gasTankLevel = gasTankLevel;
99+
//}
100+
87101
}

unit-testing/exercises/car-exercises/src/test/java/org/launchcode/CarTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,22 @@ public void emptyTest() {
2424
public void testInitialGasTank() {
2525
assertEquals( 10, test_car.getGasTankLevel(),.001);
2626
}
27-
2827
//TODO: gasTankLevel is accurate after driving within tank range
28+
@Test
29+
public void testGasTankAfterDriving() {
30+
test_car.drive(50);
31+
assertEquals(9, test_car.getGasTankLevel(), .001);
32+
}
2933
//TODO: gasTankLevel is accurate after attempting to drive past tank range
34+
@Test
35+
public void testGasTankAfterExceedingTankRange() {
36+
test_car.drive(501);
37+
assertEquals(0, test_car.getGasTankLevel(), .001);
38+
}
3039
//TODO: can't have more gas than tank size, expect an exception
40+
@Test
41+
public void testGasOverfillException() {
42+
assertThrows(IllegalArgumentException.class, () -> test_car.addGas(5), "Shouldn't get here, car cannot have more gas in tank than the size of the tank"
43+
);
44+
}
3145
}

0 commit comments

Comments
 (0)