Capers Design #
-
How is your
.capers
directory structured? What files / classes are you using to represent your repository state, and what information is contained in these files / classes?Our
.capers
. directory will be structured as follows:.capers ├── dogs │ ├── <dog_name_1> │ ├── <dog_name_2> │ └── ... └── story
The
dogs
folder will contain serializedDog
objects. SinceDog
s have unique names, we will use that as the name of the files that eachDog
object is serialized to.A
Dog
needs to know itsint age
, itsString breed
, and itsString name
. These will be stored as fields in theDog
class so that they are serialized.The
story
file 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
.capers
directory change?To create a story, we check if the file
.capers/story
exists. If the file exists, we read the file first usingUtils.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.capers/story
.When a dog is created with the
dog
command, we instantiate theDog
with the provided arguments. Then, we use theDog::saveDog
instance method to serialize the dog. Here, we use theUtils
to save theDog
object to the file.capers/dogs/_name
, where_name
is the dog’s name. We also print out the dog’stoString
.When we have a dog’s birthday, we use the provided name to read the
Dog
from the.capers/dogs
directory. 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’stoString
and 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
.capers
folder doesn’t exist. We create the.capers
and.capers/dogs
directories.Then, we create a
Dog
with 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
Main
reads theDog
Sammie from.capers/dogs/Sammie
. We increment Sammie’s age by 1 and print Sammie and congratulations. Then, we serialize the updated age back to.capers/dogs/Sammie
.