/**

 *

 * Build and use of Interest Computer frame

 * @author Ahmad Mobasseri

 */

import javax.swing.*;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.event.*;

import java.text.NumberFormat;

 

class InterestFrame extends JFrame implements ActionListener

{

      private static final int TXT_FLD_COLOMNS = 10;

      private static final int FRAME_WIDTH  = 600;

      private static final int FRAME_HEIGHT = 300;

     

      private JLabel header;

      private JLabel principleLabel;

      private JTextField principleText;

      private JLabel blankLabel1;

      private JLabel rateLabel;

      private JTextField rateText;

      private JButton calculate;

      private JLabel timeLabel;

      private JTextField timeText;

      private JLabel blankLabel2;

      private JLabel amountLabel;

      private JTextField amountText;

      private JButton clear;

      private JPanel bodyPanel;

      public static final long serialVersionUID = 1;

 

/**

 * Constructor

 */

   public InterestFrame()    

   {

        // JFrame

        setTitle("Calculate Account Value");

        setSize(FRAME_WIDTH, FRAME_HEIGHT);

//      setBackground(Color.GREEN);

        getContentPane().setBackground(Color.GREEN);

 

       

        // Header

      header = new JLabel("Welcome to the Interest Computer");

      Font headerFont = new Font("serif", Font.BOLD, 32);

      header.setFont(headerFont);

      header.setForeground(Color.RED);

      add(header, BorderLayout.NORTH);          // add header to frame layout

       

      // Body Layout

      bodyPanel = new JPanel(new GridLayout(4, 3));

      bodyPanel.setBackground(Color.YELLOW);

           

      Font labelFont = new Font("serif", Font.BOLD, 20);                // Font for labels

      Font textFont = new Font("New Times Roman", Font.BOLD, 18);       // Font for Text Fields

     

      // Principle

      principleLabel = new JLabel("Principle:", SwingConstants.RIGHT); // Label

      principleLabel.setFont(labelFont);

      bodyPanel.add(principleLabel);                       

      principleText = new JTextField(TXT_FLD_COLOMNS);                              // Text Fld

      principleText.setFont(textFont);

      bodyPanel.add(principleText);                        

      blankLabel1 = new JLabel("");                                                       // Blank Label

      bodyPanel.add(blankLabel1);                                

 

      // Rate

      rateLabel = new JLabel("Rate:", SwingConstants.RIGHT);                  // Label

      rateLabel.setFont(labelFont);

      bodyPanel.add(rateLabel);                                  

      rateText = new JTextField(TXT_FLD_COLOMNS);                                   // Text Fld

      rateText.setFont(textFont);

      bodyPanel.add(rateText);                                   

      // Calculate                                                                                    // Button

      calculate = new JButton("Calculate");

      bodyPanel.add(calculate);                                  

 

      // Time

      timeLabel = new JLabel("Time(in years):", SwingConstants.RIGHT);  // Label

      timeLabel.setFont(labelFont);

      bodyPanel.add(timeLabel);                                  

      timeText = new JTextField(TXT_FLD_COLOMNS);                                   // Text Fld

      timeText.setFont(textFont);

      bodyPanel.add(timeText);                                   

      blankLabel2 = new JLabel("");                                                       // Blank label

      bodyPanel.add(blankLabel2);                                     

 

      // Amount

      amountLabel = new JLabel("Amount:", SwingConstants.RIGHT);        // Label

      amountLabel.setFont(labelFont);

      bodyPanel.add(amountLabel);                                

      amountText = new JTextField(TXT_FLD_COLOMNS);                           // Text

      amountText.setFont(textFont);

      amountText.setEditable(false);

      bodyPanel.add(amountText);                                 

      // Clear                                                                                        // Button

      clear = new JButton("Clear");

      bodyPanel.add(clear);        

 

      // Add bodyPanel to JFrame Layout

      add(bodyPanel, BorderLayout.CENTER);

     

      // Events

      calculate.addActionListener(this);                    // on clicking Calculate button  

      clear.addActionListener(this);                              // on clicking Clear button 

   }

  

/**

 * Event Action Listener for Calculate & Clear buttons 

 */

   public void actionPerformed(ActionEvent event)

   { 

         JButton clickedButton = (JButton) event.getSource();

         if (clickedButton == calculate)                    // Calculate button

         { 

               try

               {

                     double pricipleValue  = Double.parseDouble(principleText.getText());

                     double rateValue      = Double.parseDouble(rateText.getText());  

                     double timeValue      = Double.parseDouble(timeText.getText());

                     InterestComputer compute = new InterestComputer(pricipleValue, rateValue, timeValue);

                     double amountDouble = compute.interestCompute();

                     String myString = NumberFormat.getCurrencyInstance().format(amountDouble);

                     amountText.setText(myString);

               }

               catch (NumberFormatException e)

               {

                     System.out.println("Hmm.. Numbers only");

                     amountText.setText("Hmm.. Numbers Only");

               }

               catch (Exception e)

               {

                     System.out.println("Error: " + e.getMessage());

                     amountText.setText("No Nagative numbers");  

               }   

         }

         else           // Clear button

         {

               principleText.setText("");

               rateText.setText(""); 

               timeText.setText("");

               amountText.setText("");

               principleText.requestFocus();

         }

  }

  

}