Skip to content
Snippets Groups Projects
Commit 765c362e authored by Patrick Kaster's avatar Patrick Kaster Committed by Patrick Kaster
Browse files

initial commit u5

parents
No related branches found
No related tags found
No related merge requests found
Showing
with 470 additions and 0 deletions
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>u5_p</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
import car.Car;
import car.Dealership;
import car.Invoice;
import car.exception.DealershipException;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Dealership carDealer = new Dealership(new HashMap<>(Map.ofEntries(
Map.entry(1, new Car(1, 10000, 100, 1000)),
Map.entry(2, new Car(2, 20000, 500, 2000))
)));
try {
Car carToBuy = carDealer.testDrive(1);
Invoice invoiceBoughtCar = carDealer.sell(carToBuy);
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;
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 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 {
if (!this.isAvailable()) {
throw new CarIsUnavailableException();
}
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();
}
this.damage = false;
}
public void accident() {
this.damage = true;
}
public int id() {
return this.id;
}
public int cost() {
return this.cost;
}
public int repairCost() {
return this.repairCost;
}
public int costPerMonth() {
return this.costPerMonth;
}
public boolean isSold() {
return this.sold;
}
public boolean isDamaged() {
return this.damage;
}
public boolean isAvailable() {
// check if a car is available (not sold or not rented)
return !this.isSold() && !this.isRented();
}
public boolean isRented() {
return this.monthsRented != 0;
}
}
package car;
import car.exception.CarIsUnavailableException;
import car.exception.DealershipException;
import car.exception.UnknownCarException;
import java.util.Map;
public class Dealership {
private Map<Integer, Car> cars;
public Dealership(Map<Integer, Car> cars) {
this.cars = cars;
}
public Car testDrive(int id) throws DealershipException {
if (!this.cars.containsKey(id)) {
throw new UnknownCarException();
}
Car car = this.cars.get(id);
if (!car.isAvailable()) {
throw new CarIsUnavailableException();
}
return this.cars.get(id);
}
public Invoice sell(Car carToSell) throws DealershipException {
if (!this.cars.containsKey(carToSell.id())) {
throw new UnknownCarException();
}
carToSell.sell();
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();
}
carToRepair.repair();
return Invoice.forRepairedCar(carToRepair);
}
}
package car;
public class Invoice {
private int carId;
private int price;
private Invoice(int carId, int price) {
this.carId = carId;
this.price = price;
}
public static Invoice forSoldCar(Car carToSell) {
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());
}
public int carId() {
return carId;
}
public int price() {
return price;
}
@Override
public String toString() {
return String.format("car.Invoice: $%d for car %d", this.price, this.carId);
}
}
package car.exception;
public class CarIsUnavailableException extends DealershipException {
}
package car.exception;
public class DealershipException extends Exception {
}
package car.exception;
public class NoNeedToRepairCarException extends DealershipException {
}
package car.exception;
public class UnableToRentCarException extends DealershipException {
}
package car.exception;
public class UnknownCarException extends DealershipException {
}
import car.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, 100);
car.sell();
car.sell();
}
@Test(expected = NoNeedToRepairCarException.class)
public void repairThrowsNoNeedToRepairCarExceptionIfCarIsNowDamaged() throws NoNeedToRepairCarException {
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
import car.Car;
import car.Dealership;
import car.Invoice;
import car.exception.CarIsUnavailableException;
import car.exception.DealershipException;
import car.exception.UnknownCarException;
import org.mockito.Mockito;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.junit.Assert;
public class DealershipTest {
@Test(expected = UnknownCarException.class)
public void testDriveThrowsUnknownCarExceptionIfCarIsNotInMap() throws DealershipException {
Dealership dealer = new Dealership(new HashMap<>());
dealer.testDrive(1);
}
@Test(expected = CarIsUnavailableException.class)
public void testDriveThrowsCarIsUnavailableExceptionIfCarIsAlreadySoldOrRented() throws DealershipException {
Car car = Mockito.mock(Car.class);
Mockito.when(car.id()).thenReturn(1);
Mockito.when(car.isAvailable()).thenReturn(false);
Map cars = new HashMap();
cars.put(1, car);
Dealership dealer = new Dealership(cars);
dealer.testDrive(1);
}
@Test
public void testDriveReturnsExpectedCar() {
Car car = Mockito.mock(Car.class);
Mockito.when(car.id()).thenReturn(1);
Mockito.when(car.isAvailable()).thenReturn(true);
Map cars = new HashMap();
cars.put(1, car);
Dealership dealer = new Dealership(cars);
try {
Assert.assertSame(car, dealer.testDrive(1));
} catch (DealershipException e) {
Assert.fail();
}
}
@Test(expected = UnknownCarException.class)
public void sellThrowsUnknownCarExceptionIfCarIsNotInMap() throws DealershipException {
Car car = Mockito.mock(Car.class);
Dealership dealer = new Dealership(new HashMap<>());
dealer.sell(car);
}
@Test
public void sellReturnsExpectedInvoice() {
int id = 1;
int price = 1000;
Car car = Mockito.mock(Car.class);
Mockito.when(car.id()).thenReturn(id);
Mockito.when(car.cost()).thenReturn(price);
Map cars = new HashMap();
cars.put(1, car);
Dealership dealer = new Dealership(cars);
try {
Invoice invoice = dealer.sell(car);
Assert.assertEquals("car.Invoice: $1000 for car 1", invoice.toString());
Mockito.verify(car, Mockito.times(1)).sell();
} catch (DealershipException e) {
Assert.fail();
}
}
@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("car.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);
Dealership dealer = new Dealership(new HashMap<>());
dealer.repair(car);
}
@Test
public void testRepairReturnsExpectedInvoice() {
int id = 1;
int repairPrice = 1000;
Car car = Mockito.mock(Car.class);
Mockito.when(car.id()).thenReturn(id);
Mockito.when(car.repairCost()).thenReturn(repairPrice);
Map cars = new HashMap();
cars.put(1, car);
Dealership dealer = new Dealership(cars);
try {
Invoice invoice = dealer.repair(car);
Assert.assertEquals("car.Invoice: $1000 for car 1", invoice.toString());
Mockito.verify(car, Mockito.times(1)).repair();
} catch (DealershipException e) {
Assert.fail();
}
}
}
\ No newline at end of file
public class RentCarStepdefs {
}
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(plugin = {"pretty"}, features={"src/test/resources/car/RentCar.feature"})
public class RunCucumberTest {
}
\ No newline at end of file
Feature: Car Rent
Scenario: Renting a Car works if car is available
Given There is a car available to rent
When Marvin took a test drive
And Marvin later decides to rent the car
Then Marvin should get an invoice
Scenario: Only one person can rent a car
Given There is a car available to rent
When Marvin took a test drive
And Patrick took a test drive
And Marvin later decides to rent the car
And Patrick later decides to rent the car
Then Patrick wont be able to rent the car
\ No newline at end of file
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