Skip to content

Instantly share code, notes, and snippets.

@aonic
Created February 17, 2011 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aonic/831945 to your computer and use it in GitHub Desktop.
Save aonic/831945 to your computer and use it in GitHub Desktop.
trying out scala
package scalaapplication1
import java.io._
import java.util.Scanner
import javax.swing.JFileChooser
import javax.swing.JOptionPane
object Main {
def main(args: Array[String]): Unit = {
try {
val fc = new JFileChooser()
val status = fc.showOpenDialog(null)
val data = fc.getSelectedFile()
val s = new Scanner(new BufferedReader(new FileReader(data)))
while(s.hasNextLine()) {
var transaction = scanTransaction(s)
transaction match {
case Array("SUMMARY",_,_,_) => scanSummary(s, transaction)
}
}
}
catch {
case e: FileNotFoundException => e.printStackTrace()
case e: Exception => {
if(e.getMessage().startsWith("Summary")) {
JOptionPane.showMessageDialog(null, e.getMessage())
}
}
}
}
def scanSummary(s: Scanner, summary: Array[String]): Unit = {
val expected = scanMoney(summary)
var value = 0.00d
for(i <- 0 to (summary(3).toInt - 1)) {
var transaction = scanTransaction(s)
value += scanMoney(transaction)
if(transaction.length == 4) transaction(0) match {
case "SUMMARY" => scanSummary(s, transaction)
}
}
if (!(Math.abs(value - expected) < 0.00001)) {
throw new Exception("Summary error on %s.\nAmount is $%.2f but should be $%.2f".format(summary(1), expected, value));
}
}
def scanTransaction(s: Scanner): Array[String] = {
val line = s.nextLine().trim()
return line.split("\\s+");
}
def scanMoney(transaction: Array[String]): Double = {
return transaction(2).substring(1).toDouble
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment