diff --git a/scala/.gitignore b/scala/.gitignore new file mode 100644 index 0000000000..e3f094ee63 --- /dev/null +++ b/scala/.gitignore @@ -0,0 +1 @@ +project \ No newline at end of file diff --git a/scala/build.sbt b/scala/build.sbt index c9cc5d5987..0fe8784916 100644 --- a/scala/build.sbt +++ b/scala/build.sbt @@ -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") diff --git a/scala/src/main/scala/com/gildedrose/Runner.scala b/scala/src/main/scala/com/gildedrose/Runner.scala new file mode 100644 index 0000000000..acb3f60a20 --- /dev/null +++ b/scala/src/main/scala/com/gildedrose/Runner.scala @@ -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) + } + } + } +}