Skip to content
Snippets Groups Projects
Commit 523ef51b authored by MarvinKlemp's avatar MarvinKlemp
Browse files

Merge branch 'feature/rent' into develop

parents f806258a ebf5915d
No related branches found
No related tags found
No related merge requests found
......@@ -2,25 +2,31 @@ package car;
import car.exception.CarIsUnavailableException;
import car.exception.NoNeedToRepairCarException;
import car.exception.UnableToRentCarException;
public class Car {
private int id;
private int cost;
private int repairCost;
private int costPerMonth;
private boolean sold;
private int monthsRented;
private boolean damage;
public Car(int id, int cost, int repairCost) {
public Car(int id, int cost, int costPerMonth, int repairCost) {
this.id = id;
this.cost = cost;
this.repairCost = repairCost;
this.costPerMonth = costPerMonth;
this.sold = false;
this.damage = false;
this.monthsRented = 0;
}
public void sell() throws CarIsUnavailableException {
......@@ -31,6 +37,18 @@ public class Car {
this.sold = true;
}
public void rent(int months) throws UnableToRentCarException {
if (months <= 0) {
throw new IllegalArgumentException();
}
if (this.monthsRented != 0) {
throw new UnableToRentCarException();
}
this.monthsRented = months;
}
public void repair() throws NoNeedToRepairCarException {
if (!this.damage) {
throw new NoNeedToRepairCarException();
......@@ -55,6 +73,10 @@ public class Car {
return this.repairCost;
}
public int costPerMonth() {
return this.costPerMonth;
}
public boolean isSold() {
return this.sold;
}
......@@ -66,6 +88,10 @@ public class Car {
public boolean isAvailable() {
// check if a car is available (not sold or not rented)
return !this.isSold();
return !this.isSold() && !this.isRented();
}
public boolean isRented() {
return this.monthsRented != 0;
}
}
......@@ -4,6 +4,7 @@ import car.exception.CarIsUnavailableException;
import car.exception.DealershipException;
import car.exception.NoNeedToRepairCarException;
import car.exception.UnknownCarException;
import car.exception.*;
import java.util.Map;
......@@ -37,6 +38,16 @@ public class Dealership {
return Invoice.forSoldCar(carToSell);
}
public Invoice rent(Car carToRent, int months) throws DealershipException {
if (!this.cars.containsKey(carToRent.id())) {
throw new UnknownCarException();
}
carToRent.rent(months);
return Invoice.forRentedCar(carToRent, months);
}
public Invoice repair(Car carToRepair) throws DealershipException {
if (!this.cars.containsKey(carToRepair.id())) {
throw new UnknownCarException();
......
......@@ -14,6 +14,10 @@ public class Invoice {
return new Invoice(carToSell.id(), carToSell.cost());
}
public static Invoice forRentedCar(Car carToRent, int months) {
return new Invoice(carToRent.id(), carToRent.costPerMonth() * months);
}
public static Invoice forRepairedCar(Car carToRepair) {
return new Invoice(carToRepair.id(), carToRepair.repairCost());
}
......
......@@ -9,18 +9,22 @@ public class Main {
public static void main(String[] args) {
Dealership carDealer = new Dealership(new HashMap<>(Map.ofEntries(
Map.entry(1, new Car(1, 10000, 1000)),
Map.entry(2, new Car(2, 20000, 2000))
Map.entry(1, new Car(1, 10000, 100, 1000)),
Map.entry(2, new Car(2, 20000, 500, 2000))
)));
try {
Car car = carDealer.testDrive(1);
Invoice invoiceBoughtCar = carDealer.sell(car);
Car carToBuy = carDealer.testDrive(1);
Invoice invoiceBoughtCar = carDealer.sell(carToBuy);
car.accident();
Invoice repairedCarInvoice = carDealer.repair(car);
Car carToRent = carDealer.testDrive(2);
Invoice invoiceRentedCar = carDealer.rent(carToRent, 10);
carToBuy.accident();
Invoice repairedCarInvoice = carDealer.repair(carToBuy);
System.out.println(invoiceBoughtCar);
System.out.println(invoiceRentedCar);
System.out.println(repairedCarInvoice);
} catch (DealershipException e) {
System.out.println(e);
......
package car.exception;
public class UnableToRentCarException extends DealershipException {
}
......@@ -2,13 +2,15 @@ package car;
import car.exception.CarIsUnavailableException;
import car.exception.NoNeedToRepairCarException;
import car.exception.UnableToRentCarException;
import org.junit.Assert;
import org.junit.Test;
public class CarTest {
@Test(expected = CarIsUnavailableException.class)
public void sellThrowsCarIsUnavailableExceptionIfCarIsUnavailable() throws CarIsUnavailableException {
Car car = new Car(1, 1000, 100);
Car car = new Car(1, 1000, 100, 100);
car.sell();
car.sell();
......@@ -16,8 +18,28 @@ public class CarTest {
@Test(expected = NoNeedToRepairCarException.class)
public void repairThrowsNoNeedToRepairCarExceptionIfCarIsNowDamaged() throws NoNeedToRepairCarException {
Car car = new Car(1, 1000, 100);
Car car = new Car(1, 1000, 100, 100);
car.repair();
}
@Test(expected = IllegalArgumentException.class)
public void rentThrowsIllegalArgumentExceptionWithNegativeMonthsArgument() throws UnableToRentCarException {
Car car = new Car(1, 1000, 100, 100);
car.rent(-10);
}
@Test(expected = UnableToRentCarException.class)
public void rentThrowsUnableToRentCarExceptionIfAlreadyRented() throws UnableToRentCarException {
Car car = new Car(1, 1000, 100, 100);
try {
car.rent(10);
} catch (UnableToRentCarException e) {
Assert.fail();
}
car.rent(10);
}
}
\ No newline at end of file
......@@ -77,6 +77,36 @@ public class DealershipTest {
}
}
@Test(expected = UnknownCarException.class)
public void rentThrowsUnknownCarExceptionIfCarIsNotInMap() throws DealershipException {
Car car = Mockito.mock(Car.class);
Dealership dealer = new Dealership(new HashMap<>());
dealer.rent(car, 10);
}
@Test
public void rentReturnsExpectedInvoice() {
int id = 1;
int rentPrice = 1000;
Car car = Mockito.mock(Car.class);
Mockito.when(car.id()).thenReturn(id);
Mockito.when(car.costPerMonth()).thenReturn(rentPrice);
Map cars = new HashMap();
cars.put(1, car);
Dealership dealer = new Dealership(cars);
try {
Invoice invoice = dealer.rent(car, 10);
Assert.assertEquals("Invoice: $10000 for car 1", invoice.toString());
Mockito.verify(car, Mockito.times(1)).rent(10);
} catch (DealershipException e) {
Assert.fail();
}
}
@Test(expected = UnknownCarException.class)
public void repairThrowsUnknownCarExceptionIfCarIsNotInMap() throws DealershipException {
Car car = Mockito.mock(Car.class);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment