Skip to content

Commit 3342e32

Browse files
Merge pull request #2 from LaunchCodeEducation/starter_testing
Starter testing
2 parents 63c1d20 + bbf6535 commit 3342e32

File tree

5 files changed

+105
-0
lines changed

5 files changed

+105
-0
lines changed

lib/hamcrest-core-1.3.jar

44 KB
Binary file not shown.

lib/junit-4.13-beta-3.jar

372 KB
Binary file not shown.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package org.launchcode.java.demos.testing.main;
2+
3+
public class Car {
4+
5+
private String make;
6+
private String model;
7+
private int gasTankSize;
8+
private double gasTankLevel;
9+
private double milesPerGallon;
10+
private double odometer = 0;
11+
12+
public Car(String make, String model, int gasTankSize, double milesPerGallon) {
13+
this.make = make;
14+
this.model = model;
15+
this.gasTankSize = gasTankSize;
16+
// Gas tank level defaults to a full tank
17+
this.gasTankLevel = gasTankSize;
18+
this.milesPerGallon = milesPerGallon;
19+
}
20+
21+
public String getMake() {
22+
return make;
23+
}
24+
25+
public void setMake(String make) {
26+
this.make = make;
27+
}
28+
29+
public String getModel() {
30+
return model;
31+
}
32+
33+
public void setModel(String model) {
34+
this.model = model;
35+
}
36+
37+
public int getGasTankSize() {
38+
return gasTankSize;
39+
}
40+
41+
public void setGasTankSize(int gasTankSize) {
42+
this.gasTankSize = gasTankSize;
43+
}
44+
45+
public double getGasTankLevel() {
46+
return gasTankLevel;
47+
}
48+
49+
public void setGasTankLevel(double gasTankLevel) {
50+
if (gasTankLevel > this.getGasTankSize()) {
51+
throw new IllegalArgumentException("Can't exceed tank size");
52+
}
53+
this.gasTankLevel = gasTankLevel;
54+
}
55+
56+
public double getMilesPerGallon() {
57+
return milesPerGallon;
58+
}
59+
60+
public void setMilesPerGallon(double milesPerGallon) {
61+
this.milesPerGallon = milesPerGallon;
62+
}
63+
64+
public double getOdometer() {
65+
return odometer;
66+
}
67+
68+
/**
69+
* Drive the car an amount of miles. If not enough fuel, drive as far as fuel allows.
70+
* Adjust fuel levels based on amount needed to drive the distance requested.
71+
* Add miles to odometer.
72+
*
73+
* @param miles - the miles to drive
74+
*/
75+
public void drive(double miles)
76+
{
77+
//adjust fuel based on mpg and miles requested to drive
78+
double maxDistance = this.milesPerGallon * this.gasTankLevel;
79+
double milesAbleToTravel = miles > maxDistance ? maxDistance : miles;
80+
double gallonsUsed = milesAbleToTravel / this.milesPerGallon;
81+
this.gasTankLevel = this.gasTankLevel - gallonsUsed;
82+
this.odometer += milesAbleToTravel;
83+
}
84+
85+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.launchcode.java.demos.testing.main;
2+
3+
public class Main {
4+
5+
public static void main(String[] args) {
6+
Car car = new Car("Toyota", "Prius", 10, 50);
7+
System.out.println(car.getMake() + " - " + car.getModel());
8+
}
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.launchcode.java.demos.testing.test;
2+
3+
public class CarTest {
4+
5+
//TODO: add emptyTest so we can configure our runtime environment (remove this test before pushing to your personal GitLab account)
6+
//TODO: constructor sets gasTankLevel properly
7+
//TODO: gasTankLevel is accurate after driving within tank range
8+
//TODO: gasTankLevel is accurate after attempting to drive past tank range
9+
//TODO: can't have more gas than tank size, expect an exception
10+
11+
}

0 commit comments

Comments
 (0)