import java.awt.GridBagLayout;

import java.awt.GridBagConstraints;

import java.awt.CardLayout;

import java.awt.FlowLayout;

import java.awt.Button;

import java.awt.Frame;

import java.awt.GridLayout;

import java.awt.Label;

import java.awt.Panel;

import java.awt.TextArea;

import java.awt.TextField;

import java.awt.Insets;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

 

public class AccountInteractionFrame extends Frame

{

private BankMachine _bankMachine;

private BankAccount _bankAccount;

private Label _prompt = new Label( "Please enter the requested information." );

private Label _balance = new Label( "" ),

_interestRate = new Label( "" );

 

private TextField _name = new TextField(""),

_password = new TextField(""),

_credit = new TextField( "0.0" ),

_debit = new TextField( "0.0" );

private Panel _panelHolder = new Panel(), // the panel used to hold either of the next two panels

_loginPanel = new Panel(),

_interactionPanel = new Panel();

 

public AccountInteractionFrame( BankMachine bankMachine ) {

_bankMachine = bankMachine;

setTitle( "Account Interaction" );

setLayout( new GridBagLayout() );

GridBagConstraints gbc;

gbc = new GridBagConstraints();

gbc.gridx = 0;

gbc.gridy = 0;

gbc.weighty = 1.0;

gbc.weightx = 1.0;

gbc.fill = GridBagConstraints.BOTH;

gbc.insets = new Insets( 4,4,4,4 );

add( _prompt, gbc );

_panelHolder.setLayout( new CardLayout() );

gbc = new GridBagConstraints();

gbc.gridx = 0;

gbc.gridy = 1;

gbc.weightx = 1.0;

gbc.insets = new Insets( 4,4,4,4 );

gbc.fill = GridBagConstraints.BOTH;

add( _panelHolder, gbc );

setUpLoginPanel();

setUpInteractionPanel();

setPanel( _loginPanel );

pack();

}

private void setUpLoginPanel() {

_password.setEchoChar( '*' );

_loginPanel.setLayout( new GridBagLayout() );

GridBagConstraints gbc;

gbc = new GridBagConstraints();

gbc.gridx = 0;

gbc.gridy = 1;

gbc.fill = GridBagConstraints.HORIZONTAL;

gbc.insets = new Insets( 4,4,4,4 );

_loginPanel.add( new Label( "Name: " ), gbc );

gbc = new GridBagConstraints();

gbc.gridx = 1;

gbc.gridy = 1;

gbc.fill = GridBagConstraints.HORIZONTAL;

gbc.insets = new Insets( 4,4,4,4 );

_loginPanel.add( _name, gbc );

gbc = new GridBagConstraints();

gbc.gridx = 0;

gbc.gridy = 2;

gbc.fill = GridBagConstraints.HORIZONTAL;

gbc.insets = new Insets( 4,4,4,4 );

_loginPanel.add( new Label( "Password: " ), gbc );

gbc = new GridBagConstraints();

gbc.gridx = 1;

gbc.gridy = 2;

gbc.fill = GridBagConstraints.HORIZONTAL;

gbc.insets = new Insets( 4,4,4,4 );

_loginPanel.add( _password, gbc );

Panel buttonHolder = new Panel();

buttonHolder.setLayout( new FlowLayout( FlowLayout.CENTER ) );

gbc = new GridBagConstraints();

gbc.gridx = 0;

gbc.gridy = 4;

gbc.gridwidth = 2;

gbc.fill = GridBagConstraints.HORIZONTAL;

gbc.insets = new Insets( 4,4,4,4 );

_loginPanel.add( buttonHolder, gbc );

Button a = new Button( "Cancel" );

buttonHolder.add( a );

a.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) {

setVisible( false );

}} );

Button b = new Button( "Ok" );

buttonHolder.add( b );

b.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) {

_bankAccount = _bankMachine.getBank().findAccountByNameAndPassword( _name.getText(), _password.getText() );

if( _bankAccount == null )

_prompt.setText( "Could not find a bank account with the supplied name and password." );

else

setPanel( _interactionPanel );

}} );

}

private void setPanel( Panel p ) {

_panelHolder.removeAll();

_panelHolder.add( p, "CENTER" );

_panelHolder.validate();

if( p == _interactionPanel ) {

setTitle( "Account: " + _bankAccount.getName() );

_balance.setText( "$" + _bankAccount.getBalance() );

_interestRate.setText( _bankAccount.getInterestRate() + "%" );

}

pack();

}

private void setUpInteractionPanel() {

_interactionPanel.setLayout( new GridBagLayout() );

GridBagConstraints gbc;

Button b;

int row = 0;

gbc = new GridBagConstraints();

gbc.gridx = 0;

gbc.gridy = 0;

gbc.insets = new Insets( 4,4,4,4 );

_interactionPanel.add( new Label( "Balance: " ), gbc );

gbc = new GridBagConstraints();

gbc.gridx = 0;

gbc.gridy = 1;

gbc.insets = new Insets( 4,4,4,4 );

_interactionPanel.add( new Label( "Interest Rate: " ), gbc );

gbc = new GridBagConstraints();

gbc.gridx = 1;

gbc.gridy = 0;

gbc.fill = GridBagConstraints.HORIZONTAL;

gbc.insets = new Insets( 4,4,4,4 );

_interactionPanel.add( _balance, gbc );

gbc = new GridBagConstraints();

gbc.gridx = 1;

gbc.gridy = 1;

gbc.fill = GridBagConstraints.HORIZONTAL;

gbc.insets = new Insets( 4,4,4,4 );

_interactionPanel.add( _interestRate, gbc );

gbc = new GridBagConstraints();

gbc.gridx = 0;

gbc.gridy = 2;

gbc.insets = new Insets( 4,4,4,4 );

_interactionPanel.add( _credit, gbc );

gbc = new GridBagConstraints();

gbc.gridx = 0;

gbc.gridy = 3;

gbc.insets = new Insets( 4,4,4,4 );

_interactionPanel.add( _debit, gbc );

b = new Button( "credit()" );

gbc = new GridBagConstraints();

gbc.gridx = 1;

gbc.gridy = 2;

gbc.insets = new Insets( 4,4,4,4 );

_interactionPanel.add( b, gbc );

b.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) {

try {

double creditAmount = new Double( _credit.getText() ).doubleValue();

_bankAccount.credit( creditAmount );

_balance.setText( "$" + _bankAccount.getBalance() );

}

catch ( NumberFormatException exception ) {

_prompt.setText( "Credit field was not in numerical form." );

}

_bankMachine.updateInfo();

validate();

}} );

b = new Button( "debit()" );

gbc = new GridBagConstraints();

gbc.gridx = 1;

gbc.gridy = 3;

gbc.insets = new Insets( 4,4,4,4 );

_interactionPanel.add( b, gbc );

b.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) {

try {

double debitAmount = new Double( _debit.getText() ).doubleValue();

if( debitAmount < _bankAccount.getBalance() )

_bankAccount.debit( debitAmount );

else

_prompt.setText( "You can't take out that much." );

_balance.setText( "$" + _bankAccount.getBalance() );

}

catch ( NumberFormatException exception ) {

_prompt.setText( "Debit field was not in numerical form." );

}

_bankMachine.updateInfo();

validate();

}} );

}

}