// 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()
);
}
}