scg/ch12/AdventurePage

From FANG

Jump to: navigation, search

001 package scg.ch12;
002 
003 import java.io.PrintWriter;
004 import java.util.Scanner;
005 
006 public abstract class AdventurePage {
007   /** index value used to indicate that there is no matching page */
008   public static final int NO_SUCH_PAGE = -1;
009 
010   /**
011    * The format of the input file (as far as this method is concerned)
012    * is the name of a page type (subclass of {@link AdventurePage})
013    * followed by the data for the content of that object. This method
014    * reads the line (containing "Answer" or "Question") and constructs
015    * the appropriate type of page, passing in a reference to this book
016    * and the scanner; the page constructor is responsible for consuming
017    * all necessary information and leaving the scanner set to read the
018    * next class naming line.
019    *
020    @param   book     the {@link AdventureBook} to which the new page
021    *                   belongs
022    @param   scanner  input {@link Scanner} attached to a proper input
023    *                   stream
024    *
025    @return  new page ({@link Question} or {@link Answer})
026    */
027   public static AdventurePage readPageFromFile(AdventureBook book,
028     Scanner scanner{
029     String questionOrAnswer = scanner.nextLine();
030     if (questionOrAnswer.equals("Question")) {
031       return new Question(book, scanner);
032     } else {
033       return new Answer(book, scanner);
034     }
035   }
036 
037   /**
038    * Parallel with {@link #readPageFromFile(AdventureBook, Scanner)},
039    this method writes a given page out to the open text output file.
040    *
041    @param   page  the page to write
042    @param   out   the {@link PrintWriter} to which the page should be
043    *                saved
044    */
045   public static void writePageToFile(AdventurePage page,
046     PrintWriter out{
047     page.save(out);
048   }
049 
050   /** the book to which this page belongs */
051   private final AdventureBook book;
052 
053   /** the index of the parent node */
054   private int parentNdx;
055 
056   /** this node's text value */
057   private final String text;
058 
059   /**
060    * Construct a new {@link AdventurePage}. The page will have the given
061    * text as its content and will be added to the given {@link
062    * AdventureBook}.
063    *
064    @param  book  the {@link AdventureBook} to which this page should
065    *               belong
066    @param  text  the page contents
067    */
068   protected AdventurePage(AdventureBook book, String text,
069     int parentNdx{
070     this.book = book;
071     book.add(this);
072     this.text = text;
073     setParentNdx(parentNdx);
074   }
075 
076   /**
077    * Get the page number of this page in the book to which it belongs.
078    *
079    @return  the 0-based page number (index) of this page
080    */
081   public int getNdx() {
082     return book.indexOf(this);
083   }
084 
085   /**
086    * Return the current text value for this node
087    *
088    @return  current text value
089    */
090   public String getText() {
091     return text;
092   }
093 
094   /**
095    * Play this page. A page is played by presenting some text to the
096    * user (through standard output) and accept some input from the user
097    * (through standard input). This method _must_ be provided by all
098    * subclasses of this class. It is provided here solely to describe
099    * the public interface for users of {@link AdventurePage} objects.
100    *
101    @return  true if the computer won the game, false otherwise.
102    */
103   public abstract boolean play();
104 
105   /**
106    * Get the book to which this page belongs. This is protected because
107    * the book is only of interest to this object and the pages which
108    * extend it.
109    *
110    @return  the {@link AdventureBook} to which this page belongs
111    */
112   protected AdventureBook getBook() {
113     return book;
114   }
115 
116   /**
117    * The parent of this node must be a {@link Question} (if it exists).
118    * Thus the cast here is safe.
119    *
120    @return  reference to the {@link Question} which leads to this page
121    *          (if there is one); null if the node is the root of the
122    *          tree
123    */
124   protected Question getParent() {
125     if (getParentNdx() != NO_SUCH_PAGE{
126       return (QuestiongetBook().get(getParentNdx());
127     } else {
128       return null;
129     }
130   }
131 
132   /**
133    * Get a reference to the {@link Question} (if any) for which this
134    * question is an answer. Implementation detail must be visible to all
135    {@link AdventureBook} types.
136    *
137    @return  reference to {@link Question} (if there is one) of which
138    *          this {@link AdventureBook} is a child.
139    */
140   protected int getParentNdx() {
141     return parentNdx;
142   }
143 
144   /**
145    * Write the {@link AdventurePage} to the given text file so that it
146    * can be read back in by {@link #readPageFromFile(AdventureBook,
147    Scanner)}. Overriding methods _must_ call this method before
148    * writing anything to the text file.
149    *
150    @param  out  a {@link PrintWriter}, opened and attached to an
151    *              output text file. Where to save this page.
152    */
153   protected void save(PrintWriter out{
154     out.println(getClass().getName());
155     out.println(getText());
156     out.println(getParentNdx());
157   }
158 
159   /**
160    Set the value of the parent of the current {@link AdventureBook}.
161    * Implementation detail must be visible to all {@link AdventureBook}
162    * types.
163    *
164    @param  parent  reference to the {@link Question} of which this
165    *                 {@link AdventureBook} is a child.
166    */
167   protected void setParentNdx(int parentNdx{
168     this.parentNdx = parentNdx;
169   }
170 }
171 
172 //Uploaded on Mon Mar 29 21:39:45 EDT 2010


Download/View scg/ch12/AdventurePage.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter

Games
Games