//----------------------- // File: Vacation.cpp // Author: D. Frey // Date: 7/11/03 // Section: 1234 // Email: frey@cs.umbc.edu // Project: none // // This code illustrates the implementation of // a class which uses aggregation (aka composition) //------------------------------------------------ #include #include #include "DayOfYear.h" #include "Vacation.h" using namespace std; //---------------------- // Vacation constructor // PreConditions // none // PostCondition // a new object is created with the // user specified values // //---------------------------- // Note the use of the initialization list // to invoke the DayOfYear constructor for m_startDOY // // alternate implementations are also possible // --- what might some of them be? // --- why is this one best? // //---------------------- Vacation::Vacation( int month, int day, int nrDays) : m_startDOY( month, day), m_nrDays( nrDays) { if (m_nrDays <= 0) m_isValid = false; else m_isValid = m_startDOY.IsValid( ); } //----------------------------------- // Set( ) -- a mutator // PreConditions: // 1 <= month <= 12 // 1 <= day <= 31 // PostConditions // month and day changed // // Note the use of DayOfYear's method //------------------------------------- void Vacation::Set (int newMonth, int newDay) { m_startDOY.Set( newMonth, newDay); m_isValid = m_startDOY.IsValid( ); } //----------------------------------- // Set( ) -- a mutator // PreConditions: // 1 <= day // PostConditions // vacation duration changed // //------------------------------------- void Vacation::Set (int newNrDays) { m_nrDays = newNrDays; m_isValid = (m_nrDays > 0); } //----------------------------------- // GetDayOfYear( ) -- an accessor // PreConditions: // none // PostConditions // returns the DayOfYear via reference to const // // Note the return type // //------------------------------------- const DayOfYear& Vacation::GetDayOfYear ( ) const { return m_startDOY; } //----------------------------------- // GetMonth( ) -- an accessor // PreConditions: // none // PostConditions // returns 1 = Jan, 2 = Feb, etc // // Note the use of DayOfYear's method //------------------------------------- int Vacation::GetMonth ( ) const { return m_startDOY.GetMonthNumber( ); } //----------------------------------- // GetDay( ) -- an accessor // PreConditions: // none // PostConditions // returns day of month that vacation begins // // Note the use of DayOfYear's method //------------------------------------- int Vacation::GetDay ( ) const { return m_startDOY.GetDay( ); } //----------------------------------- // GetDuration( ) -- an accessor // PreConditions: // none // PostConditions // returns vacation duration // //------------------------------------- int Vacation::GetDuration ( ) const { return m_nrDays; } //----------------------------------- // IsValid // PreConditions: // none // PostConditions // returns true if the Vacation data is valid // //------------------------------------- int Vacation::GetDuration ( ) const { return m_isValid; } //-------------------------------- // Input // Preconditions: // none // PostCondition: // user prompted for month, day and duration // which are saved in private data // // Note use of DayOfYear's method //------------------------------------ void Vacation::Input ( ) { m_startDOY.Input(); cout << "Enter number of days: "; cin >> m_nrDays; if (m_nrDays < 1) { cout << "Invalid vacation duration" << endl; exit ( 1 ); } } //-------------------------------- // Output // Preconditions: // none // PostCondition: // month, day and duration displayed // // Note use of DayOfYear's method //------------------------------------ void Vacation::Output ( ) const { m_startDOY.Output(); cout << "\nDuration: " << m_nrDays << endl; }