Tags | FileIO |
You are required to create a back-end service that will help capture basic information about prospective students who come to inquire here at Umuzi. In this project you’ll just be storing and retrieving information from plain old json files.
Visitor
. Instances of this class should have the following properties:save
that saves the visitor’s data to a JSON file. The file name should be named like this, visitor_{their_full_name}.json
.alice.save() # results in visitor_alice_cooper.json
bob.save() # results in visitor_bob_marley.json
charlie.save() # results in visitor_charley_sheen.json
Notice that the full name used in the file is all lower-case and spaces are replaced by underscores.
load
that takes in a name and then grabs a Visitor object from the file. It should return a Visitor instance.For example:
Visitor.load("Alice Cooper")
# returns a Visitor instance
Visitor.load("Bob Marley")
# Same deal for good ol' Bob
Take note that the load
function should be attached to the Visitor
class, load
is not a standalone function.
Here are some upgrades you can add to your project if you are up for it.
alice.save() # results in visitor_1.json
bob.save() # results in visitor_2.json
charlie.save() # results in visitor_3.json
alice.comments = "Kinda weird, I don't think he'll fit in"
alice.save() # results in an UPDATE to visitor_1.json
Your load method should also get a bit of an update.
charlie = Visitor.load(3)
charlie.comments = "Winning!"
charlie.save() # results in an UPDATE to visitor_3.json
Imagine that the code produced by the learner needs to be maintained and some parts of the code will have to change over time. Ideally, changes to the code should be made just once, and that change should affect everything else without going through the file(s) looking to change the same thing.