As a quick reminder, in the following lessons where we’ll create database-related code, all of these functions will be placed inside a Database class that looks like this:
class Database(dbFilename: String):
// def insert = ???
// def selectAll = ???
// def delete = ???
end Database
The first Database function we’ll need is an ‘insert’ function. Because this is just a flat-file database, this function will append a single new record to the database file. Therefore, I know that it takes a string as input, and I start to sketch the insert function like this:
def insert(record: String)
Next — and mostly because I’ve been through this process before — I know that I want this function to use another ‘writeToFile’ function to do the actual file-writing. I know this because the delete and update functions I’ll write in the following lessons will require me to read in the entire file and then re-write it completely. So writeToFile will need to be able to both (a) append to a file and (b) completely overwrite the file (and I don’t want to have a boolean append parameter in the insert function).
Therefore, let’s forget about the insert function for a moment and first work on the writeToFile function.