Chapter.java :  » UnTagged » seniordesignp8 » com » project8 » book » Android Open Source

Android Open Source » UnTagged » seniordesignp8 
seniordesignp8 » com » project8 » book » Chapter.java
package com.project8.book;

import java.io.Serializable;
import java.util.ArrayList;

/** A Chapter is essentially a collection of Pages -- There is some redundancy with Book class */
public class Chapter implements Serializable {

  
    private static final long serialVersionUID = 3368434766417086258L;

    public static long chapter_id = 0;
    
    long id;
    long chapterNum = 0;    //Not every chapter requires a number, default = 0
    String chapterName;
    Page chapterStart;
    Page chapterEnd;
    
    ArrayList<Page> pagesInChapter = new ArrayList<Page>();
    
    public Chapter(String chName)
    {
      id = chapter_id++;
      
      chapterName = chName;
    }
    
    public Chapter(String chName, Page beginning, Page end)
    {
      id = chapter_id++;
      
      chapterName = chName;
      chapterStart = beginning;
      chapterEnd = end;
    }
    
    public Chapter(long chNum, String chName, Page beginning, Page end)
    {
      id = chapter_id++;
      
      chapterName = chName;
      chapterStart = beginning;
      chapterEnd = end;
      chapterNum = chNum;
    }
    
    public void setNum(long chNum)
    {
      chapterNum = chNum;
    }
    
    public long getId()
    {
      return id;
    }
    
    public String getName()
    {
      return chapterName;
    }
    
    public long getNum()
    {
      return chapterNum;
    }
    
    public Page getFirstPage()
    {
      return chapterStart;
    }
    
    public Page getlastPage()
    {
      return chapterEnd;
    }
    
    /** Add a new Page to this Chapter */
    public void addOnePage(Page newPage)
    {
      newPage.setChapterName(this.chapterName);
      newPage.setChapterNum(chapterNum);
      
      pagesInChapter.add(newPage);
            
      sortPages();
    }
    
    /** Add a collection of new Pages to this Chapter */
    public void addMultiplePages(ArrayList<Page> newPages)
    {
      for(int i = 0; i < newPages.size(); i++)
      {
        newPages.get(i).setChapterName(this.chapterName);
        newPages.get(i).setChapterNum(this.chapterNum);
        
        pagesInChapter.add((Page)newPages.get(i));
      }
      
      sortPages();
    }
    
    /** Remove a page from this Chapter -- There shouldn't ever be a need to call this method */
    public void removeOnePage(Page pageToRemove)
    {
      if(pagesInChapter.contains(pageToRemove))
      {
        int index = pagesInChapter.indexOf(pageToRemove);
        pagesInChapter.remove(index);
      }
    }
    
    /** Sorts the Pages in this Chapter so that they are always in order of increasing page number */
    private void sortPages()
    {
      for(int i = 0; i < pagesInChapter.size() - 1; i++)
        for(int j = i; j < pagesInChapter.size(); j++)
        {
          if(((Page)pagesInChapter.get(j)).getPageNum() < ((Page)pagesInChapter.get(i)).getPageNum())
          {
            Page temp = (Page)pagesInChapter.get(i);
            pagesInChapter.set(i, (Page)pagesInChapter.get(j));
            pagesInChapter.set(j, temp);
          }
        }
    }
    
    public ArrayList<Page> getPages()
    {
      return pagesInChapter;
    }
    
    public long numPages()
    {
      return pagesInChapter.size();
    }
    
    @Override
    public String toString()
    {
      return chapterName;
    }
    
    @Override
    public boolean equals(Object o)
    {
      Chapter anotherChapter = (Chapter)o;
      
      if(this.getName().equals(anotherChapter.getName()) && this.getId() == anotherChapter.getId())
        return true;
            
      return false;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.