...

View Full Version : Bank program with abstract class.



alvinleephd
06-03-2008, 04:29 AM
This is what I am supposed to do:
Create application that will allow you to create a new account either savings or checking for your customer. It must allow the customer to deposit as well as withdraw from his account. Assume that user deposits $100 for the checking account and $500 for the savings account when the account is created. You must create array at least 10 customers.
Entry:
• You must create an object array for customers who could have either a savings account or a checking account.
• You must create a menu to ask the user if they wish to create a new account or they wish to deposit/withdraw from an account.
• To create an account - the input you must get from the user is their first name, last name, four pin digit code, want to create a savings account or a checking account.
• You must allow the user to return to the menu.
• If the user chooses to deposit/withdraw option – then you need to ask if they wish to either deposit or withdraw.
• You must then ask the user for his 4 digit pin code
• Make sure it exists in the array

Requirements:
• Savings account and checking account must be subclasses of Account class. You need to think if you need to make the account class an abstract class or not.
• In withdrawals you must charge $0.75 for any withdrawal over $2000 in the savings account and $0.50 for any withdrawal over $750 in the checking account.
• You must not let the customer withdraw, if his withdrawal exceeds his balance

Output:
• If account is created – display pin code, first name, last name, the type of account, the amount in the account.
• If the option chosen was withdrawal/deposit –you will need to display the Pin code number, last name, first name, deposit/withdrawal, charges if incurred, and the balance in the account

I have created an abstract (Account) class along with two subclasses. (Checking and Savings), and a calculation class for output and calculation.

Abstract account class:


public abstract class Account {

protected int pinNumberInteger;
protected double withdrawChargeDouble, totalDouble, depositDouble, withdrawDouble;
protected String nameString;



public Account(){
this("No Account");
}
public Account(String customerName)
{
nameString = customerName;

}

public abstract void calculateAccount();

public String getNameString()
{
return nameString;
}
public double getWithdrawAmount()
{
return withdrawDouble;
}
public double getDepositAmount()
{
return depositDouble;
}
public double getWithdrawCharge()
{
return withdrawChargeDouble;
}
public double getTotal()
{
return totalDouble;
}
public double getPinNumber()
{
return pinNumberInteger;
}


}



Checking class:



public class Checking extends Account{

public Checking() {

super("No Account");

}


public void calculateAccount()
{

if(withdrawDouble > 750)
{
withdrawChargeDouble = .50;
}
else
{
withdrawChargeDouble = .00;
}

totalDouble += (depositDouble - withdrawDouble - withdrawChargeDouble);
}

}


Savings class:



public class Savings extends Account{

public Savings() {
super("No Account");
}
public void calculateAccount()
{

if(withdrawDouble > 2000)
{
withdrawChargeDouble = .75;
}
else
{
withdrawChargeDouble = .00;
}
totalDouble += (depositDouble - withdrawDouble - withdrawChargeDouble);

}

}



public class ComputeTotals extends JFrame implements ActionListener {

Account account = null;
JPanel mainPanel = new JPanel();
JTextField depositAmountTextField = new JTextField(10);
JTextField withdrawAmountTextField = new JTextField(10);
JTextField nameTextField = new JTextField(15);
JTextField existingPinTextField = new JTextField(4);
JTextField createPinTextField = new JTextField(4);
JButton depositButton = new JButton("Deposit");
JButton withdrawButton = new JButton("Withdraw");
JButton createAccountButton = new JButton("Create Account");
JTextArea outputTextArea = new JTextArea("Your Bank Statement:" , 10, 30);
JScrollPane outputScrollPane = new JScrollPane(outputTextArea);
String accountType[] = {"Checking", "Savings"};
JComboBox accountTypeComboBox = new JComboBox(accountType);

String [] customerAccountArray = new String[10] ;
int [] pinNumberArray = new int [10];

int existingPinNumberInteger, desiredPinNumberInteger, counterInteger;
double depositAmountDouble, withdrawAmountDouble;
String nameString;



public static void main(String[] args) {
ComputeTotals myTotals = new ComputeTotals();
myTotals.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public ComputeTotals()
{
designFrame();
}
public void designFrame()
{
mainPanel.add(new JLabel(" Lee Bank "));
mainPanel.add(new JLabel(" Create Account "));
mainPanel.add(new JLabel(" Name: "));
mainPanel.add(nameTextField);
mainPanel.add(new JLabel(" Desired PIN Number "));
mainPanel.add(createPinTextField);
mainPanel.add(new JLabel(" Account Type "));
mainPanel.add(accountTypeComboBox);
mainPanel.add(createAccountButton);
mainPanel.add(new JLabel(" Enter PIN: "));
mainPanel.add(existingPinTextField);
mainPanel.add(new JLabel(" Amount: "));
mainPanel.add(depositButton);
mainPanel.add(depositAmountTextField);
mainPanel.add(withdrawButton);
mainPanel.add(withdrawAmountTextField);
mainPanel.add(outputTextArea);

createAccountButton.addActionListener(this);
depositButton.addActionListener(this);
withdrawButton.addActionListener(this);

add(mainPanel);
setSize(832,287);
setVisible(true);
}
public void actionPerformed(ActionEvent evt)
{
Object sourceObject = evt.getSource();

if(sourceObject == createAccountButton)
{
getInputForCreation();
createAccount();
displayCreatedOutput();
}
else if(sourceObject == withdrawButton || sourceObject == depositButton)
{
getInputForExisting();
calculate();
displayStatementOutput();

}
}

public void getInputForCreation()
{

if (createPinTextField.equals(""))
{
JOptionPane.showMessageDialog(null, "Invalid input!");
}
else
{
desiredPinNumberInteger = Integer.parseInt(createPinTextField.getText());
}

if (nameTextField.equals(""))
{
JOptionPane.showMessageDialog(null, "Invalid input!");
}
else
{
nameString = nameTextField.getText();
}
}

public void getInputForExisting()
{
double depositDouble, withdrawDouble;
depositDouble = account.getDepositAmount();
withdrawDouble = account.getWithdrawAmount();

for(int i = 0; i <= 9; i++)
if(existingPinNumberInteger != pinNumberArray[i] || existingPinTextField.equals(""))
{
JOptionPane.showMessageDialog(null, "This pin number is not in the system");
break;
}
else
{
existingPinNumberInteger = Integer.parseInt(existingPinTextField.getText());
}
if (depositAmountTextField.equals("") && withdrawAmountTextField.equals("") )
{
JOptionPane.showMessageDialog(null, "Invalid input!");
}
else
{
depositDouble = Double.parseDouble(depositAmountTextField.getText());
withdrawDouble = Double.parseDouble(withdrawAmountTextField.getText());

}

}


public void createAccount()
{
for(int i = 0; i <= 9; i++)
{
if(desiredPinNumberInteger == pinNumberArray[i])
{
JOptionPane.showMessageDialog(null, "A customer with this PIN already exists");
break;
}
else
{
pinNumberArray[counterInteger] = desiredPinNumberInteger;
customerAccountArray[counterInteger] = nameString;
System.out.println(desiredPinNumberInteger + nameString);
}
}
}

public void getMethods()
{
if(accountTypeComboBox.getSelectedItem().equals("Checking"))
{
account = new Checking();

}
else
{
account = new Savings();
}
}



public void calculate()
{

if (account != null)
{
account.calculateAccount();
}
}
public void displayCreatedOutput()
{
double totalDouble;
if(accountTypeComboBox.getSelectedItem().equals("Checking"))
{
totalDouble = 100;

}
else
{
totalDouble = 500;
}

outputTextArea.setText("PIN code: " + desiredPinNumberInteger +
'\n' +"Name: " + nameString +
'\n' + "Type of Account:" + accountTypeComboBox.getSelectedItem() +
'\n' + "Amount:" + totalDouble);

}

public void displayStatementOutput()
{
double depositDouble, withdrawDouble, withdrawChargeDouble, totalDouble;
depositDouble = account.getDepositAmount();
withdrawDouble = account.getWithdrawAmount();
withdrawChargeDouble = account.getWithdrawCharge();
totalDouble = account.getTotal();

outputTextArea.setText("PIN code: " + existingPinNumberInteger +
"Name: " + nameString +
"Type of Account:" + accountTypeComboBox.getSelectedItem()
+ "Amount:" + totalDouble);

}


}




I declared the object account as a class variable and used it to get the calculate method from the subclasses. I don't think there is an actual problem with this, but the problem is, I don't know how to send my input over to the checkings and savings subclasses to make the calculation. The input for the withdrawal is given through the withdrawAmountTextField, and I want to initialize the withdrawDouble variable to this. Likewise, I would like to get the input for the depositDouble through depositAmountTextField. I have tried doing this by getting the variables from the other class and then intializing them as the input from the textfields in getInputForExisting():


public void getInputForExisting()
{
double depositDouble, withdrawDouble;
depositDouble = account.getDepositAmount();
withdrawDouble = account.getWithdrawAmount();

for(int i = 0; i <= 9; i++)
if(existingPinNumberInteger != pinNumberArray[i] || existingPinTextField.equals(""))
{
JOptionPane.showMessageDialog(null, "This pin number is not in the system");
break;
}
else
{
existingPinNumberInteger = Integer.parseInt(existingPinTextField.getText());
}
if (depositAmountTextField.equals("") && withdrawAmountTextField.equals("") )
{
JOptionPane.showMessageDialog(null, "Invalid input!");
}
else
{
depositDouble = Double.parseDouble(depositAmountTextField.getText());
withdrawDouble = Double.parseDouble(withdrawAmountTextField.getText());

}

}


I do not know how to send this input over to the other subclasses (Checking and Savings).

Also, I do not know how to associate the pin number with its amount, to create an entire account object which includes the corresponding pin number, name, and amount in the account. I have not done this with multiclasses before and some advice on how to do this would be great.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum