Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
88 changes: 88 additions & 0 deletions src/main/java/object/example/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package object.example;

public class Car {
private String model;
private String brand;
private int year;
private String color;
private int mileage;
private boolean isElectric;

public Car(String model, String brand, int year, String color, int mileage, boolean isElectric) {
this.model = model;
this.brand = brand;
this.year = year;
this.color = color;
if (mileage < 0) {
throw new IllegalArgumentException("Mileage cannot be negative");
}
this.mileage = mileage;
this.isElectric = isElectric;
}

public String getModel() {
return model;
}

public void setModel(String model) {
this.model = model;
}

public String getBrand() {
return brand;
}

public void setBrand(String brand) {
this.brand = brand;
}

public int getYear() {
return year;
}

public void setYear(int year) {
this.year = year;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

public int getMileage() {
return mileage;
}

public void setMileage(int mileage) {
this.mileage = mileage;
}

public boolean isElectric() {
return isElectric;
}

public void setElectric(boolean electric) {
isElectric = electric;
}

public void drive(int milesDriven){
setMileage(getMileage() + milesDriven);
}

public void displayCarInfo(){
System.out.println("**********************");
System.out.println("Model " + getModel());
System.out.println("Brand " + getBrand());
System.out.println("Year: " + getYear());
System.out.println("Color " + getColor());
System.out.println("Mileage " + getMileage());
if(isElectric()) {
System.out.println("This vehicle runs on electricity.");
} else {
System.out.println("This vehicle runs on gasoline.");
}
}
}
110 changes: 110 additions & 0 deletions src/test/java/object/example/CarTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package object.example;

import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class CarTest {
@Test
public void constructorShouldCreatePersonWithValidData() {
Car car = new Car("Volkswagen", "Jetta", 2015 , "brown", 110_000, false);

assertEquals("Volkswagen", car.getModel());
assertEquals("Jetta", car.getBrand());
assertEquals(2015, car.getYear());
assertEquals("brown", car.getColor());
assertEquals(110000, 110_000);
assertEquals(false, car.isElectric());

}

@Test
public void constructorWithNegativeMileageThrowException() {
assertThrows(
IllegalArgumentException.class,
() -> new Car("Honda", "Civic", 2010,
"grey", -50_000, false)
);
}

@Test
public void setModeleShouldUpdateModel() {
Car car = new Car("Honda", "Civic", 2010, "grey", 50_000, false);
car.setModel("Honda");
assertEquals("Honda", car.getModel());
}

@Test
public void setBrandShouldUpdate() {
Car car = new Car("Honda", "Civic", 2010, "grey", 50_000, false);
car.setBrand("Civic");
assertEquals("Civic", car.getBrand());
}

@Test
public void setYearShouldUpdateYear() {
Car car = new Car("Honda", "Civic", 2010, "grey", 50_000, false);
car.setYear(2010);
assertEquals(2010, car.getYear());
}

@Test
public void setColorShouldUpdateColor() {
Car car = new Car("Honda", "Civic", 2010, "grey", 50_000, false);
car.setColor("grey");
assertEquals("grey", car.getColor());
}

@Test
public void setMileageShouldUpdateMileage() {
Car car = new Car("Honda", "Civic", 2010, "grey", 50_000, false);
car.setMileage(50_000);
assertEquals(50_000, car.getMileage());
}

@Test
public void setElectricShouldUpdateElectric() {
Car car = new Car("Honda", "Civic", 2010, "grey", 50_000, false);
car.setElectric(true);
assertEquals(true, car.isElectric());
}

@Test
public void testDrive() {
Car car = new Car("Civic", "Honda", 2020, "Blue", 10000, false);

car.drive(500);

assertEquals(10500, car.getMileage());
}

@Test
public void testDisplayCarInfo() {
Car car = new Car("Model S", "Tesla", 2023, "Red", 15000, true);

ByteArrayOutputStream output = new ByteArrayOutputStream();
System.setOut(new PrintStream(output));

car.displayCarInfo();

String expectedOutput = """
**********************
Model Model S
Brand Tesla
Year: 2023
Color Red
Mileage 15000
This vehicle runs on electricity.
""";

assertEquals(expectedOutput, output.toString());
}




}