public class MemoTest {
static Random random = new Random();
static String verses[] = {
"AAA", "BBB", "CCC", "DDD", "EEE"
};
static Memo[] memos;
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws InterruptedException {
initialize();
}
private static void initialize() throws InterruptedException {
memos = new Memo[10];
memos[0] = new Memo(new Time(7, 0), "Get up !");
memos[1] = new Memo(new Time(8, 30), "Give me a verse", (Memo alarm) -> {
System.out.println("Today's verse: " + verses[random.nextInt(verses.length)]);
});
memos[2] = new Memo(new Time(12, 10), "Lunch time ...");
memos[3] = new Memo(new Time(17, 10), "Call my mother", new Notify() {
@Override
public void todo(Memo alarm) {
// Send a message to mother that I am going home
}
});
memos[4] = new Memo(new Time(17, 30), "Go home ~");
memos[5] = new Memo(new Time(11, 10), "Go to sleep :)");
memoService();
}
/**
* Check each alarm whether it is time up. If yes, notify me and set the
* notified flag to true
*
* @param now
*/
private static void checkForReset(Date now) {
}
private static void memoService() throws InterruptedException {
while (true) {
Date now = new Date();
checkForReset(now);
for (Memo memo : memos) {
if (memo != null) {
if (!memo.notified && memo.time.timeup(now)) {
memo.notified = true;
memo.notify.todo(memo);
}
}
}
Thread.sleep(1000);
}
}
}