


import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JOptionPane;


public class BankApplet extends JApplet
{  

   
   JButton    createButton             = new JButton("New Account");
   JButton    withdrawButton             = new JButton("Withdraw");
   JButton    depositButton              = new JButton("Deposit");
   JButton    balanceButton              = new JButton("Balance");

   JTextField nameText            = new JTextField(20);
   JLabel nameLabel = new JLabel("Name or Account#");
   JTextField amtText             = new JTextField(20);
   JLabel amtLabel = new JLabel("Amount");
   JTextArea  feedback               = new JTextArea(20, 50);
   JScrollPane scroll = new JScrollPane(feedback);

   Container c;
   Bank myBank;

   public void init( )
   {      
      // Some messages for the top of the Apple

      feedback.setFont(new Font("Monospaced",Font.BOLD, 24));
      createButton.setFont(new Font("Monospaced",Font.BOLD, 24));
      withdrawButton.setFont(new Font("Monospaced",Font.BOLD, 24));
      depositButton.setFont(new Font("Monospaced",Font.BOLD, 24));
      balanceButton.setFont(new Font("Monospaced",Font.BOLD, 24));
      amtText.setFont(new Font("Monospaced",Font.BOLD, 24));
      nameText.setFont(new Font("Monospaced",Font.BOLD, 24));


      c = getContentPane();
      c.setLayout(new FlowLayout());
      c.add(new JLabel("Welcome to automatic teller machine"));
      addHorizontalLine(Color.blue);
       
       c.add(nameLabel);
      c.add(nameText);

      addNewLine();
      c.add(amtLabel);
      c.add(amtText);
      addNewLine();
      c.add(createButton);

      

      c.add(withdrawButton);
      c.add(depositButton);
      c.add(balanceButton);

      addNewLine( );
     

      // A TextArea at the bottom to write messages:
      addHorizontalLine(Color.blue);
      addNewLine( );
      feedback.setEditable(false);
      //      feedback.append("I am ready for your first action.");
      feedback.append("Enter values in the fields and press action" + "\n");
      //      c.add(feedback);
      //scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
      //scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
      
      c.add(scroll);
      addNewLine();
   
      // Tell the JButtons what they should do when they are clicked:
      createButton.addActionListener(new createListener( ));
      withdrawButton.addActionListener(new withdrawListener( ));
      depositButton.addActionListener(new depositListener( ));
      balanceButton.addActionListener(new balanceListener( ));
      myBank = new Bank();

   }


   class createListener implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {
         //feedback.append("Please enter name and amount\n");
	 String name = nameText.getText();
	 try {
	    double amt = Double.parseDouble(amtText.getText());
	    feedback.append("Creating account\n");
	    feedback.append("Account number: " +
			 myBank.createAccount(amt,name)+ "\n");
	 }
	 catch (NumberFormatException exc)
	 { feedback.append(exc.toString() + "\n");
	   feedback.append("Enter real number" + "\n");}

      }
   }

   class withdrawListener implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {   
	 //feedback.append("\n Please enter account number and amount\n");
	 int aNo = Integer.parseInt(nameText.getText());
	 double amt = Double.parseDouble(amtText.getText());
	 feedback.append("Withdrawing money"+ "\n");
	 try {
	    feedback.append("Balance: " +
			 myBank.withdraw(aNo, amt) + "\n");
	 }
	 catch (InsufficientFundsException exc)
	{
	   feedback.append(exc.toString());
	}
      }
   }

   class depositListener implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {  
	 //feedback.append("Please enter account number and amount\n");
	 int aNo = Integer.parseInt(nameText.getText());
	 double amt = Double.parseDouble(amtText.getText());
	 feedback.append("Depositing money\n");
	 myBank.deposit(aNo, amt);

      }
   }
   
   class balanceListener implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {
	 int aNo = Integer.parseInt(nameText.getText());
	 feedback.append("Balance: " +
			 myBank.inquireBalance(aNo)+ "\n");
      }
   }    


     

   private void addHorizontalLine(Color co)
   {  
      // Add a Canvas 10000 pixels wide but only 1 pixel high, which acts as
      // a horizontal line to separate one group of components from the next.
      Canvas line = new Canvas( );
      line.setSize(10000,1);
      line.setBackground(co);
      c.add(line);
   }

   
   private void addNewLine( ) 
   {  
      // Add a horizontal line in the background color. The line itself is
      // invisible, but it serves to force the next Component onto a new line.
      addHorizontalLine(getBackground( ));
   }


      
      

      
}





