Program: Reminder Service
The ReminderService
program provides a
simple reminder service. The
load( )
method reads a plain text file containing
a list of appointments like the ones shown here, using a
SimpleDateFormat
:
1999 07 17 10 30 Get some sleep. 1999 07 18 01 27 Finish this program 1999 07 18 01 29 Document this program
Example 6-4 shows the full program.
Example 6-4. ReminderService.java
import java.io.*; import java.text.*; import java.util.*; import javax.swing.*; /** * Read a file of reminders, sleep until each is due, beep. */ public class ReminderService { class Item { Date due; String message; Item(Date d, String m) { due = d; message = m; } } ArrayList l = new ArrayList( ); public static void main(String argv[]) throws IOException { ReminderService rs = new ReminderService( ); rs.load( ); rs.run( ); } protected void load( ) throws IOException { BufferedReader is = new BufferedReader( new FileReader("ReminderService.txt")); SimpleDateFormat formatter = new SimpleDateFormat ("yyyy MM dd hh mm"); String aLine; while ((aLine = is.readLine( )) != null) { ParsePosition pp = new ParsePosition(0); Date date = formatter.parse(aLine, pp); if (date == null) { message("Invalid date in " + aLine); continue; } String mesg = aLine.substring(pp.getIndex( )); l.add(new Item(date, mesg)); } } public void run( ) { System.out.println("ReminderService: Starting at " + new Date( )); while (!l.isEmpty( )) { Date d = new Date( ); Item i = (Item)l.get(0); long interval = i.due.getTime() - ...
Get Java Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.