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() - d.getTime(  ); 
            if (interval > 0) { 
                System.out.println("Sleeping until " + i.due); 
                try { 
                    Thread.sleep(interval); 
                } catch (InterruptedException e) { 
                    System.exit(1);    // unexpected intr 
                } 
                message(i.due + ": " + i.message); 
            } else 
                message("MISSED " + i.message + " at " + i.due); 
            l.remove(0); 
        } 
        System.exit(0); 
    } 
    void message(String message) { 
        System.out.println("007" + message); 
        JOptionPane.showMessageDialog(null, 
            message,  
            "Timer Alert",                // titlebar 
            JOptionPane.INFORMATION_MESSAGE);    // icon 
    } 
}

I create a nested class Item to store one notification, storing its due date and time and the message to display when it’s due. The load( ) method reads the file containing the data and converts it, using the date parsing from Section 6.6. The run( ) method does the necessary arithmetic to sleep( ) for the right length of time to wait until the next reminder is needed. The reminder is then displayed both on the standard output (for debugging) and in a dialog window using the Swing JOptionPane (see Section 13.8). The message( ) method consolidates both displays, allowing you to add a control to use only standard output or only the dialog.

See Also

In JDK 1.3, the new class java.util.Timer can be used to implement much of the functionality of this reminder program.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.75.217