Skip to content

Commit

Permalink
Some code cleaning and commenting
Browse files Browse the repository at this point in the history
  • Loading branch information
lukecollier committed Jul 4, 2017
1 parent 0de37d1 commit c5cd879
Show file tree
Hide file tree
Showing 47 changed files with 84 additions and 17 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

## Part 1 Console Application
To use the console application goto the project directory and type `java -cp target/rentalcar-technical-test-1.0-SNAPSHOT-jar-with-dependencies com.rentalcars.app.ConsoleApplication` in the console of your choice.
![Proof of the working console application](images/console-proof.png?raw=true)
![Proof of the working console application](images/console-proof.png =500x?raw=true)

## Part 2 API
Either run the bash script at `/target/universal/com-rentalcars-rest-api-1.0-SNAPSHOT/bin/com-rentalcars-rest-api`
Expand All @@ -12,11 +12,11 @@ The server will attempt to run on port 9000 of the machine (localhost:9000).

Once the localhost is running there are 4 different url's for each of the tasks
1. http://localhost:9000/car/sort/price - get's the vehicle list in price order
![Sort price working](images/sort-price.png?raw=true)
![Sort price working](images/sort-price.png =500x?raw=true)
2. http://localhost:9000/car/summary - get's the vehicle list summary
![Sort price working](images/summary.png?raw=true)
![Sort price working](images/summary.png =500x?raw=true)
3. http://localhost:9000/car/sort/rating - get's the vehicle list in rating and
vehicle type order
![Sort price working](images/sort-rating.png?raw=true)
![Sort price working](images/sort-rating =500x.png?raw=true)
4. http://localhost:9000/car/sort/score - get's the vehicle list by score order
![Sort price working](images/sort-score.png?raw=true)
![Sort price working](images/sort-score.png =500x?raw=true)
23 changes: 17 additions & 6 deletions app/com/rentalcars/CarInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import java.util.stream.Collectors;


/**
* Get's car list information as a list of strings
*/
public class CarInfo {
// Handy variables with the path's to the relevant resource files
private final static String CAR_TYPES_PATH = "/car-types.json";
Expand All @@ -31,7 +34,7 @@ public class CarInfo {
/**
* Initializes the different LetterToValue types by using try-with-resources that will automatically close the
* stream
* @throws IOException
* @throws IOException Throws an exception if anything goes wrong with input stream of resources
*/
public CarInfo() throws IOException {
try(InputStream inputStreamCarTypes = getClass().getResourceAsStream(CAR_TYPES_PATH)){
Expand All @@ -56,7 +59,7 @@ public CarInfo() throws IOException {
/**
* Task one of the technical test, get's the name and price ordered by the price in ascending order.
* @param carList the car list to order and get the string of
* @return
* @return a string list of cars
*/
public String formatPriceOrder(List<Car> carList) {
return carList.stream()
Expand All @@ -69,10 +72,9 @@ public String formatPriceOrder(List<Car> carList) {
/**
* Task two of the technical test, get's the name, sipp code, sipp code meanings output in the order it was received
* @param carList the car list to get the string of
* @return
* @throws IOException
* @return a string list of cars
*/
public String formatCarSummary(List<Car> carList) throws IOException {
public String formatCarSummary(List<Car> carList) {
return carList.stream()
.map(car -> String.format("{%s} - {%s} - {%s} - {%s} - {%s} - {%s} - {%s}",
car.getName(),
Expand All @@ -86,6 +88,11 @@ public String formatCarSummary(List<Car> carList) throws IOException {
.collect(Collectors.joining("\n"));
}

/**
* Formats the list of cars by rating and car type order
* @param carList the car list to format and sorted
* @return a string list of cars
*/
public String formatCarTypeRatingOrder(List<Car> carList) {
return carList.stream()
.sorted((car1, car2) -> {
Expand All @@ -106,6 +113,11 @@ public String formatCarTypeRatingOrder(List<Car> carList) {
.collect(Collectors.joining("\n"));
}

/**
* Formats the list sorted by a score value
* @param carList the car list to format and sort
* @return a string list of cars
*/
public String formatCarScoreOrder(List<Car> carList) {
return carList.stream()
.sorted((car1, car2) -> {
Expand All @@ -126,7 +138,6 @@ public String formatCarScoreOrder(List<Car> carList) {
* Takes the last two characters of the SIPP and finds their combined score
* @param sipp a string that is the SiPP code to use
* @return an integer value representing the total score of the SiPP
* @throws IOException if an error occurs with the input stream
*/
private int getScoreFromSipp(String sipp) {
return transmissionTypes.getScoreFromLetter(sipp.charAt(TRANS_POS))
Expand Down
8 changes: 8 additions & 0 deletions app/com/rentalcars/ConsoleApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@
import java.net.URL;
import java.util.List;

/**
* The console application get's and displays all the tasks in the technical test to a console
*/
public class ConsoleApplication {

private final static String TARGET_WEBSITE_PATH = "http://www.rentalcars.com/js/vehicles.json";

/**
* The main method that runs
* @param args
* @throws IOException if any of the streams have issues
*/
public static void main(String[] args) throws IOException {

// The website url to get the data from
Expand Down
2 changes: 1 addition & 1 deletion app/com/rentalcars/LetterToValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.Map;

/**
* Specifications are a map of characters to an string
* Specifications are a map of characters to a string and score value
*/
public class LetterToValue {
private final Map<Character, SpecificationWrapper> specificationData;
Expand Down
3 changes: 3 additions & 0 deletions app/com/rentalcars/models/Car.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.math.BigDecimal;

/**
* A car object that matches the vehicles car objects
*/
public class Car {
private String sipp;
private String name;
Expand Down
3 changes: 3 additions & 0 deletions app/com/rentalcars/models/Search.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.util.List;

/**
* Matches the structure of the rentalcars vehicles json file
*/
public class Search {
public List<Car> VehicleList;
}
3 changes: 3 additions & 0 deletions app/com/rentalcars/models/SpecType.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.rentalcars.models;

/**
* A specification type used to be mapped to against the resources json types
*/
public class SpecType {
private Character letter;
private String value;
Expand Down
3 changes: 3 additions & 0 deletions app/com/rentalcars/models/VehiclesResponse.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.rentalcars.models;

/**
* Matches the structure of the rentalcars vehicles json file
*/
public class VehiclesResponse {
public Search Search = new Search();
}
Expand Down
29 changes: 26 additions & 3 deletions app/controllers/CarsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,59 @@
import java.io.*;
import java.net.URL;

/**
* Cars controller used by play framework to serve requests
*/
public class CarsController extends Controller {

private final static String TARGET_WEBSITE_PATH = "http://www.rentalcars.com/js/vehicles.json";

/**
* Serves a list of vehicles when requested sorted by price
* @return a string list of vehicles
* @throws IOException thrown if something goes wrong with rentalcars response
*/
public Result getSortPrice() throws IOException {
CarInfo carInfo = new CarInfo();
return ok(carInfo.formatPriceOrder(getRentalCarsVehiclesResponse().Search.VehicleList));

}

/**
* Serves a list of vehicles when requested with summary information shown
* @return a string list of vehicles
* @throws IOException thrown if something goes wrong with rentalcars response
*/
public Result getCarSummary() throws IOException {
CarInfo carInfo = new CarInfo();
return ok(carInfo.formatCarSummary(getRentalCarsVehiclesResponse().Search.VehicleList));
}

/**
* Serves a list of vehicles when requested sorted by rating and car type
* @return a string list of vehicles
* @throws IOException thrown if something goes wrong with rentalcars response
*/
public Result getSortCarRating() throws IOException {
CarInfo carInfo = new CarInfo();
return ok(carInfo.formatCarTypeRatingOrder(getRentalCarsVehiclesResponse().Search.VehicleList));
}

/**
* Serves a list of vehicles when requested sorted by score
* @return a string list of vehicles
* @throws IOException thrown if something goes wrong with rentalcars response
*/
public Result getSortScore() throws IOException {
CarInfo carInfo = new CarInfo();
return ok(carInfo.formatCarScoreOrder(getRentalCarsVehiclesResponse().Search.VehicleList));
}


/**
* The rental cars vehicle response
* @return a object of rental cars
* @throws IOException
* The rental cars vehicle response mapped into an object
* @return an list of cars
* @throws IOException thrown if an issue occurs with getting the json file
*/
private static VehiclesResponse getRentalCarsVehiclesResponse() throws IOException {
// The website url to get the data from
Expand Down
Binary file modified target/scala-2.11/classes/com/rentalcars/CarInfo.class
Binary file not shown.
Binary file modified target/scala-2.11/classes/com/rentalcars/ConsoleApplication.class
Binary file not shown.
Binary file modified target/scala-2.11/classes/com/rentalcars/LetterToValue$1.class
Binary file not shown.
Binary file not shown.
Binary file modified target/scala-2.11/classes/com/rentalcars/LetterToValue.class
Binary file not shown.
Binary file modified target/scala-2.11/classes/com/rentalcars/models/Car.class
Binary file not shown.
Binary file modified target/scala-2.11/classes/com/rentalcars/models/Search.class
Binary file not shown.
Binary file modified target/scala-2.11/classes/com/rentalcars/models/SpecType.class
Binary file not shown.
Binary file not shown.
Binary file modified target/scala-2.11/classes/controllers/CarsController.class
Binary file not shown.
Binary file modified target/scala-2.11/classes/controllers/ReverseAssets.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified target/scala-2.11/classes/controllers/routes$javascript.class
Binary file not shown.
Binary file modified target/scala-2.11/classes/controllers/routes.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified target/scala-2.11/classes/router/Routes$$anonfun$routes$1.class
Binary file not shown.
Binary file modified target/scala-2.11/classes/router/Routes.class
Binary file not shown.
Binary file modified target/scala-2.11/classes/router/RoutesPrefix$$anonfun$1.class
Binary file not shown.
Binary file modified target/scala-2.11/classes/router/RoutesPrefix$.class
Binary file not shown.
Binary file modified target/scala-2.11/classes/router/RoutesPrefix.class
Binary file not shown.
17 changes: 15 additions & 2 deletions test/com/rentalcars/LetterToValueTest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.rentalcars;

import org.junit.Test;
import com.rentalcars.LetterToValue;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

/**
* Tests the letter to value class using the car-types.json
*/
public class LetterToValueTest {
// Handy variables with the path's to the relevant resource files
private final static String CAR_TYPES_PATH = "/car-types.json";
Expand All @@ -22,11 +23,23 @@ public void testReadSpecificationFromFile() throws IOException {
}
}

/**
* Test that checks that the entire json object is correctly mapped
* @throws IOException
*/
@Test
public void testGetValueFromLetter() throws IOException {
try(InputStream inputStreamCarTypes = getClass().getResourceAsStream(CAR_TYPES_PATH)) {
LetterToValue letterToValue = LetterToValue.readSpecification(inputStreamCarTypes);
assertEquals(letterToValue.getValueFromLetter('M'), "Mini");
assertEquals(letterToValue.getValueFromLetter('E'), "Economy");
assertEquals(letterToValue.getValueFromLetter('C'), "Compact");
assertEquals(letterToValue.getValueFromLetter('I'), "Intermediate");
assertEquals(letterToValue.getValueFromLetter('S'), "Standard");
assertEquals(letterToValue.getValueFromLetter('F'), "Full size");
assertEquals(letterToValue.getValueFromLetter('P'), "Premium");
assertEquals(letterToValue.getValueFromLetter('L'), "Luxury");
assertEquals(letterToValue.getValueFromLetter('X'), "Special");
}
}

Expand Down

0 comments on commit c5cd879

Please sign in to comment.