import java.awt.*; import java.util.*; import java.lang.*; import TypePanel; import InterestPanel; import NamePanel; import DepositPanel; import NewControlsPanel; import LabelPanel; import TransferChoicePanel; import AccountControls; // // Dialog classes // class ErrorDialog extends Dialog { Label message; Button OKButton; public ErrorDialog(Frame parent, String error) { super(parent, new String("Error"), true); message=new Label(error); OKButton=new Button("OK"); add("Center",message); add("South",OKButton); pack(); } public boolean handleEvent(Event event) { if(event.id==Event.ACTION_EVENT) { this.dispose(); return true; } return false; } } class TransferDialog extends Dialog { BankAccounts accounts; TransferChoicePanel accountsPanel; LabelPanel ammountPanel; Button transfer; TextArea output; public TransferDialog(Frame parent, BankAccounts accts, TextArea out) { super(parent, new String("Transfer"), true); accounts=accts; output=out; setLayout(new BorderLayout(3,1)); accountsPanel=new TransferChoicePanel(accounts); add("North",accountsPanel); accountsPanel.show(); ammountPanel=new LabelPanel("Ammount: "); add("Center",ammountPanel); ammountPanel.show(); transfer=new Button("Transfer"); add("South",transfer); pack(); } public boolean handleEvent(Event event) { if(event.id==Event.ACTION_EVENT) { if(event.target==transfer) { Double value=new Double(ammountPanel.text.getText()); Account fromAccount; Account toAccount; boolean gotmoney=false; try { fromAccount= accounts.getAccount(accountsPanel.from.getSelectedItem()); toAccount= accounts.getAccount(accountsPanel.to.getSelectedItem()); } catch(Exception e) { output.appendText("Account does not exist"); this.dispose(); return true; } try { fromAccount.Withdraw(value.doubleValue()); gotmoney= true; } catch(Exception e) { output.appendText("Unable to withdraw from account"+ fromAccount.Name()); } if(gotmoney) { try { toAccount.Deposit(value.doubleValue()); } catch(Exception e) { output.appendText("Unable to deposit into account"+ toAccount.Name()); try { fromAccount.Deposit(value.doubleValue()); } catch(Exception e1) { output.appendText("Unable to put money back in "+ fromAccount.Name()); } } } this.dispose(); return true; } } return false; } } class NewAccountDialog extends Dialog { Bank bank; NewControlsPanel controls; Button create; public NewAccountDialog(Frame parent, Bank mainBank, TextArea out) { super(parent, new String("Create new account"), true); bank=mainBank; setLayout(new BorderLayout(5,5)); controls=new NewControlsPanel(); add("Center",controls); controls.show(); create=new Button("Create account"); add("South",create); pack(); } public boolean action(Event event, Object obj) { if(event.target==create) { Double interest; Account newAccount; // creation of a new account was requested if(controls.typePanel.accountType.getSelectedItem().equals("Checking")) { // checking account interest=new Double(controls.interestPanel.text.getText()); try { newAccount=new CheckingAccount(controls.namePanel.text.getText(), interest.doubleValue(),bank.output); } catch(Exception e) { bank.output.appendText("Account has no name"); return true; } } else if(controls.typePanel.accountType.getSelectedItem().equals("Savings")) { // savings account interest=new Double(controls.interestPanel.text.getText()); try { newAccount=new SavingsAccount(controls.namePanel.text.getText(), interest.doubleValue(),bank.output); } catch(Exception e) { bank.output.appendText("Account has no name"); return true; } } else if(controls.typePanel.accountType.getSelectedItem().equals("CD")) { // CD account interest=new Double(controls.interestPanel.text.getText()); Double deposit=new Double(controls.depositPanel.text.getText()); Integer period=new Integer(controls.periodPanel.text.getText()); try { newAccount=new CDAccount(controls.namePanel.text.getText(), deposit.doubleValue(), period.intValue(), interest.doubleValue(),bank.output); } catch(Exception e) { bank.output.appendText("Account has no name"); return true; } } else if(controls.typePanel.accountType.getSelectedItem().equals("Loan")) { // Loan account interest=new Double(controls.interestPanel.text.getText()); Double loan=new Double(controls.depositPanel.text.getText()); try { newAccount=new LoanAccount(controls.namePanel.text.getText(), loan.doubleValue(), interest.doubleValue(),bank.output); } catch(Exception e) { bank.output.appendText("Account has no name"); return true; } } // end of "switch" of account creation else return true; // at this moment a new account has been succesfully created! bank.accounts.addAccount(newAccount); // Update the popup menu //if(bank.accounts.size()==1) //bank.accountControls.accountChoice.removeItem("(no accounts)"); bank.accountControls.accountChoice.addItem(newAccount.Name()); bank.accountControls.accountChoice.select(newAccount.Name()); // free the dialog resources this.dispose(); return true; } // end of "create" event else if(event.target==controls.typePanel.accountType) { controls.depositPanel.text.setEditable(false); controls.periodPanel.text.setEditable(false); if(((String)obj).equals("CD")) { controls.depositPanel.text.setEditable(true); controls.periodPanel.text.setEditable(true); } else if(((String)obj).equals("Loan")) { controls.depositPanel.text.setEditable(true); controls.depositPanel.text.setText(""); controls.periodPanel.text.setText(""); controls.interestPanel.text.setText(""); } } // end of action for type Choice return false; } // end of action method } // end of class definition