Skip to content

Commit

Permalink
Merge pull request #274 from patsonluk/issue273-new-airplane-models
Browse files Browse the repository at this point in the history
New airplane models merged from Germanwings change
  • Loading branch information
patsonluk authored Feb 23, 2020
2 parents 80fda2b + 76ef76b commit 749d740
Show file tree
Hide file tree
Showing 80 changed files with 172 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ object AirplaneModelSimulation {
//purge all the existing discounts due to low demand
val airplanesByModel = allAirplanes.groupBy(_.model)
val allModelDiscounts = ListBuffer[ModelDiscount]()
airplanesByModel.foreach {
case (model, airplanes) =>
allModelDiscounts.appendAll(getModelDiscountsByLowDemand(model, airplanes.length))
ModelSource.loadAllModels().foreach { model =>
allModelDiscounts.appendAll(getModelDiscountsByLowDemand(model, airplanesByModel.get(model) match {
case Some(airplanes) => airplanes.length
case None => 0
}))
}


ModelSource.updateModelDiscounts(allModelDiscounts.toList)
}

Expand Down
40 changes: 30 additions & 10 deletions airline-data/src/main/scala/com/patson/data/Patchers.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.patson.data

import com.patson.model.Computation
import com.patson.model._
import com.patson.data.airplane.ModelSource
import com.patson.model.airplane.Model
import com.patson.model.airplane._
import java.sql.PreparedStatement
import java.sql.Connection

import com.patson.data.Constants._
import com.mchange.v2.c3p0.ComboPooledDataSource
import com.patson.LinkSimulation

import scala.collection.mutable.ListBuffer
import com.patson.util.LogoGenerator

Expand Down Expand Up @@ -35,18 +37,36 @@ object Patchers {
}

def airplaneModelPatcher() {
ModelSource.updateModels(Model.models)

val existingModelNames = ModelSource.loadAllModels().map(_.name)

val existingModelsByName = ModelSource.loadAllModels().map(model => (model.name, model)).toMap

val newModels = ListBuffer[Model]()
Model.models.foreach { model =>
if (!existingModelNames.contains(model.name)) {
newModels.append(model)
existingModelsByName.get(model.name) match {
case Some(existingModel) =>
if (existingModel.price != model.price) { //adjust existing value
val updatingAirplanes = AirplaneSource.loadAirplanesCriteria(List(("a.model", existingModel.id))).map { airplane =>
val newValue = (model.price * airplane.condition / Airplane.MAX_CONDITION).toInt
airplane.copy(value = newValue);
}
AirplaneSource.updateAirplanesDetails(updatingAirplanes)
}
if (existingModel.capacity != model.capacity) { //adjust configuration and then actual capacity
AirplaneSource.loadAirplaneConfigurationsByCriteria(List(("model", existingModel.id))).foreach { configuration =>
val factor = model.capacity.toDouble / existingModel.capacity
var newCapacity = ((configuration.economyVal * factor).toInt, (configuration.businessVal * factor).toInt , (configuration.firstVal * factor).toInt)
val adjustmentDelta : Int = model.capacity - (newCapacity._1 * ECONOMY.spaceMultiplier + newCapacity._2 * BUSINESS.spaceMultiplier + newCapacity._3 * FIRST.spaceMultiplier).toInt
val newConfiguration = configuration.copy(economyVal = newCapacity._1 + adjustmentDelta, businessVal = newCapacity._2, firstVal = newCapacity._3)
AirplaneSource.updateAirplaneConfiguration(newConfiguration)
println(s"Configuration from $configuration to $newConfiguration")
}
}
case None => newModels.append(model)
}
}

ModelSource.updateModels(Model.models)
LinkSimulation.refreshLinksPostCycle()
ModelSource.saveModels(newModels.toList)

}

def patchDelaySchema() = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ object Computation {
val SELL_RATE = 0.8

def calculateAirplaneSellValue(airplane : Airplane) : Int = {
//80% off
val value = airplane.value * SELL_RATE
val currentNewMarketPrice = airplane.model.applyDiscount(ModelDiscount.getDiscounts(airplane.model.id)).price
val value = currentNewMarketPrice * SELL_RATE
if (value < 0) 0 else value.toInt
}

Expand Down
147 changes: 116 additions & 31 deletions airline-data/src/main/scala/com/patson/model/airplane/Model.scala

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,29 @@ object ModelDiscount {
List(ModelDiscount(model.id, priceDiscount, DiscountType.PRICE, DiscountReason.FAVORITE, None), constructionTimeDiscount)
}

/**
* Get discounts including both specific to airline and those blanket to model
* @param airlineId
* @param modelId
* @return
*/
def getDiscounts(airlineId : Int, modelId : Int) : List[ModelDiscount] = {
val discounts = ListBuffer[ModelDiscount]()
//get airline specific discounts
discounts.appendAll(ModelSource.loadAirlineDiscountsByAirlineIdAndModelId(airlineId, modelId))
//get blanket model discounts
discounts.appendAll(getDiscounts(modelId))
discounts.toList
}

/**
* Get discounts that is blanket to the model
* @param modelId
* @return
*/
def getDiscounts(modelId : Int) : List[ModelDiscount] = {
val discounts = ListBuffer[ModelDiscount]()
//get blanket model discounts
discounts.appendAll(ModelSource.loadModelDiscountsByModelId(modelId))
discounts.toList
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 749d740

Please sign in to comment.