/**
* A simple class to play around with arrays
*/
public class DailyGoal
{
String [] goal;
public DailyGoal(int numberDays) {
goal = new String[numberDays];
}
public boolean setGoal(String goalForDay, int day) {
if(day>=1 && day<=goal.length) {
goal[day-1] = goalForDay;
return true;
}
return false;
}
/**
* Return the goal for the given day if it is set,
* or null if it is not set or the day is invalid.
*/
public String getGoal(int day) {
if(day>=1 && day<=goal.length) {
return goal[day-1];
}
return null;
}
/**
* Print the goals, with the day numbers specified.
* If no goal is set for a day, skip it.
* Example possible output:
* 1 Eat
* 3 Sleep
* 4 nothing
*
*/
public void printGoals() {
for(int i=0; i<goal.length; i++) {
if(goal[i]!=null) {
System.out.println((i+1)+" "+goal[i]);
}
}
}
/**
* Return the longest goal on the list.
*/
public String longestGoal() {
String longestGoal=goal[0];
for(int i=1;i<goal.length;i++) {
if(goal[i]!=null && goal[i].length() > longestGoal.length()) {
longestGoal = goal[i];
}
}
return longestGoal;
}
}