Skip to content

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
merged 14 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
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);
}
}
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
}
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;
}
}
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
}
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
}
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());
}
}