Instructions
Requirements and Specifications
Source Code
DAY OF WEEK
import java.util.ArrayList;
import java.util.List;
public enum DayOfWeek
{
MONDAY('M'),
TUESDAY('T'),
WEDNESDAY('W'),
THURSDAY('R'),
FRIDAY('F'),
SATURDAY('S'),
SUNDAY('Y');
/** Accessor to the abbreviation for this day.
* @return char the one-letter abbreviation
*/
private final char letter;
// Constructor
DayOfWeek(char letter)
{
this.letter = letter;
}
public char getLetter()
{
return this.letter;
}
/** Accessor to a List of days, in order.
* @return List list of the days.
*/
public static List getDays()
{
List days = new ArrayList();
for(DayOfWeek day: values())
days.add(day);
return days;
}
/** Get the day corresponding to the specified letter.
* @param letter an abbreviation for a day.
* @return DayOfWeek the day which matches the given letter.
*/
public static DayOfWeek toDayOfWeek(char letter)
{
switch(letter)
{
case 'M':
return DayOfWeek.MONDAY;
case 'T':
return DayOfWeek.TUESDAY;
case 'W':
return DayOfWeek.WEDNESDAY;
case 'R':
return DayOfWeek.THURSDAY;
case 'F':
return DayOfWeek.FRIDAY;
case 'S':
return DayOfWeek.SATURDAY;
case 'Y':
return DayOfWeek.SUNDAY;
default:
throw new IllegalArgumentException("toDayOfWeek: " + letter + " is not a day of week");
}
}
/** Get the day following this one.
* @return DayOfWeek day that follows this one in the defined sequence.
*/
public DayOfWeek next()
{
return values()[(this.ordinal()+1) % values().length];
}
}