// // 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("current balance $" + balance) println(Prompt) choice = readChar choice match { case 'c' => { getTransaction('c') } case 'd' => { getTransaction('d') } case 'h' => { // FIXME improve this transactions.foreach(elem => println(elem)) } case 's' => { println("text to search for:") val s = readLine transactions.filter(trans => descrMatch(trans, s)). foreach(elem => println(elem)) } case 'q' => { } case _ => { println("invalid choice") } } } while (choice != 'q') def descrMatch(trans : (Int, String, String), searchText : String) : Boolean = { val (a, c, d) = trans d.contains(searchText) } def getTransaction(choice : Char) { println("enter amount") val amount = readInt println("enter description") val d = readLine choice match { case 'c' => { balance -= amount transactions = (amount, "check", d) :: transactions } case 'd' => { balance += amount transactions = (amount, "deposit", d) :: transactions } case _ => { } // FIXME should this be an error? } }