// // program to simulate checkbook, sort of -- maintain list of transactions // (debits and credits) and current balance, allow some simple queries of // transactions // // each transaction will consist of an amount, "debit" or "credit", // and a line of text to identify it (e.g., "paycheck" or "rent") // case class Transaction(amount : Int, kind : String, descrip : String) var balance = 0 var transactions = List[Transaction]() val Prompt = """enter one of: d for debit c for credit 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 'd' | 'c' => { 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 'd' => { balance -= amount transactions = Transaction(amount, "debit", descrip) :: transactions } case 'c' => { balance += amount transactions = Transaction(amount, "credit", descrip) :: transactions } case _ => { } // FIXME should this be an error? } } def printTransaction(t : Transaction) { println(t.kind + " $" + t.amount + " (" + t.descrip + ")") } def descrMatch(trans : Transaction, searchText : String) : Boolean = { trans.descrip.contains(searchText) }