Advertisement
yo2man

TreeHouse: Implementing the Data Model in Build an Interacti

Jun 30th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.31 KB | None | 0 0
  1. // TreeHouse: Implementing the Data Model in Build an Interactive Story App
  2.  
  3. // 1st, create some packages to keep things organized:
  4. // Create new subpackage. RightClickonPackage>NewPackage> name it UI
  5. // RightClickonPackage>NewPackage> name it model
  6.  
  7.  
  8.  
  9. //-------------------------------------------------------------------------------------------------------------------------------------
  10. Page.java
  11. package teamtreehouse.com.interactivestory.model;
  12.  
  13.  
  14. public class Page {
  15. // We said we would have an image, some story text, and two button choices.
  16.  
  17.     private int mImageId *1*; // Image: We're gonna store our images in our drawable directories, and we will be able to reference them by their ids which are ints.
  18.  
  19.     private String mText *2*; // Story Text
  20.  
  21.     // Remember that we said for simplicity, each page will contain two Choices.
  22.     private Choice mChoice1 *3*;
  23.     private Choice mChoice2 *4*;
  24.  
  25.     // a boolean flag to let us know we are on our last steps**
  26.     private boolean mIsFinal = false;
  27.  
  28.  
  29.     // Custom Constructor: *
  30.     //  Why use custom constructor? https://teamtreehouse.com/forum/i-am-completely-confused-with-this-customer-constructor-concepts-in-this-current-tutorial
  31.     public Page(int imageId *1*, String text *2*, Choice choice1 *3*, Choice choice2 *4*) {      // a constructor declaration looks like a method declaration—except that they use the name of the class and has no return type.
  32.         mImageId = imageId; *1*
  33.         mText = text; *2*
  34.         mChoice1 = choice1; *3*
  35.         mChoice2 = choice2; *4*
  36.     }
  37.  
  38.     // Page 5 and Page 6 end up now where because they are at the end of the story.
  39.     // So we remove the choices and add create a new constructor of null choices.
  40.     // Simple:
  41.     public Page(int imageId, String text) {
  42.         mImageId = imageId;
  43.         mText = text;
  44.         mChoice1 = null;
  45.         mChoice2 = null;
  46.         mIsFinal = true; **
  47.  
  48.     }
  49.  
  50.     //Getter for Boolean Final **
  51.     public boolean isFinal() {
  52.         return mIsFinal;
  53.     }
  54.  
  55.     //Setter for Boolean Final **
  56.     public void setFinal(boolean isFinal) {
  57.         mIsFinal = isFinal;
  58.     }
  59.  
  60.  
  61. // Okay, so we have all the data we need in private member variables, but
  62. // we don't have any way to get and set this data.
  63. // We need Getter and Setter methods.
  64. // Now, you may be aware of this pattern from Java, but, if not, we add a pair of
  65. // methods for each member variable, one to get it, and one to set it.
  66.  
  67. // Now this work about getters and setters may seem tedious or overkill.
  68. // The thing to remember is that it's a good practice that lets us control the details
  69. // behind the scenes of these objects,
  70. // which can prove invaluable when making future changes.
  71.  
  72.  
  73.     // So let's do that first for image id:
  74.     public int getImageId() {
  75.         return mImageId;
  76.     }
  77.    
  78.     // for Text method
  79.     public String getText() {
  80.         return mText;
  81.     }
  82.    
  83.     // for Choice1
  84.     public Choice getChoice1() {
  85.         return mChoice1;
  86.     }
  87.  
  88.     // for Choice 2
  89.     public Choice getChoice2() {
  90.         return mChoice2;
  91.     }
  92. }
  93.  
  94. //-------------------------------------------------------------------------------------------------------------------------------------
  95.  
  96. // There are different ways we can represent this choice, but let's add another class.
  97. Choice.java
  98.  
  99.  
  100. package teamtreehouse.com.interactivestory.model;
  101.  
  102. // Custom Constructor *
  103. public class Choice {
  104.     private String mText; // Text that describe the choice
  105.     private int mNextPage; // We're going to use the index of the pages in the array to link one to another.
  106.  
  107.     public Choice(String text, int nextPage) {
  108.         mText = text;
  109.         mNextPage = nextPage;
  110.     }
  111.  
  112.     // getter
  113.     public String getText() {
  114.         return mText;
  115.     }
  116.  
  117.     // setter
  118.     public void setText(String text) {
  119.         mText = text;
  120.     }
  121.  
  122.     public int getNextPage() {
  123.         return mNextPage;
  124.     }
  125.  
  126.     public void setNextPage(int nextPage) {
  127.         mNextPage = nextPage;
  128.     }
  129. }
  130. //------------------------------------------------------------------------------------------------------------------------------------
  131. // 2nd, create Arrays so you can pull pages out of arrays (much cleaner):
  132.  
  133.  
  134.  
  135. package teamtreehouse.com.interactivestory.model;
  136.  
  137.     // Okay, anyhow, we need to now add our story data to this array.
  138.     // Hm, what should we do here?
  139.     // How about we initialize our array, mPages, in onCreate.
  140.     // So let's add a couple lines.
  141.     // And say mPages equals a new array of Pages and we'll give it a size of seven.
  142.     // We have seven story pages in this version of the story.
  143.     mPages = new Page[7];
  144.  
  145. public class Story {
  146.     private Page[] mPages;
  147.    
  148.    
  149.     public Story() {                //method to initialize our story:
  150.         mPages = new Page[7];
  151.  
  152.  
  153.         mPages[0] = new Page(
  154.         //Wondering why this is formatted this way? Because we used our own Custom Constructor *
  155.                 R.drawable.page0,
  156.                 "On your return trip from studying Saturn's rings, you hear a distress signal that seems to be coming from the surface of Mars. It's strange because there hasn't been a colony there in years. Even stranger, it's calling you by name: \"Help me, %1$s, you're my only hope.\"",
  157.                 new Choice("Stop and investigate", 1),
  158.                 new Choice("Continue home to Earth", 2));
  159.  
  160.         mPages[1] = new Page(
  161.                 R.drawable.page1,
  162.                 "You deftly land your ship near where the distress signal originated. You didn't notice anything strange on your fly-by, but there is a cave in front of you. Behind you is an abandoned rover from the early 21st century.",
  163.                 new Choice("Explore the cave", 3),
  164.                 new Choice("Explore the rover", 4));
  165.  
  166.         mPages[2] = new Page(
  167.                 R.drawable.page2,
  168.                 "You continue your course to Earth. Two days later, you receive a transmission from HQ saying that they have detected some sort of anomaly on the surface of Mars near an abandoned rover. They ask you to investigate, but ultimately the decision is yours because your mission has already run much longer than planned and supplies are low.",
  169.                 new Choice("Head back to Mars to investigate", 4),
  170.                 new Choice("Continue home to Earth", 6));
  171.  
  172.         mPages[3] = new Page(
  173.                 R.drawable.page3,
  174.                 "Your EVA suit is equipped with a headlamp, which you use to navigate the cave. After searching for a while your oxygen levels are starting to get pretty low. You know you should go refill your tank, but there's a very faint light up ahead.",
  175.                 new Choice("Refill at ship and explore the rover", 4),
  176.                 new Choice("Continue towards the faint light", 5));
  177.  
  178.         mPages[4] = new Page(
  179.                 R.drawable.page4,
  180.                 "The rover is covered in dust and most of the solar panels are broken. But you are quite surprised to see the on-board system booted up and running. In fact, there is a message on the screen: \"%1$s, come to 28.543436, -81.369031.\" Those coordinates aren't far, but you don't know if your oxygen will last there and back.",
  181.                 new Choice("Explore the coordinates", 5),
  182.                 new Choice("Return to Earth", 6));
  183.  
  184.         mPages[5] = new Page(
  185.                 R.drawable.page5,
  186.                 "After a long walk slightly uphill, you end up at the top of a small crater. You look around, and are overjoyed to see your favorite android, %1$s-S1124. It had been lost on a previous mission to Mars! You take it back to your ship and fly back to Earth.");
  187.  
  188.         mPages[6] = new Page(
  189.                 R.drawable.page6,
  190.                 "You arrive home on Earth. While your mission was a success, you forever wonder what was sending that signal. Perhaps a future mission will be able to investigate...");
  191.     }
  192.  
  193.     public Page getPage(int pageNumber) {
  194.         return mPages[pageNumber];
  195.     }
  196. }
  197.  
  198.  
  199.  
  200. //---------------------------------------------------------------------------------------------------------------------------------
  201.  
  202. // Constructors explained:
  203. // http://stackoverflow.com/questions/19061599/methods-vs-constructors-in-java
  204. // https://teamtreehouse.com/library/build-an-interactive-story-app/the-modelviewcontroller-pattern/implementing-the-data-model
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement