import java.awt.*; // To use Abstract Windows Toolkit classes. import java.awt.event.*; // To use Abstract Windows Toolkit event handling. import javax.swing.*; // To use Java swing classes. import java.util.*; // For date processing. import java.text.*; // For currency formatting. /** * This class defines an applet to determine if a library book is late, and * if so, calculate the late fee to be assessed. Internationalization is * used so the user can choose a currency. For this applet it will be the * US Dollar and the British Pound. * * As of November 13, 2000 the current rate of exchange is: * .25 United States Dollar is equal to 0.1739 British Pound * * Written by: David Nelson */ public class Ch12Applet extends JApplet implements ActionListener, ItemListener { // Object references for user interface components having a broad scope in this applet. JLabel dueDateHdg; // For due date heading. JTextField dueDate; // For due date text field. ButtonGroup currencySelect; // For radio button group of currency. JRadioButton item1; // For US Dollar item. JRadioButton item2; // For British Pound item. JLabel lateFeeHdg; // For late fee heading. JTextField lateFee; // For late fee text field (non editable). // Constants for currency choices. public static final String ITEM1 = "US Dollar"; public static final String ITEM2 = "British £"; // All other variables public double newLateFee = .25; // Construct number and date "formatters" for the default locale (USA). NumberFormat localCurrency = NumberFormat.getCurrencyInstance(Locale.US); DateFormat localDate = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US); /** * This method is automatically called when the browser loads the web page * that launches the applet. Its purpose is to perform applet initalization. */ public void init() { // Specify the applet's layout and background color. getContentPane().setLayout(new FlowLayout()); getContentPane().setBackground(Color.lightGray); } /** * This method is automatically called after init() and every time the * web page is revisited (due to surfing). It adds components to the * applet window in the desired sequence. */ public void start() { // Declare a main heading, set its font, color, alignment, and add it // to the applet window. JLabel mainHdg = new JLabel("Library of Sorts"); mainHdg.setFont(new Font("Arial", Font.BOLD, 20)); mainHdg.setForeground(Color.blue); mainHdg.setHorizontalAlignment(SwingConstants.CENTER); getContentPane().add(mainHdg); // Instantiate an object representing the current date and time. GregorianCalendar today = new GregorianCalendar(); // Construct a text field for the user to enter the due date. JLabel dueDateHdg = new JLabel("Due Date:"); getContentPane().add(dueDateHdg); dueDate = new JTextField(" ", 10); getContentPane().add(dueDate); dueDate.addActionListener(this); // Construct a label heading for the currency selection. // Create a button group for the user to select between either the // US Dollar or the British Pound as the currency. // Add the items to the button group. // Add the radio buttons to the applet window and register a listener. JLabel currencyHdg = new JLabel("Currency:"); getContentPane().add(currencyHdg); currencySelect = new ButtonGroup(); item1 = new JRadioButton(ITEM1, true); currencySelect.add(item1); item2 = new JRadioButton(ITEM2, false); currencySelect.add(item2); getContentPane().add(item1); item1.addItemListener(this); JLabel blank2 = new JLabel(" "); getContentPane().add(blank2); getContentPane().add(item2); item2.addItemListener(this); // Construct a label and text box in which the late fee will be displayed. // Set its color scheme, alignment, mark it as non-editable. Add it // to the applet window. JLabel lateFeeHdg = new JLabel("Amount Due:"); getContentPane().add(lateFeeHdg); lateFee = new JTextField(" ", 10); lateFee.setBackground(Color.black); lateFee.setForeground(Color.green); lateFee.setHorizontalAlignment(SwingConstants.CENTER); lateFee.setEditable(false); getContentPane().add(lateFee); } /** * This method is automatically called when the user is about to surf to * another web page or closes the browser. Its purpose is to stop applet * execution. It resets the applet for a smooth restart. */ public void stop() { // Remove all components so they can be re-added on a restart. getContentPane().removeAll(); } /** * This method is automatically called after stop() when the browser is being * closed. Its purpose is to allow the applet to perform termination processing. * * This applet doesn't require the method, but a shell is coded for documentation * and maintenance purposes. */ public void destroy() { } /** * This method implements the abstract method of the ActionListener interface. * * In this applet, the only event to be handled is the user specifying a * due date for the borrowed library book. */ public void actionPerformed(ActionEvent e) { // Call an internal method that will trigger the late fee calculation. CalculateLateFee(); } /** * This method implements the abstract method of the ItemListener interface. * * In this applet, the user has selected a different currency from the radio * button group. */ public void itemStateChanged(ItemEvent e) { // If a currency select radio button was clicked, determine which // item was selected. if (e.getSource() instanceof JRadioButton) { if (e.getItem().equals(item1)) { // Set late fee to US currency of 25 cents newLateFee = .25; // United States Currency localCurrency = NumberFormat.getCurrencyInstance(Locale.US); // Recalculate the late fee CalculateLateFee(); } else if (e.getItem().equals(item2)) { // Set late fee to British currency equivalent of US 25 cents newLateFee = .1739; // British Pound Currency localCurrency = NumberFormat.getCurrencyInstance(Locale.UK); // Recalculate the late fee CalculateLateFee(); } } } /** * This method calculates the late fee which is determined by two things. * First, the due date field will trigger the action. * Second, the selected currency will determine what currency to display * the late fee in. */ public void CalculateLateFee() { // Construct an object representing the current date. GregorianCalendar currentDate = new GregorianCalendar(); // "try" the block which converts the string in the due date text field to a // GregorianCalendar object to prepare to compare it to the current date. try { DateFormat localDate = DateFormat.getDateInstance (DateFormat.SHORT,Locale.US); GregorianCalendar due = new GregorianCalendar(); due.setTime(localDate.parse(dueDate.getText().trim())); // Compare the due date to the current date. if (due.before(currentDate)) { // If the due date is before the current date calculate and display the // late fee. The daily late fee is equivalent to 25 cents per day. So // we multiplay the 25 cents per day by the number of days between the // current date and the due date. int days = currentDate.get(Calendar.DAY_OF_YEAR) - due.get(Calendar.DAY_OF_YEAR); double amtDue = newLateFee * days; // Set the amount due in the late fee text box // (formatted by the appropiate locale). lateFee.setText(localCurrency.format(amtDue)); } else { // If the due date is equal to or after the current date the late fee // will be zero. So set the days to zero to force a return value for // amount due to display "0.00" as the late fee. int days = 0; double amtDue = newLateFee * days; lateFee.setText(localCurrency.format(amtDue)); } } // Otherwise... catch(Exception err) { } } }