//////////////////////////////////////////////////////////////////////////
//                                                                      //
// MODULE: LunarMonth                   AUTHOR: Tim Sabin               //
// DESCRIPTION:  LunarMonth is a TimePeriod class that represents one   //
//   Lunar Month (4 weeks?)                                             //
// DATE CREATED: 06/14/1998             LAST UPDATED: 06/30/1998        //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

import TimePeriod;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.io.RandomAccessFile;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.ParsePosition;

/** A <code>LunarMonth</code> is a time period of exactly 4 weeks. Like
 * weeks, a <code>LunarMonth</code> starts on a Saturday. */
public class LunarMonth extends TimePeriod {
    /** Create a <code>LunarMonth</code> object. An external file is read
     * that contains the starting dates of fiscal years 1996 - 2004.
     * @param    year    USPS Fiscal Year
     * @param    fiscalMonth    USPS "Accounting Period" ID (1-13)*/
    public LunarMonth (int year, int fiscalMonth) {
        int yr = 0;
        int month = 0;
        int day = 0;
        String line;
        RandomAccessFile YearStartDate;
        DecimalFormat dp;
        ParsePosition pos;
        // Read in start day of fiscal year into local variables
        try {
            YearStartDate = new RandomAccessFile ("YearStart.date", "r");
            while (true) {
                line = YearStartDate.readLine ();
                pos = new ParsePosition (0);
                dp = new DecimalFormat ();
                yr = ((Long)dp.parse (line, pos)).intValue ();
                if (yr == year) {
                    break;
                }
            }
            pos.setIndex (pos.getIndex () + 1);
            month = ((Long)dp.parse (line, pos)).intValue () - 1;
            pos.setIndex (pos.getIndex () + 1);
            day = ((Long)dp.parse (line, pos)).intValue ();
            pos.setIndex (pos.getIndex () + 1);
            yr = ((Long)dp.parse (line, pos)).intValue ();// Start yr not same as FY!
            YearStartDate.close ();
        } catch (IOException e) {
            System.out.println ("Can't open YearStart.date");
        }    // end try block
        // Set up the begin date: use 1st day, then pace to passed lunar month
        begDate = new GregorianCalendar ();
        begDate.set (yr, month, day);
        begDate.add (Calendar.DATE, 28 * (fiscalMonth - 1));
        // Set up the end date: use beginning date, pace to last day of lunar month
        endDate = new GregorianCalendar ();
        endDate.set (begDate.get (Calendar.YEAR), begDate.get (Calendar.MONTH),
          begDate.get (Calendar.DATE));
        endDate.add (Calendar.DATE, 27);
        // Identify this object
        tpType = AP;
        tpID = fiscalMonth;
        tpFY = year;
    }
}
