Register FAQ Calendar Search Today's Posts Mark Forums Read

Go Back   Java Programming Forums > Java Programming > New to Java

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-06-2008, 08:34 AM
Junior Member
 
Join Date: May 2008
Posts: 12
crazydeo is on a distinguished road
Exclamation Help using Abstract classes with arrays

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:
Java Code:
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:

Java Code:
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:
Java Code:
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);
 
    }
 
}
ComputeTotal class:
Java Code:
import javax.swing.*;
import java.awt.event.*;
 
public class ComputeTotals extends JFrame implements ActionListener {
 
    Account account = null;
    JPanel mainPanel = new JPanel();
    JTextField nameTextField = new JTextField(15);
    JTextField createPinTextField = new JTextField(4);
    JButton newAccountButton = new JButton("Create an Account");
    JTextField depositAmountTextField = new JTextField(10);
    JTextField withdrawAmountTextField = new JTextField(10);
 
    JTextField existingPinTextField = new JTextField(4);
    JButton depositButton = new JButton("Deposit");
    JButton withdrawButton = new JButton("Withdraw");
 
    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();
        add(mainPanel);
        setTitle("Welcome to Maricon Bank");
        setSize(832,287);
        setVisible(true);
    }
    public void designFrame()
    {
        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(newAccountButton);
        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);
 
        newAccountButton.addActionListener(this);
        depositButton.addActionListener(this);
        withdrawButton.addActionListener(this);
 
 
    }
    public void actionPerformed(ActionEvent evt)
    {
        Object sourceObject = evt.getSource();
 
        if(sourceObject == newAccountButton)
        {
            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():

Java Code:
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.

UPDATE: i've been working on this for the last 2 days.... just stuck now.
Reply With Quote
  #2 (permalink)  
Old 10-06-2008, 04:43 PM
Junior Member
 
Join Date: May 2008
Location: State College, Pennsylvania
Posts: 14
Chris.Brown.SPE is on a distinguished road
Send a message via AIM to Chris.Brown.SPE
Default Re: Help using Abstract classes with arrays

You don't need to create the account object. Have the checking and savings accounts be subclasses of account so they automatically have all of the information in them that accounts has. Then you don't have to worry about transferring it.
Reply With Quote
  #3 (permalink)  
Old 10-06-2008, 04:45 PM
Junior Member
 
Join Date: May 2008
Location: State College, Pennsylvania
Posts: 14
Chris.Brown.SPE is on a distinguished road
Send a message via AIM to Chris.Brown.SPE
Default Re: Help using Abstract classes with arrays

To get the pin number into the class you should just be able to use a setter right? I'm not sure i completely understand the problem.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Custom Search
100 most searched terms
Search Cloud
"bad endpoint type" com.sun.xml.internal.messaging.saaj.soapexceptionimpl com.sun.xml.internal.messaging.saaj.soapexceptionimpl: bad endpoint type com.sun.xml.internal.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception com.sun.xml.internal.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl: message send failed com.sun.xml.internal.messaging.saaj.soapexceptionimpl: message send failed com.sun.xml.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception com.sun.xml.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception: com.sun.xml.messaging.saaj.soapexceptionimpl: message send failed convert arraylist to map date_format_now eclipse shortcut keys ejb3 quartz java convert double to binary java forum java forums java jtextarea bold java jtextarea color java jtextarea font size java programmer forum java programmers forum java programming forum java programming forums java read last line java read last line of file java sendkeys java.security.privilegedactionexception java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl: bad response java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl: message send failed java.security.privilegedactionexception: com.sun.xml.messaging.saaj.soapexceptionimpl: message send failed javapf javaprogrammingforums jtextarea bold jtextarea font jtextarea font color jtextarea font size programing forums programming forums reset jcombobox saaj0008 saaj0008: bad response; bad request saaj0008: bad response; not found severe: saaj0008 severe: saaj0008: bad response severe: saaj0008: bad response; bad request severe: saaj0008: bad response; not found soap java.security.privilegedactionexception soapexceptionimpl: bad endpoint type textpad java

All times are GMT. The time now is 01:40 PM.


Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.