Project: Calendar-Application
package calendar;
 
/**
* @author Adam Barrell 
* @date 23/02/2012 
* @brief 
* This class creates an object of type Setting which holds 
* attributes about each type of setting in the calendar. 
* Each setting has it's own name, value and type. Setting 
* objects are written to CSV for persistence. 
*/
 
public class Setting extends CsvStructure{ 
    public static final int TYPE_STRING = 0
    public static final int TYPE_INT = 1
    public static final int TYPE_FLOAT = 2
     
    public static final int NO_FIELDS = 3
    public static final int IDX_NAME = 0
    public static final int IDX_VALUE = 1
    public static final int IDX_TYPE = 2
     
    private String m_name; 
    private String m_value; 
    private int m_type; 
     
    /* Accessor and Mutator methods */ 
     
    /**
     * Gets the global variable of setting name. 
     * @return Returns setting name as a String. 
     */
 
    public String GetName() { 
     if (m_name.length() > 30) { 
      throw new StringIndexOutOfBoundsException("The length of " + 
        "setting name "+m_name+ 
        " is greater than 30 characters, " + 
        "please edit the settings.csv file to fix this!"); 
     
        return m_name; 
    } 
 
    /**
    * Sets the global variable of setting name. 
    * @param name A string to identify setting. 
    */
 
    public boolean SetName(String name) { 
     if (name.length() > 30) { 
      throw new StringIndexOutOfBoundsException("The length of " + 
        "setting name "+name+ 
        " is greater than 30 characters"); 
     
        m_name = name; 
        return true
    } 
 
    /**
    * Gets the type of the setting. 
    * @return Returns an integer representing the type 
    * of setting. 
    * @throws NoSuchFieldException Checks value of type is valid 
    */
 
    public int GetType() throws NoSuchFieldException { 
     if ((m_type != Setting.TYPE_STRING) && (m_type != Setting.TYPE_INT) 
       && (m_type != Setting.TYPE_FLOAT)) { 
      throw new NoSuchFieldException("Setting type "+m_type+" does " + 
        "not exist!"); 
     }  
     return m_type;          
    } 
 
    /**
    * Sets the type of the setting. 
    * @param type An integer representing the type of 
    * setting. 
    */
 
    public boolean SetType(int type) { 
     if ((type != Setting.TYPE_STRING) && (type != Setting.TYPE_INT) 
       && (type != Setting.TYPE_FLOAT)) { 
      throw new IllegalArgumentException("Type "+type+" is not a " + 
        "valid type!"); 
     
        m_type = type; 
        return true
    } 
 
    /**
    * Gets the value of the setting. 
    * @return Returns the value of setting as a String. 
    */
 
    public String GetValue() { 
     if (m_value.length() > 30) { 
      throw new StringIndexOutOfBoundsException("The length of " + 
        "setting value "+m_value+ 
        " is greater than 30 characters, " + 
        "please edit the settings.csv file to fix this!"); 
     
        return m_value; 
    } 
     
    /**
    * Checks the setting type is of type integer. If setting is invalid throw  
    * an exception. 
    * @return Parses and returns whatever value is stored as m_value into an  
    * integer. 
    */
 
    public int GetValueInt() { 
        if(m_type != Setting.TYPE_INT) { 
            throw new IllegalArgumentException("Setting "+m_name+ 
              " is not of type INT"); 
        } 
        return new Integer(m_value).intValue(); 
    } 
     
    /**
    * Checks the setting type is of type float. If setting is invalid throw  
    * an exception. 
    * @return Parses and returns whatever value is stored as m_value into a  
    * double. 
    */
 
    public double GetValueFloat() { 
        if(m_type != Setting.TYPE_FLOAT) { 
            throw new IllegalArgumentException("Setting "+m_name+ 
              " is not of type FLOAT(double)"); 
        } 
        return new Double(m_value).doubleValue(); 
    } 
 
    /**
    * Sets the value of the setting to a value of type String. 
    * @param value A value for the setting of type String. 
    */
 
    public boolean SetValue(String value) { 
     if (value.length() > 30) { 
      throw new StringIndexOutOfBoundsException("The length of " + 
        "setting value "+value+ 
        " is greater than 30 characters"); 
     
     if (m_type != Setting.TYPE_STRING) { 
       throw new IllegalArgumentException("Setting "+m_name+ 
               " is not of type String"); 
     
        m_value = value; 
        return true
    } 
     
    /**
    * Sets the value to a value of type String if an integer parameter has been 
    * passed in. 
    * @param value An integer value for the setting value. 
    */
 
    public boolean SetValue(int value) { 
        if(m_type != Setting.TYPE_INT) { 
            throw new IllegalArgumentException("Setting "+m_name+ 
              " is not of type INT"); 
        } 
        m_value = new Integer(value).toString(); 
        return true
    } 
     
    /**
    * Sets the value to a value of type String if a double parameter has  
    * been passed in. 
    * @param value A double value for the setting value. 
    */
 
    public boolean SetValue(double value) { 
        if(m_type != Setting.TYPE_FLOAT) { 
            throw new IllegalArgumentException("Setting "+m_name+ 
              " is not of type FLOAT(double)"); 
        } 
        m_value = new Double(value).toString(); 
        return true
    } 
     
    /**
     * Dummy constructor - for testing. Creates a default setting with the  
     * name "setting" and the value "default" 
     */
 
    public Setting() { 
        m_name = "setting"
        m_value = "default"
        m_type = Setting.TYPE_STRING; 
    } 
     
   /**
    * Creates a new Setting given the type 
    * @param name The name (identifier) of the setting. 
    * @param value The value for the setting. 
    * @param type The type Setting.TYPE_INT, Setting.TYPE_STRING or  
    * Setting.TYPE_FLOAT. 
    */
 
    public Setting(String name, String value, int type) { 
        m_name = name; 
        m_value = value; 
        m_type = type; 
    } 
     
    /**
     * Creates a new Setting with predefined String Type 
     * @param name The name (identifier) of the setting 
     * @param value The value 
     */
 
    public Setting(String name, String value) { 
        m_name = name; 
        m_value = value; 
        m_type = Setting.TYPE_STRING; 
    } 
     
    /**
    * Creates a new Setting with predefined INT Type. 
    * Parses INT as a String to store as setting value. 
    * @param name The name (identifier) of the setting. 
    * @param value The value for the setting. 
    */
 
    public Setting(String name, int value) { 
        m_name = name; 
        m_value = new Integer(value).toString(); 
        m_type = Setting.TYPE_INT; 
    } 
     
   /**
    * Creates a new Setting with predefined double Type. 
    * Parses double as a String to store as setting value. 
    * @param name The name (identifier) of the setting. 
    * @param value The value for the setting. 
    */
 
    public Setting(String name, double value) { 
        m_name = name; 
        m_value = new Double(value).toString(); 
        m_type = Setting.TYPE_FLOAT; 
    } 
 
   /**
    * Computes the Setting as a row in the CSV file 
    * @return The CSV line of the input, as a String 
    */
 
    @Override 
    public String ToCSV() { 
        String csv = "\""+m_name+"\",\""+m_value+"\","+m_type; 
        return csv; 
    } 
     
     
     
    public static void main(String args[]) { 
      
     Setting settingDefault = new Setting(); 
      
     /* Test SetName() (regular input + String overflow) */ 
     try { 
      settingDefault.SetName("Test Setting"); 
      settingDefault.SetName("overflowoverflowoverflowoverflowoverflow"); 
      System.out.println("Fail: String overflow allowed on field Name"); 
     catch (StringIndexOutOfBoundsException e) { 
      System.out.println("Pass: " +e.getMessage()); 
     
      
     /* Test GetName() */ 
     try { 
      settingDefault.SetName("ValidStringLength"); 
      settingDefault.GetName(); 
      System.out.println("Pass: Returned valid String length"); 
     catch (StringIndexOutOfBoundsException e) { 
      System.out.println("Fail: "+e); 
     
      
     /* Test GetType */ 
     try { 
      settingDefault.SetType(TYPE_STRING); 
      settingDefault.GetType(); 
      System.out.println("Pass: Valid type has been set"); 
     catch (NoSuchFieldException e) { 
      System.out.println("Fail: "+e.getMessage()); 
     
      
     /* Test getting a float value from a value type attribute set to INT */ 
     try { 
      settingDefault.SetType(TYPE_INT); 
      settingDefault.SetValue(55); 
      settingDefault.GetValueFloat(); 
      System.out.println("Fail: Value should not be of type float."); 
     catch (Exception e) { 
      System.out.println("Pass: "+e.getMessage()); 
     
      
     /* Test getting an INT value from a value type attribute set to float */ 
     try { 
      settingDefault.SetType(TYPE_FLOAT); 
      settingDefault.SetValue(55.5); 
      settingDefault.GetValueInt(); 
      System.out.println("Fail: Value should not be of type int."); 
     catch (Exception e) { 
      System.out.println("Pass: "+e.getMessage()); 
     
      
     /* Test SetValue(String) */ 
     //Overflow String 
     try { 
      settingDefault.SetType(TYPE_STRING); 
      settingDefault.SetValue("overflowoverflowoverflowoverflowoverflow"); 
      System.out.println("Fail: Value was allowed to be overflowed"); 
     catch (StringIndexOutOfBoundsException e) { 
      System.out.println("Pass: " +e.getMessage()); 
     
      
     //Set String value using setting type float 
     try { 
      settingDefault.SetType(TYPE_FLOAT); 
      settingDefault.SetValue("TestName"); 
      System.out.println("Fail: Allowed to set String using " + 
        "setting type float"); 
     catch (IllegalArgumentException e) { 
      System.out.println("Pass: " +e.getMessage()); 
     
      
     /* Test SetValueInt(INT) */ 
     try { 
      settingDefault.SetType(TYPE_FLOAT); 
      settingDefault.SetValue(55); 
      System.out.println("Fail: Setting value of type INT has " + 
        "been allowed to" + 
        "be set using setting type float"); 
     catch (IllegalArgumentException e) { 
      System.out.println("Pass: "+e.getMessage()); 
     
      
     /* Test SetValue(double) */ 
     try { 
      settingDefault.SetType(TYPE_INT); 
      settingDefault.SetValue(55.55); 
      System.out.println("Fail: Setting of type INT has been " + 
        "allowed to set value" 
        " to type double"); 
     catch (IllegalArgumentException e) { 
      System.out.println("Pass: "+e.getMessage()); 
     
      
    } 
     
}