Ecco il codice, molto semplice:
codice:
public class BankFrame extends JFrame
{
public BankFrame( BankData data )
{
setTitle("Bank Account Manager");
setSize(230,150);
GridBagLayout layout = new GridBagLayout();
ActionListener listener = new BankAction();
setLayout( layout );
nContoLabel = new JLabel( "N° Conto corrente ");
versamentoLabel = new JLabel("Versamento");
nContoField = new JTextField(10);
versamentoField = new JTextField(10);
fineButton = new JButton("Fine");
fineButton.addActionListener(listener);
risultatoLabel = new JLabel();
saldoLabel = new JLabel();
add( nContoLabel, new GBC(0, 0).setAnchor(GBC.WEST) );
add( nContoField , new GBC(1,0).setInsets(1) );
add( versamentoLabel, new GBC(0, 1).setAnchor(GBC.WEST));
add( versamentoField , new GBC(1,1).setInsets(1));
add( fineButton, new GBC( 0, 2, 2, 1));
add( risultatoLabel, new GBC( 0, 3, 2, 1) );
add( saldoLabel, new GBC( 0, 4, 2, 1) );
}
private class BankAction implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
int accountNumber = Integer.parseInt( nContoField.getText() );
double versamento = Double.parseDouble( versamentoField.getText() );
try
{
int position = data.find( accountNumber );
if( position > 0 )
{
System.out.println( "Trovato l'acconto");
BankAccount account = data.read( position );
account.deposit( versamento );
data.write(position, account);
risultatoLabel.setText( "Account trovato");
saldoLabel.setText( "Nuovo bilancio = " + account.getBalance() );
}
else
{
System.out.println( "Creo un nuovo account");
BankAccount account = new BankAccount( accountNumber, versamento);
data.write( data.size(), account);
risultatoLabel.setText( "Creo un nuovo acconto");
saldoLabel.setText( "Nuovo bilancio = " + account.getBalance() );
}
}
catch( IOException exception){}
}
}
private JLabel nContoLabel;
private JLabel versamentoLabel;
private JTextField nContoField;
private JTextField versamentoField;
private JButton fineButton;
private JLabel risultatoLabel;
private JLabel saldoLabel;
private BankData data;
}