aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes fundamental java question from a novice! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "fundamental java question from a novice!" Watch "fundamental java question from a novice!" New topic
Author

fundamental java question from a novice!

James Lechte
Ranch Hand

Joined: Mar 02, 2001
Posts: 30
The following situation i have relies on fundamentals of programming and at the moment i am a bit stuck!
problem:
8 classes based around a bank system that creates and manages accounts. The bank class handles interaction with all other classes of the bank - ie. account(abstract) savings, cheque whilst remaining classes handle the GUI interface.
When i build the container class i need to add an account to the bank(which stores accounts) then update the JList properly. how is this done effectively?? like container to listener to bank = bank to listener to container?
the prob is in the code for the listener to create account
ie.
getWindow().getList().addElement(theBank.createChequeAccount(accountName);
so this goes off and adds to the vector in the bank, but i have to update the listmodel through a setter?
classes below shows connection:
import java.util.Vector;
import java.util.Iterator;
public class Bank {
private String bankName;
private Vector accounts;
public Bank(String inBankName) {
setVector();
setBankName(inBankName);
}

private void setVector() {
accounts = new Vector();
}
private void setBankName(String inBankName) {
bankName = inBankName;
}

public boolean createSavingsAccount(String inName) {
SavingsAccount theAccount = new SavingsAccount(inName);
accounts.addElement(theAccount);
return true;
}

public boolean createChequeAccount(String inName) {
ChequeAccount theAccount = new ChequeAccount(inName);
accounts.addElement(theAccount);
return true;
}

public double getTotalBalance() {
double totalAmount = 0.0;
Iterator i =accounts.iterator();
while(i.hasNext()) {
Account anAccount =(Account)i.next();
totalAmount += anAccount.getAccountBalance();
}
return totalAmount;
}

public boolean verifyAccount(String inAccountNumber) {
Iterator i = accounts.iterator();
while(i.hasNext()) {
Account anAccount = (Account)i.next();
if (anAccount.getAccountNumber().equals(inAccountNumber))
return true;
}
return false;
}

public void deposit(String inAccountNumber, double inAmount) {
Iterator i = accounts.iterator();
while(i.hasNext()) { //loops vector
Account anAccount = (Account)i.next();
if(anAccount.getAccountNumber().equals(inAccountNumber))
anAccount.deposit(inAmount);
}
}
public boolean withdraw(String inAccountNumber, double inAmount) {
Iterator i = accounts.iterator();
while(i.hasNext()) {
Account anAccount = (Account)i.next();
if(anAccount.getAccountNumber().equals(inAccountNumber)) {
if(anAccount.withdraw(inAmount))
return true;
}
}
return false;
}
public String accountsTally() {
Iterator i = accounts.iterator();
int savings = 0;
int cheque = 0;
while(i.hasNext()) {
Account anAccount = (Account)i.next();
if(anAccount instanceof SavingsAccount)
savings++;
else if(anAccount instanceof ChequeAccount)
cheque++;
}
String tally = "The number of Savings Accounts: " + savings + "\n";
tally += "The number of Cheque Accounts: " + cheque + "\n";
return tally;
}
public String accountDetails(String inAccountNumber) {
String theString = "";
Iterator i = accounts.iterator();
while(i.hasNext()) {
Account anAccount = (Account)i.next();
if(anAccount.getAccountNumber().equals(inAccountNumber))
theString += anAccount.toString();
}
return theString;
}
}

import java.util.Vector;
import java.io.*;
public class ChequeAccount extends Account {
private String accountOwner;
private double accountBalance;
private Vector transactions;
private int transactionCount = 0;
public ChequeAccount(String inAccountOwner) {
super();
setAccountBalance(0.0);
setAccountOwner(inAccountOwner);
transactions = new Vector();
setFirstTransaction(0.0);
}
public ChequeAccount(String inAccountOwner, double inBalance) {
super();
setAccountBalance(inBalance);
setAccountOwner(inAccountOwner);
transactions = new Vector();
setFirstTransaction(inBalance);
}

private void setAccountOwner(String inAccountOwner) {
accountOwner = inAccountOwner;
}

public void setAccountBalance(double amount) {
accountBalance = amount;
}

public String getAccountOwner() {
return accountOwner;
}

public double getAccountBalance() {
return accountBalance;
}

public String getAccountNumber() {
return super.getAccountNumber();
}

private void setFirstTransaction(double inBalance) {
String transactionType = "Initial Setup Balance";
Transaction inTransaction = new Transaction(inBalance, inBalance, transactionType);
transactions.addElement(inTransaction);
}
public void deposit(double inAmount) {
String type = "Deposit";
accountBalance += inAmount;
Transaction inTransaction = new Transaction(inAmount, accountBalance, type);
transactions.addElement(inTransaction);
}

public boolean withdraw(double inAmount) {
String type = "Withdrawal";
accountBalance -= inAmount;
Transaction inTransaction = new Transaction(inAmount, accountBalance, type);
transactions.addElement(inTransaction);
return true;
}
public String toString() {
return "Bank account no: " + getAccountNumber() +"\n" + "Account Name: " +
accountOwner + "\n" + "Current account balance: $" + accountBalance +"\n"
+ transactions.toString();
}

}

public abstract class Account {
private static int accountCreated =1;
private String accountNumber;

public Account() {
setAccountNumber();
}
public void setAccountNumber() {
Integer inAccountNumber = new Integer(accountCreated++);
accountNumber = inAccountNumber.toString();
}
public String getAccountNumber() {
return accountNumber;
}
abstract boolean withdraw(double inAmount);
abstract double getAccountBalance();
abstract void deposit(double inAmount);
}

import corejava.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class GuiInterface extends JCloseableFrame {
private JList accountList;
private DefaultListModel accountListModel;
private JButton addSavingsButton, addChequeButton;
private JTextField accountName, numSavings, numCheques;
private JLabel savings, cheque;
/////////////////////////////////////////////////////////////////////////////
public GuiInterface () {
super ("First National Bank Program");
setSize(420,400);
accountListModel = new DefaultListModel();
accountList = new JList(accountListModel);
accountList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
accountList.setSelectedIndex(0);
JScrollPane listScrollPane = new JScrollPane(accountList);

addSavingsButton = new JButton("Create Savings Account");
addSavingsButton.addActionListener(new SavingsListener());
addChequeButton = new JButton("Create Cheque Account");
addChequeButton.addActionListener(new ChequeListener());
accountName = new JTextField(20);
numSavings = new JTextField(5);
numCheques = new JTextField(5);
savings = new JLabel("Number of Savings accounts");
cheque = new JLabel("Number of Cheque Accounts");
JPanel buttonPane = new JPanel();
buttonPane.add(addSavingsButton);
buttonPane.add(addChequeButton);
JPanel bottomPane = new JPanel();
bottomPane.setLayout(new GridLayout(2,2));
bottomPane.add(accountName);
bottomPane.add(numSavings);
bottomPane.add(numCheques);
bottomPane.add(savings);
bottomPane.add(cheque);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(bottomPane, BorderLayout.SOUTH);
contentPane.add(buttonPane, BorderLayout.NORTH);
contentPane.add(listScrollPane, BorderLayout.CENTER);
}

public static void main (String args[]) {
JFrame frame = new GuiInterface();
frame.pack();
frame.setVisible(true);
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: fundamental java question from a novice!