Skip to content

Commit 09ab407

Browse files
TatsuyaRyujinEvan Philakhong
andauthored
feat: implements Formula1Car.java Class & Tests (#500)
* feat: adds Formula1Car methods * refactor: immplements lombok annotations * feat: adds Track.java class * feat: adds Formula1Car methods * feat: adds Weather.java * feat: adds rainStrategy() method in Formula1Car Class * feat: adds NoSponsorsException.java * feat: implements NoSponsorsException * feat: adds Track.java constuctors * refactor: ./gradle spotlessApply * feat: adds Formula1Car.java tests * feat: adds printSponsors() tests * refactor: adds JavaDoc style * fix: run ./gradleW spotlessApply --------- Co-authored-by: Evan Philakhong <[email protected]>
1 parent dbd5296 commit 09ab407

File tree

7 files changed

+294
-0
lines changed

7 files changed

+294
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.codedifferently.lesson16.evanphilakhong;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
@Getter
7+
@Setter
8+
public class Formula1Car {
9+
private Track track;
10+
private Team team;
11+
private String[] sponsors;
12+
private String driverName;
13+
private int driverNum;
14+
private int position;
15+
private TyreCompound currTyre;
16+
private boolean drs;
17+
18+
/**
19+
* Default Constructor for a Formula1Car with no team, empty sponsors array, empty driver name,
20+
* driver number 0, no tyre compound, and DRS is disabled by default
21+
*/
22+
public Formula1Car() {
23+
this.team = null;
24+
this.sponsors = new String[0];
25+
this.driverName = "";
26+
this.driverNum = 0;
27+
this.currTyre = null;
28+
this.drs = false;
29+
}
30+
31+
/**
32+
* @param team the tean that this Formula1Car belongs
33+
* @param sponsors array of sponsor names accosicated with this Formula1Car
34+
* @param driverName the name of the driver of this Formula1Car
35+
* @param driverNum the drivers number for the Formula1Car
36+
* @param position drivers current racing position on the grid
37+
* @param currTyre current TyreCompound fitted on this Formula1Car
38+
* @see Team
39+
* @see TyreCompound
40+
*/
41+
public Formula1Car(
42+
Team team,
43+
String[] sponsors,
44+
String driverName,
45+
int driverNum,
46+
int position,
47+
TyreCompound currTyre) {
48+
this.team = team;
49+
this.sponsors = sponsors;
50+
this.driverName = driverName;
51+
this.driverNum = driverNum;
52+
this.position = position;
53+
this.currTyre = currTyre;
54+
this.drs = false;
55+
}
56+
57+
/**
58+
* Displays a header line then, Prints a list of sponsors to the console.
59+
*
60+
* @throws NoSponsorsException if there are no sponsors associated with this Formula1Car
61+
* @see NoSponsorsException
62+
*/
63+
public void printSponsors() throws NoSponsorsException {
64+
if (sponsors.length == 0) {
65+
throw new NoSponsorsException(team + " has no Sponsors");
66+
} else {
67+
System.out.println("Here's a list of our Sponsors:");
68+
for (String sponsor : sponsors) {
69+
System.out.println(sponsor);
70+
}
71+
}
72+
}
73+
74+
/**
75+
* Checks if DRS (Drag Reduction System) is availible to use This method determines DRS
76+
* availibility based on weather or not your Formula1Car is in the current track's designated DRS
77+
* zone.
78+
*
79+
* @return {@code true} if DRS is availible (track has a DRS zone), {@code false} otherwise
80+
* @see Track#isDrsZone()
81+
*/
82+
public boolean isDrsAvailible() {
83+
if (track.isDrsZone()) {
84+
this.drs = true;
85+
return true;
86+
}
87+
return false;
88+
}
89+
90+
/**
91+
* Checks if we need to switch race strategy to account for the rainy weather if the weather is
92+
* LIGHT_RAIN we change the TyreCompound to INTERMEDIATE if the weather is HEAVY_RAIN we change
93+
* the TyreCompound to WET
94+
*
95+
* @return {@code true} if track.getWeather is LIGHT_RAIN || HEAVY_RAIN {@code false} otherwise
96+
* @see Track#getWeather()
97+
*/
98+
public boolean rainStrategy() {
99+
if (track.getWeather() == Weather.LIGHT_RAIN) {
100+
this.currTyre = TyreCompound.INTERMEDIATE;
101+
return true;
102+
} else if (track.getWeather() == Weather.HEAVY_RAIN) {
103+
this.currTyre = TyreCompound.WET;
104+
return true;
105+
}
106+
return false;
107+
}
108+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.evanphilakhong;
2+
3+
public class NoSponsorsException extends Exception {
4+
public NoSponsorsException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.codedifferently.lesson16.evanphilakhong;
2+
3+
public enum Team {
4+
ALPINE,
5+
ASTON_MARTIN,
6+
FERRARI,
7+
HASS,
8+
MCLAREN,
9+
MERCEDES,
10+
VCARB,
11+
RED_BULL,
12+
SAUBER,
13+
WILLIAMS
14+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.codedifferently.lesson16.evanphilakhong;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
@Getter
7+
@Setter
8+
public class Track {
9+
private String trackName;
10+
private String trackLocation;
11+
private int numLaps;
12+
private double trackLengthInKm;
13+
private boolean drsZone;
14+
private Weather weather;
15+
16+
// constructors
17+
public Track() {
18+
this.trackName = "Suzuka Circuit";
19+
this.trackLocation = "Suzuka, Japan";
20+
this.numLaps = 53;
21+
this.trackLengthInKm = 5.807;
22+
this.drsZone = false;
23+
this.weather = Weather.CLEAR;
24+
}
25+
26+
public Track(
27+
String trackName,
28+
String trackLocation,
29+
int numLaps,
30+
double trackLengthInKm,
31+
boolean drsZone,
32+
Weather weather) {
33+
this.trackName = trackName;
34+
this.trackLocation = trackLocation;
35+
this.numLaps = numLaps;
36+
this.trackLengthInKm = trackLengthInKm;
37+
this.drsZone = drsZone;
38+
this.weather = weather;
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.codedifferently.lesson16.evanphilakhong;
2+
3+
enum TyreCompound {
4+
SOFT,
5+
MEDIUM,
6+
HARD,
7+
INTERMEDIATE,
8+
WET
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.evanphilakhong;
2+
3+
public enum Weather {
4+
CLEAR,
5+
LIGHT_RAIN,
6+
HEAVY_RAIN
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package com.codedifferently.lesson16.evanphilakhong;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
import java.io.ByteArrayOutputStream;
9+
import java.io.PrintStream;
10+
import org.junit.jupiter.api.AfterEach;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.Test;
13+
14+
class Formula1CarTest {
15+
16+
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
17+
Track track;
18+
Formula1Car f1Car1;
19+
Formula1Car f1Car2;
20+
21+
@BeforeEach
22+
void setUp() {
23+
track = new Track();
24+
25+
String[] f1Car1Sponsors = {"Hp", "Shell", "UniCredit", "IBM", "Puma", "VGW Play"};
26+
f1Car1 =
27+
new Formula1Car(Team.FERRARI, f1Car1Sponsors, "Lewis Hamilton", 44, 8, TyreCompound.MEDIUM);
28+
f1Car1.setTrack(track);
29+
30+
String[] f1Car2Sponsors = {};
31+
f1Car2 =
32+
new Formula1Car(Team.MCLAREN, f1Car2Sponsors, "Lando Norris", 4, 2, TyreCompound.MEDIUM);
33+
f1Car2.setTrack(track);
34+
35+
System.setOut(new PrintStream(outContent));
36+
}
37+
38+
@AfterEach
39+
void restoreStreams() {
40+
System.setOut(System.out);
41+
}
42+
43+
@Test
44+
void testIsDrsAvailibe_returnsTrue() {
45+
track.setDrsZone(true);
46+
47+
assertTrue(f1Car1.isDrsAvailible());
48+
}
49+
50+
@Test
51+
void testIsDrsAvailibe_returnsFalse() {
52+
track.setDrsZone(false);
53+
54+
assertFalse(f1Car1.isDrsAvailible());
55+
}
56+
57+
@Test
58+
void testRainStrategy_returnsTrue() {
59+
// checks if tyres change depending on weather conditions
60+
track.setWeather(Weather.LIGHT_RAIN);
61+
assertTrue(f1Car1.rainStrategy());
62+
assertEquals(f1Car1.getCurrTyre(), TyreCompound.INTERMEDIATE);
63+
64+
track.setWeather(Weather.HEAVY_RAIN);
65+
assertTrue(f1Car1.rainStrategy());
66+
assertEquals(f1Car1.getCurrTyre(), TyreCompound.WET);
67+
}
68+
69+
@Test
70+
void testRainStrategy_returnsFalse() {
71+
// checks the default condition no rain
72+
assertFalse(f1Car1.rainStrategy());
73+
}
74+
75+
@Test
76+
void testPrintSponsors_withSponsors() throws NoSponsorsException {
77+
if (f1Car1.getSponsors().length == 0) {
78+
throw new NoSponsorsException(f1Car1.getTeam() + " has no Sponsors");
79+
} else {
80+
String expected =
81+
"Here's a list of our Sponsors:"
82+
+ System.lineSeparator()
83+
+ "Hp"
84+
+ System.lineSeparator()
85+
+ "Shell"
86+
+ System.lineSeparator()
87+
+ "UniCredit"
88+
+ System.lineSeparator()
89+
+ "IBM"
90+
+ System.lineSeparator()
91+
+ "Puma"
92+
+ System.lineSeparator()
93+
+ "VGW Play"
94+
+ System.lineSeparator();
95+
96+
f1Car1.printSponsors();
97+
98+
assertEquals(expected, outContent.toString());
99+
}
100+
}
101+
102+
@Test
103+
void testPrintSponsors_noSponsors() {
104+
NoSponsorsException exception =
105+
assertThrows(NoSponsorsException.class, () -> f1Car2.printSponsors());
106+
107+
assertEquals(f1Car2.getTeam() + " has no Sponsors", exception.getMessage());
108+
}
109+
}

0 commit comments

Comments
 (0)