Capers Design #
How is your
.capersdirectory structured? What files / classes are you using to represent your repository state, and what information is contained in these files / classes?
.capers. directory will be structured as follows:
.capers ├── dogs │ ├── <dog_name_1> │ ├── <dog_name_2> │ └── ... └── story
dogsfolder will contain serialized
Dogs have unique names, we will use that as the name of the files that each
Dogobject is serialized to.
Dogneeds to know its
int age, its
String breed, and its
String name. These will be stored as fields in the
Dogclass so that they are serialized.
storyfile will contain the current story. This file will contain plain text, because the story is plain text.
What is the process of creating the story, then adding to it? How about creating a dog, then having its birthday? How does your
To create a story, we check if the file
.capers/storyexists. If the file exists, we read the file first using
Utils.readContentsAsString, since the file contains plain text. Otherwise, our story is currently empty. Then, we add the text as a new line to the story, and write the updated story to
When a dog is created with the
dogcommand, we instantiate the
Dogwith the provided arguments. Then, we use the
Dog::saveDoginstance method to serialize the dog. Here, we use the
Utilsto save the
Dogobject to the file
_nameis the dog’s name. We also print out the dog’s
When we have a dog’s birthday, we use the provided name to read the
.capers/dogsdirectory. Then, we update the read dog’s age. Since the age has changed, we need to persist this new age by writing the dog back to the same file we read from (
.capers/dogs/_name). After incrementing the age, we also print out the dog’s
toStringand the birthday celebratory message.
Capers does not have branches, so we skip this question.
Serialization and Persistence #
Creating a dog, having a birthday, creating a dog.
Assume that this is the first time capers has been run.
java capers.Main dog Sammie Samoyed 5
First, we notice that the
.capersfolder doesn’t exist. We create the
Then, we create a
Dogwith the provided info and serialize it. The file that it is serialized to is
.capers/dogs/Sammie, so that we can access this dog in the future and give it birthdays.
java capers.Main birthday Sammie
.capers/dogs/Sammie. We increment Sammie’s age by 1 and print Sammie and congratulations. Then, we serialize the updated age back to