Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 7.14 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package travelPlanner;
  2.  
  3.  
  4.  
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7.  
  8. /**
  9.  * This class read and save different kind of destinationInfo
  10.  *
  11.  * @author Ragnhild Karlsson & Joakim Candefors
  12.  *
  13.  */
  14. public class DestinationReaderWriter {
  15.         private final String destinationTitle;
  16.         private final String username;
  17.         private final String travelProjectName;
  18.  
  19.        
  20.         public DestinationReaderWriter(String destinationTitle, String username,
  21.                         String travelProjectName) {
  22.                 this.destinationTitle = destinationTitle;
  23.                 this.username = username;
  24.                 this.travelProjectName = travelProjectName;                                     //to be implemented
  25.  
  26.         }
  27.  
  28.        
  29.         /**
  30.          * Return an ArrayList with DestinationHeadlines that represents the headlines that existed when the
  31.          * destination was created
  32.          */
  33.         public ArrayList<DestinationHeadline> getExistingDataTypes(){
  34.          
  35.           ArrayList<DestinationHeadline> existingDataTypes= new ArrayList<DestinationHeadline>();
  36.          
  37.           try{
  38.                 existingDataTypes = (ArrayList<DestinationHeadline>) ObjectIO.loadObject(username+"/"+destinationTitle , "existingDataTypes");
  39.                 }
  40.                 catch (ClassNotFoundException e){
  41.                         ErrorHandler.printError(e, this.getClass().toString());
  42.                         return null;
  43.                 }catch (IOException ex){
  44.                         ErrorHandler.printError(ex, this.getClass().toString());
  45.                         return null;
  46.                 }
  47.    
  48.                    return existingDataTypes;
  49.    
  50.         }
  51.          
  52.         /**
  53.          * Save an ArrayList with DestinationHeadlines that represents the headlines that existed when the
  54.          * destination was created
  55.          */
  56.         public void saveExistingDataTypes(ArrayList<DestinationHeadline> existingDataTypes){
  57.          
  58.           try{
  59.                 ObjectIO.saveObject(existingDataTypes, username + "/" + destinationTitle, "existingDataTypes");
  60.                 }
  61.           catch (IOException e){
  62.                         ErrorHandler.printError(e, this.getClass().toString());
  63.                 }
  64.                
  65.                
  66.         }
  67.        
  68.         /**
  69.          * Return a title corresponding to the specified destination headline
  70.          */
  71.      
  72.         public String getTitle(DestinationHeadline headline){
  73.                 String filename = getFileName(headline);
  74.                 String title;
  75.                 try{
  76.                  title = (String) ObjectIO.loadObject(username+"/"+destinationTitle, filename + "_title");
  77.                 }catch (ClassNotFoundException e){
  78.                         ErrorHandler.printError(e, this.getClass().toString());
  79.                         return null;
  80.                 }catch (IOException ex){
  81.                         ErrorHandler.printError(ex, this.getClass().toString());
  82.                         return null;
  83.                 }
  84.    
  85.                    return title;
  86.        
  87.         }
  88.        
  89.         /**
  90.          * Save a title corresponding to the specified destination headline
  91.          */
  92.        
  93.         public void saveTitle(DestinationHeadline headline, String title){
  94.                 String filename = getFileName(headline);        
  95.                 try{
  96.                 ObjectIO.saveObject(title, username + "/" + destinationTitle, filename + "_title");
  97.                 }catch (IOException e){
  98.                         ErrorHandler.printError(e, this.getClass().toString());
  99.                 }
  100.            
  101.         }
  102.        
  103.         /**
  104.          * Return an arrayList of String representing the subheadlines of the secified DestinationHeadline
  105.          */
  106.        
  107.         public ArrayList<String> getShortDestinationInformationHeadlines(
  108.                         DestinationHeadline headline) {
  109.                 String filename = getFileName(headline);
  110.                 ArrayList<String> shortDestinationData;
  111.                 try{
  112.                  shortDestinationData = (ArrayList<String>) ObjectIO.loadObject(username+"/"+destinationTitle, filename + "_headline");
  113.                 }catch (ClassNotFoundException e){
  114.                         ErrorHandler.printError(e, this.getClass().toString());
  115.                         return null;
  116.                 }catch (IOException ex){
  117.                         ErrorHandler.printError(ex, this.getClass().toString());
  118.                         return null;
  119.                 }
  120.  
  121.                 return shortDestinationData;
  122.         }
  123.        
  124.         /**
  125.          * Save an ArrayList of Strings representing the subheadlines for short Information about the
  126.          * specified DestinationHeadline
  127.          */
  128.         public void saveShortDestinationInformationHeadlines(ArrayList<String> headlinedata, DestinationHeadline headline){
  129.                 String filename = getFileName(headline);        
  130.                 try{
  131.                 ObjectIO.saveObject(headlinedata, username + "/" + destinationTitle, filename+"_headline");
  132.                 }catch (IOException e){
  133.                         ErrorHandler.printError(e, this.getClass().toString());
  134.                 }
  135.                
  136.         }
  137.  
  138.        
  139.         /**
  140.          * Return an ArrayList with strings representing saved short information
  141.          * data associated with the specified DestinationHeadline. For exampel
  142.          * ARRIVAL will return a Array with strings representing the saved data
  143.          * associated whith the headlines: Date, Arrivaltime, Station/Airport,
  144.          * Booking number The array will match the respondning headlineArray by
  145.          * order
  146.          *
  147.          * @param headline
  148.          * @return
  149.          */
  150.  
  151.         public ArrayList<String> getShortDestinationInformationData( DestinationHeadline headline) {
  152.                
  153.                 String filename = getFileName(headline);        
  154.                 ArrayList<String> shortDestinationData;
  155.                 try{
  156.                  shortDestinationData = (ArrayList<String>) ObjectIO.loadObject(username+"/"+destinationTitle, filename);
  157.                 }catch (ClassNotFoundException e){
  158.                         ErrorHandler.printError(e, this.getClass().toString());
  159.                         return null;
  160.                 }catch (IOException ex){
  161.                         ErrorHandler.printError(ex, this.getClass().toString());
  162.                         return null;
  163.                 }
  164.  
  165.                 return shortDestinationData;
  166.         }
  167.  
  168.         /**
  169.          * Save an ArrayList with strings representing saved short information
  170.          * data associated with the specified DestinationHeadline.
  171.          * @param shortInformationDestinationdata
  172.          * @param headline
  173.          * @return
  174.          */
  175.  
  176.        
  177.         public void saveShortDestinationInformationData(ArrayList<String> data, DestinationHeadline headline) {
  178.                
  179.                 String filename = getFileName(headline);
  180.                 try{
  181.                 ObjectIO.saveObject(data, username + "/" + destinationTitle, filename);
  182.                 }catch (IOException e){
  183.                         ErrorHandler.printError(e, this.getClass().toString());
  184.                 }
  185.  
  186.                
  187.         }
  188.        
  189.         private String getFileName(DestinationHeadline headline){
  190.                 String filename = null;
  191.                
  192.                 switch (headline){
  193.                 case ARRIVAL: filename = "Arrival";
  194.                 break;
  195.                 case DEPARTURE : filename = "Departure";
  196.                 break;
  197.                 case LIVING : filename = "Living";
  198.                 }
  199.                
  200.                 return filename;
  201.                
  202.         }
  203.        
  204.        
  205.  
  206. }