Java Notes

Rainfall user interface - Dialog

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
// Rainfall.java - User interface to RainfallStats
// This separates the user interface from the "model"
//      or "business logic".  Such separation is
//      required in larger programs to keep them
//      readable, altho in this small program it
//      is only used to illustrate the idea.
// Fred Swartz - Nov 29 2005

import javax.swing.*;

public class RainfallDialog {

    public static void main(String[] args) {

        RainfallStats rainData = new RainfallStats();

        //... Read in and accumulate data.
        while (true) {
            String rainStr = JOptionPane.showInputDialog(null, "Rain?");
            if (rainStr == null || rainStr.trim().length() == 0) {
                break;
            }
            rainData.add(Double.parseDouble(rainStr));
        }

        //... Print the results.
        JOptionPane.showMessageDialog(null,
                   "Total rain = "   + rainData.getTotal() + "\n"
                 + "Average rain = " + rainData.getAverage()
                 );
    }
}