Demo entry 1802914

jjj

   

Submitted by jjj on Jun 03, 2015 at 06:49
Language: Scala. Code size: 1.7 kB.

// Code snippet that can be run in Scala worksheet or Scala/SBT console
// Abstract parent class
abstract class VinFormat {
  val vin: String = "1234567890"
}

// The ISO format is one of the many VIN formats
case class Iso3779VinFormat(
    val wmiCode: String, 
    val vds: String, 
    val vis: String) extends VinFormat {
  override val vin = wmiCode + vds + vis
}

// The US and Canadian VIN format
case class NorthAmericanVinFormat(
    val wmiCode: String, 
    val vehicleAttributes:String, 
    val checkDigit: String, 
    val modelYear: String, 
    val plantCode: String, 
    val sequenceNumber: String) extends VinFormat{
  override val vin = wmiCode +
    vehicleAttributes + 
    checkDigit + 
    modelYear + 
    plantCode + 
    sequenceNumber
}

// Function that accepts a VIN and takes appropriate actions
def checkVIN(vin: VinFormat) {
  vin match {
    case Iso3779VinFormat(wmiCode, vds, vis) => println("ISO VIN: " + vin)
    case NorthAmericanVinFormat(wmiCode,
      vehicleAttributes, 
      checkDigit, 
      modelYear, 
      plantCode, 
      sequenceNumber) => println("North America VIN: " + vin)
  }
}

val jeep = new NorthAmericanVinFormat("wmiCode",
  "vehicleAttributes",
  "checkDigit",
  "modelYear",
  "plantCode",
  "sequenceNumber")

println(jeep.plantCode)
checkVIN(jeep) //Output = North America VIN: NorthAmericanVinFormat(wmiCode,vehicleAttributes,checkDigit,modelYear,plantCode,sequenceNumber)

val bmw = new Iso3779VinFormat(
  "WmiCode",
  "vds",
  "vis")

println(bmw.vis)
checkVIN(bmw) // Output = ISO VIN: Iso3779VinFormat(WmiCode,vds,vis)

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).