//////////////////////////////////////////////////////////////////////////
//                                                                      //
// MODULE: Week                     AUTHOR: Tim Sabin                   //
// DESCRIPTION:  Week is a TimePeriod class that represents one week.   //
//   Weeks must always start on a given day of the week. Hence, the     //
//   beginning day of the week is the 1st specified weekday *on or      //
//   after* the specified date. The end date is 6 days later.           //
// DATE CREATED: 06/11/1998         LAST UPDATED: 12/09/1998            //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

import TimePeriod;
import java.util.GregorianCalendar;
import java.util.Calendar;
import java.io.RandomAccessFile;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.ParsePosition;

/** A <code>Week</code> object represents a single week in time. Like the
 * <code>LunarMonth</code> class, Weeks start on Saturdays. */
public class Week extends TimePeriod {
    /** Create a Week object using calendar year, month, and day. */
    public Week (int year, int month, int day) {
        begDate = new GregorianCalendar ();
        begDate.set (year, month, day);
        endDate = new GregorianCalendar ();
        endDate.set (year, month, day);
        endDate.add (Calendar.DATE, 6);
        // Identify this object
        tpType = WEEKLY;
        tpID = 0;
        tpFY = year;
    }
    /** Create a Week object. Guarantees the Week starts on a Saturday.
     * @param    year    The USPS fiscal year the week is in.
     * @param    weekNo    The number of the week in the fiscal year. */
    public Week (int year, int weekNo) {
        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, 7 * (weekNo - 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, 6);
        // Identify this object
        tpType = WEEKLY;
        tpID = weekNo;
        tpFY = year;
    }
}
