-
Notifications
You must be signed in to change notification settings - Fork 23
feat: implements Formula1Car.java Class & Tests #500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
anthonydmays
merged 14 commits into
code-differently:main
from
TatsuyaRyujin:feat/lesson_16
Apr 11, 2025
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1627189
feat: adds Formula1Car methods
TatsuyaRyujin 3117c74
refactor: immplements lombok annotations
TatsuyaRyujin 7df9237
feat: adds Track.java class
TatsuyaRyujin 2f98b21
feat: adds Formula1Car methods
TatsuyaRyujin d7120f2
feat: adds Weather.java
TatsuyaRyujin d049059
feat: adds rainStrategy() method in Formula1Car Class
TatsuyaRyujin 515bf43
feat: adds NoSponsorsException.java
TatsuyaRyujin e13c3d8
feat: implements NoSponsorsException
TatsuyaRyujin 127e210
feat: adds Track.java constuctors
TatsuyaRyujin 5852e49
refactor: ./gradle spotlessApply
TatsuyaRyujin 1614b14
feat: adds Formula1Car.java tests
TatsuyaRyujin c0e39af
feat: adds printSponsors() tests
1b785e7
refactor: adds JavaDoc style
82c6ac6
fix: run ./gradleW spotlessApply
TatsuyaRyujin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
...ts/objects_app/src/main/java/com/codedifferently/lesson16/evanphilakhong/Formula1Car.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.codedifferently.lesson16.evanphilakhong; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class Formula1Car { | ||
private Track track; | ||
private Team team; | ||
private String[] sponsors; | ||
private String driverName; | ||
private int driverNum; | ||
private int position; | ||
private TyreCompound currTyre; | ||
private boolean drs; | ||
|
||
// constructors | ||
public Formula1Car() { | ||
this.team = null; | ||
this.sponsors = new String[0]; | ||
this.driverName = ""; | ||
this.driverNum = 0; | ||
this.currTyre = null; | ||
this.drs = false; | ||
} | ||
|
||
public Formula1Car( | ||
Team team, | ||
String[] sponsors, | ||
String driverName, | ||
int driverNum, | ||
int position, | ||
TyreCompound currTyre) { | ||
this.team = team; | ||
this.sponsors = sponsors; | ||
this.driverName = driverName; | ||
this.driverNum = driverNum; | ||
this.position = position; | ||
this.currTyre = currTyre; | ||
this.drs = false; | ||
} | ||
|
||
// methods | ||
public void printSponsors() throws NoSponsorsException { | ||
if (sponsors.length == 0) { | ||
throw new NoSponsorsException(team + " has no Sponsors"); | ||
} else { | ||
System.out.println("Here's a list of our Sponsors:"); | ||
for (String sponsor : sponsors) { | ||
System.out.println(sponsor); | ||
} | ||
} | ||
} | ||
|
||
public boolean isDrsAvailible() { | ||
if (track.isDrsZone()) { | ||
this.drs = true; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public boolean rainStrategy() { | ||
if (track.getWeather() == Weather.LIGHT_RAIN) { | ||
this.currTyre = TyreCompound.INTERMEDIATE; | ||
return true; | ||
} else if (track.getWeather() == Weather.HEAVY_RAIN) { | ||
this.currTyre = TyreCompound.WET; | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...ts_app/src/main/java/com/codedifferently/lesson16/evanphilakhong/NoSponsorsException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.codedifferently.lesson16.evanphilakhong; | ||
|
||
public class NoSponsorsException extends Exception { | ||
public NoSponsorsException(String message) { | ||
super(message); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...6/objects/objects_app/src/main/java/com/codedifferently/lesson16/evanphilakhong/Team.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.codedifferently.lesson16.evanphilakhong; | ||
|
||
public enum Team { | ||
ALPINE, | ||
ASTON_MARTIN, | ||
FERRARI, | ||
HASS, | ||
MCLAREN, | ||
MERCEDES, | ||
VCARB, | ||
RED_BULL, | ||
SAUBER, | ||
WILLIAMS | ||
} |
40 changes: 40 additions & 0 deletions
40
.../objects/objects_app/src/main/java/com/codedifferently/lesson16/evanphilakhong/Track.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.codedifferently.lesson16.evanphilakhong; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class Track { | ||
private String trackName; | ||
private String trackLocation; | ||
private int numLaps; | ||
private double trackLengthInKm; | ||
private boolean drsZone; | ||
private Weather weather; | ||
|
||
// constructors | ||
public Track() { | ||
this.trackName = "Suzuka Circuit"; | ||
this.trackLocation = "Suzuka, Japan"; | ||
this.numLaps = 53; | ||
this.trackLengthInKm = 5.807; | ||
this.drsZone = false; | ||
this.weather = Weather.CLEAR; | ||
} | ||
|
||
public Track( | ||
String trackName, | ||
String trackLocation, | ||
int numLaps, | ||
double trackLengthInKm, | ||
boolean drsZone, | ||
Weather weather) { | ||
this.trackName = trackName; | ||
this.trackLocation = trackLocation; | ||
this.numLaps = numLaps; | ||
this.trackLengthInKm = trackLengthInKm; | ||
this.drsZone = drsZone; | ||
this.weather = weather; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...s/objects_app/src/main/java/com/codedifferently/lesson16/evanphilakhong/TyreCompound.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.codedifferently.lesson16.evanphilakhong; | ||
|
||
enum TyreCompound { | ||
SOFT, | ||
MEDIUM, | ||
HARD, | ||
INTERMEDIATE, | ||
WET | ||
} |
7 changes: 7 additions & 0 deletions
7
...bjects/objects_app/src/main/java/com/codedifferently/lesson16/evanphilakhong/Weather.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.codedifferently.lesson16.evanphilakhong; | ||
|
||
public enum Weather { | ||
CLEAR, | ||
LIGHT_RAIN, | ||
HEAVY_RAIN | ||
} |
109 changes: 109 additions & 0 deletions
109
...bjects_app/src/test/java/com/codedifferently/lesson16/evanphilakhong/Formula1CarTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package com.codedifferently.lesson16.evanphilakhong; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class Formula1CarTest { | ||
|
||
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); | ||
Track track; | ||
Formula1Car f1Car1; | ||
Formula1Car f1Car2; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
track = new Track(); | ||
|
||
String[] f1Car1Sponsors = {"Hp", "Shell", "UniCredit", "IBM", "Puma", "VGW Play"}; | ||
f1Car1 = | ||
new Formula1Car(Team.FERRARI, f1Car1Sponsors, "Lewis Hamilton", 44, 8, TyreCompound.MEDIUM); | ||
f1Car1.setTrack(track); | ||
|
||
String[] f1Car2Sponsors = {}; | ||
f1Car2 = | ||
new Formula1Car(Team.MCLAREN, f1Car2Sponsors, "Lando Norris", 4, 2, TyreCompound.MEDIUM); | ||
f1Car2.setTrack(track); | ||
|
||
System.setOut(new PrintStream(outContent)); | ||
} | ||
|
||
@AfterEach | ||
void restoreStreams() { | ||
System.setOut(System.out); | ||
} | ||
|
||
@Test | ||
void testIsDrsAvailibe_returnsTrue() { | ||
track.setDrsZone(true); | ||
|
||
assertTrue(f1Car1.isDrsAvailible()); | ||
} | ||
|
||
@Test | ||
void testIsDrsAvailibe_returnsFalse() { | ||
track.setDrsZone(false); | ||
|
||
assertFalse(f1Car1.isDrsAvailible()); | ||
} | ||
|
||
@Test | ||
void testRainStrategy_returnsTrue() { | ||
// checks if tyres change depending on weather conditions | ||
track.setWeather(Weather.LIGHT_RAIN); | ||
assertTrue(f1Car1.rainStrategy()); | ||
assertEquals(f1Car1.getCurrTyre(), TyreCompound.INTERMEDIATE); | ||
|
||
track.setWeather(Weather.HEAVY_RAIN); | ||
assertTrue(f1Car1.rainStrategy()); | ||
assertEquals(f1Car1.getCurrTyre(), TyreCompound.WET); | ||
} | ||
|
||
@Test | ||
void testRainStrategy_returnsFalse() { | ||
// checks the default condition no rain | ||
assertFalse(f1Car1.rainStrategy()); | ||
} | ||
|
||
@Test | ||
void testPrintSponsors_withSponsors() throws NoSponsorsException { | ||
if (f1Car1.getSponsors().length == 0) { | ||
throw new NoSponsorsException(f1Car1.getTeam() + " has no Sponsors"); | ||
} else { | ||
String expected = | ||
"Here's a list of our Sponsors:" | ||
+ System.lineSeparator() | ||
+ "Hp" | ||
+ System.lineSeparator() | ||
+ "Shell" | ||
+ System.lineSeparator() | ||
+ "UniCredit" | ||
+ System.lineSeparator() | ||
+ "IBM" | ||
+ System.lineSeparator() | ||
+ "Puma" | ||
+ System.lineSeparator() | ||
+ "VGW Play" | ||
+ System.lineSeparator(); | ||
|
||
f1Car1.printSponsors(); | ||
|
||
assertEquals(expected, outContent.toString()); | ||
} | ||
} | ||
|
||
@Test | ||
void testPrintSponsors_noSponsors() { | ||
NoSponsorsException exception = | ||
assertThrows(NoSponsorsException.class, () -> f1Car2.printSponsors()); | ||
|
||
assertEquals(f1Car2.getTeam() + " has no Sponsors", exception.getMessage()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.