// // program to simulate checkbook, sort of -- maintain list of transactions // (checks and deposits) and current balance, allow some simple queries of // transactions // // each transaction will consist of an amount, "check" or "deposit", // and a line of text to identify it (e.g., "paycheck" or "rent") // var balance = 0 var transactions = List[(Int, String, String)]() val Prompt = """enter one of: c for check d for deposit h for transaction history s to search transactions q to quit""" println("starting balance (integer):") balance = readInt var choice = ' ' do { println("\ncurrent balance $" + balance) println(Prompt) choice = readChar choice match { case 'c' | 'd' => { getTransaction(choice) } case 'h' => { println(transactions.length + " transactions available") println("how many to display?") val count = readInt transactions.take(count).foreach(elem => printTransaction(elem)) } case 's' => { println("text to search for:") val s = readLine transactions.filter(trans => descrMatch(trans, s)). foreach(elem => printTransaction(elem)) } case 'q' => { } case _ => { println("invalid choice") } } } while (choice != 'q') println("ending balance $" + balance) def getTransaction(choice : Char) { println("enter amount (integer)") val amount= readInt println("enter description (line of text)") val descrip = readLine choice match { case 'c' => { balance -= amount transactions = (amount, "check", descrip) :: transactions } case 'd' => { balance += amount transactions = (amount, "deposit", descrip) :: transactions } case _ => { } // FIXME should this be an error? } } def printTransaction(t : (Int, String, String)) { val (amount, kind, descrip) = t println(kind + " $" + amount + " (" + descrip + ")") } def descrMatch(trans : (Int, String, String), searchText : String) : Boolean = { val (a, c, d) = trans d.contains(searchText) }