Skip to content

Commit

Permalink
Sort answer by price
Browse files Browse the repository at this point in the history
  • Loading branch information
nikalaikina committed Apr 4, 2016
1 parent ee38c1c commit 4aa14a9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class FlightsProvider(val cities: List[String], val dateFrom: LocalDate, val dat
println("Got flights info")

private def getFlightsFromServer(from: String, to: String, dateFrom: LocalDate, dateTo: LocalDate) = {
val urlPattern = s"https://api.skypicker.com/flights?flyFrom=$from&to=$to&dateFrom=${formatter.format(dateFrom)}&dateTo=${formatter.format(dateTo)}&directFlight=1&price_to=70"
val urlPattern = s"https://api.skypicker.com/flights?flyFrom=$from&to=$to&dateFrom=${formatter.format(dateFrom)}&dateTo=${formatter.format(dateTo)}&directFlight=1"
parseFlights(Http.get(urlPattern))
}

Expand Down
8 changes: 6 additions & 2 deletions src/main/scala/com/github/nikalaikina/Logic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,27 @@ import java.time.temporal.ChronoUnit

import scala.collection.immutable.IndexedSeq
import scala.collection.mutable
import scala.collection.mutable.ListBuffer

class Logic(val settings: Settings) {
val flightsProvider = new FlightsProvider(settings.cities, settings.dateFrom, settings.dateTo)

def writeAnswer(writer: PrintWriter) = {
var queue = mutable.Queue[Route]()
queue ++= (for (city <- settings.homeCities; day <- getFirstDays) yield new Route(city, day))
var routes = new ListBuffer[Route]()

while (queue.nonEmpty) {
val current = queue.dequeue
val day = current.days
if (isFine(current)) {
writer.println(current)
routes += current
} else if (day < settings.daysTo && current.cost < settings.cost) {
processNode(queue, current)
}
}

routes.sortBy(r => r.cost).foreach(r => writer.println(r))
}

private def isFine(route: Route): Boolean = {
Expand All @@ -46,7 +50,7 @@ class Logic(val settings: Settings) {
}

private def getFlights(route: Route, city: String) = {
flightsProvider.getFlights(route.curCity, city, route.date.plusDays(2), route.date.plusDays(settings.daysTo))
flightsProvider.getFlights(route.curCity, city, route.curDate.plusDays(2), route.curDate.plusDays(settings.daysTo))
}

private def getFirstDays: IndexedSeq[LocalDate] = {
Expand Down
8 changes: 5 additions & 3 deletions src/main/scala/com/github/nikalaikina/Route.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package com.github.nikalaikina
import java.time.LocalDate
import java.time.temporal.ChronoUnit

class Route(val firstCity: String, val date: LocalDate) {
class Route(val firstCity: String, val firstDate: LocalDate) {
var flights: List[Flight] = List()

def this(node: Route, flight: Flight) {
this(node.firstCity, node.date)
this(node.firstCity, node.firstDate)
flights = node.flights :+ flight
}

def days = if (flights.isEmpty) 0L else ChronoUnit.DAYS.between(date, flights.last.date)
def days = if (flights.isEmpty) 0L else ChronoUnit.DAYS.between(firstDate, flights.last.date)

def cities(except: List[String]) = {
flights.map(f => f.flyTo).distinct.count(c => !except.contains(c))
Expand All @@ -21,5 +21,7 @@ class Route(val firstCity: String, val date: LocalDate) {

def curCity = if (flights.isEmpty) firstCity else flights.last.flyTo

def curDate = if (flights.isEmpty) firstDate else flights.last.date

override def toString = s"${flights.size} $cost\t$flights"
}

0 comments on commit 4aa14a9

Please sign in to comment.