
Microsoft Year 2000 Readiness Disclosure & Resource Center |
 |
 |
 |
Building Year-2000 Compliant Applications with Visual Studio and Windows DNA |
 |
4. DNA Integration - Working with the sample code
Client Portion: Java
The sample application presents a simple user interface using an applet written in Java on a web page (Figure 1). JavaScript interacts with the applet to receive the values for the Customer ID, Transaction Date, Amount, Terms and Due Date. Before receiving the data, it first calls the applet's CheckData() function (Listing 1).
Figure 2 The web-based user interface. (If your browser does
not support inline frames, click here to view Figure
2 in a separate page.)
The CheckData() function first verifies that all of the required fields have been entered. Next, it checks that the value entered for the Transaction Date is a valid date. This is accomplished by using the Java SimpleDateFormat class (java.text.SimpleDateFormat), which is a subclass of the DateFormat class (java.text.DateFormat). In the Java language, a date is represented as a specific millisecond in time.
public String CheckData() { //Validates the data. //Returns an error message if data is invalid. //Otherwise, just returns an empty string. Calendar transDate = Calendar.getInstance(); Calendar dueDate = Calendar.getInstance(); //Check for required fields... if (m_cboCustomer.getSelectedItem().length() == 0) return "A customer is required."; if (m_txtDate.getText().trim().length() == 0) return "A transaction date is required."; if (m_txtDate.getText().trim().length() != 10) return "The transaction date is invalid. Proper format is 'mm/dd/yyyy'"; if (m_txtAmount.getText().trim().length() == 0) return "A transaction amount is required."; if (m_cboTerms.getSelectedItem() == "Due Date") if (m_txtDueDate.getText().trim().length() == 0) return "A due date is required."; else if (m_txtDueDate.getText().trim().length() != 10) return "The due date is invalid. Proper format is 'mm/dd/yyyy'"; //Validate the dates entered using the pattern mm/dd/yyyy. //If I try to call parse() for an invalid date, an exception //occurs, which is caught by the exception handler //Specify the date format that I'm expecting. SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy"); df.setLenient(false); //Attempt to create a date from the text entered by the user. try { transDate.setTime(df.parse(m_txtDate.getText())); } catch (Exception e) { //If the user entered an invalid date, return an error message return "The transaction date is invalid. Proper format is 'mm/dd/yyyy'"; }
//Also validate the due date, if one was entered. if (m_cboTerms.getSelectedItem() == "Due Date") { try { dueDate.setTime(df.parse(m_txtDueDate.getText())); //make sure due date is the same or later //than the transaction date... if (transDate.after(dueDate)) { return "The due date cannot be earlier than the transaction date."; } } catch (Exception e) { return "The due date is invalid. Proper format is 'mm/dd/yyyy'"; } } //Make sure the amount entered is numeric. try { Float.valueOf(m_txtAmount.getText()).floatValue(); } catch (Exception e) { if (e.getClass().toString().equals("class java.lang.NumberFormatException")) return "Invalid numeric amount."; else return e.getClass().toString(); } //Everything is Ok, just return an empty string. return ""; }
|
|
|