Skip to content
This repository has been archived by the owner on Jan 8, 2022. It is now read-only.

Add scala runner #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions scala/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
project
2 changes: 2 additions & 0 deletions scala/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ version := "1.0"
scalaVersion := "2.10.2"

libraryDependencies += "org.scalatest" % "scalatest_2.10" % "2.0"

mainClass in (Compile, run) := Some("com.gildedrose.Runner")
43 changes: 43 additions & 0 deletions scala/src/main/scala/com/gildedrose/Runner.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.gildedrose

import java.io.IOException
import java.nio.charset.StandardCharsets
import java.nio.file.{Files, Paths}

import scala.collection.JavaConverters._

object Runner {
def main(args: Array[String]): Unit = {
System.out.println(args(0))
if (args.length < 2) {
System.out.println("Please pass in and out file args")
System.exit(1)
}

val inFile = args(0)
val outFile = args(1)

System.out.println("Reading from: " + inFile + ". Writing to: " + outFile)

val items = Files.lines(Paths.get(inFile)).iterator().asScala
.filter(s => !(s.isEmpty || s.startsWith(";")))
.map(s => {
val Array(name, sellIn, quality) = s.split("__")
new Item(name, sellIn.toInt, quality.toInt)
})

val gildedRose = new GildedRose(items.toArray)
gildedRose.updateQuality()

val outString = gildedRose.items.map(i => Array(i.name, i.sellIn, i.quality).mkString("__")).mkString("\n")

try {
Files.write(Paths.get(outFile), outString.getBytes(StandardCharsets.US_ASCII))
} catch {
case e: IOException => {
System.out.println("Error writing outfile: " + outFile)
System.exit(1)
}
}
}
}