1 2 3 4

advertisement
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
Demo for: Custom-Made Classes
Driver.class */
package wu.andy;
import javax.swing.JOptionPane;
import java.util.GregorianCalendar;
public class Driver
{
public static void main(String[] args)
{
// ***** Test for calcNewDate() *****
boolean forward = true;
String forwardStr = "";
// Ask user to enter a start date
String start = JOptionPane.showInputDialog(null,
"Start Date is (in mm/dd/yyyy format):",
"calcNewDate() Test",
JOptionPane.QUESTION_MESSAGE);
// Ask whether it is for calculating a future or a past date
// The default is set as Yes (hence the [Y] part in the prompt)
String direction = JOptionPane.showInputDialog(null,
"Looking for a future date? (Y/N) [Y]",
"calcNewDate() Test",
JOptionPane.QUESTION_MESSAGE);
// Based on user's answer, adjust the prompt for the next dialog box,
// and if not for forward calculation (default), change that.
// The user will be searching for a past date only when he/she
// specifically type in "N" or "No" (case-insensitive). If the user
// enters anything else, or simply presses Enter without entering
// anyting at all, the search is always forward. This behavior is in
// agreement with Yes being the default.
if ( direction.equalsIgnoreCase("N") ||
direction.equalsIgnoreCase("No") )
{
forward = false;
forwardStr = "How many days ago in the past?";
}
else
{
forwardStr = "How many days ahead in the future?";
}
// Ask user for the interval between the start date and the target date
E:/BCIS 3680/10e-customclasses/Driver.java
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89 }
String intervalStr = JOptionPane.showInputDialog(null,
forwardStr,
"calcNewDate() Test",
JOptionPane.QUESTION_MESSAGE);
int interval = Integer.parseInt(intervalStr);
// Call calcNewDate() in DateAssistant class
String target = DateAssistant.calcNewDate(start, interval, forward);
// Display the result
JOptionPane.showMessageDialog(null,
"The date you're looking for is : " + target + ".",
"calcNewDate() test",
JOptionPane.INFORMATION_MESSAGE);
// **** Test for formatDate() - TimeZone Version *****
// Create a GregorianCalendar object for the current time
GregorianCalendar gc = new GregorianCalendar();
// Ask user for the time zone
// The default is Central, as indicated by the [Central] part at the
// end of the prompt. If the user doesn't enter any of the other five
// zone names in correct spelling (but case doesn't matter), or enters
// anything else, the time zone remains Central (also see source code
// in DateAssistant.class).
String timeZone = JOptionPane.showInputDialog(null,
"Please specify a time zone (Hawaii, Alaska, Pacific,\n" +
"Mountain, Central, Eastern) [Central]",
"formatDate() Test",
JOptionPane.QUESTION_MESSAGE);
// Call formatDate() in DateAssisant, passing time zoen string as the
// second argument
String nowString = DateAssistant.formatDate(gc, timeZone);
// Display the result
JOptionPane.showMessageDialog(null,
"The current time is " + nowString + ".",
"formatDate() Test",
JOptionPane.INFORMATION_MESSAGE);
}
E:/BCIS 3680/10e-customclasses/Driver.java
Download