Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Java program with abstract class along with two subclasses

  1. #1
    Junior Member
    Join Date
    May 2008
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Java program with abstract class along with two subclasses

    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);
     
        }
     
    }

    ComputeTotal class:
    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():

    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.


  2. #2
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    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.

  3. #3
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    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.

Similar Threads

  1. [SOLVED] Error "TitleChanger is abstract; cannot be instantiated"
    By Uzual in forum AWT / Java Swing
    Replies: 2
    Last Post: May 26th, 2009, 11:23 AM
  2. [SOLVED] How to link two different class?
    By John in forum Object Oriented Programming
    Replies: 11
    Last Post: April 27th, 2009, 02:57 PM
  3. Why output is displaying 0 for calculation in java?
    By tazjaime in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 26th, 2009, 01:18 PM
  4. [SOLVED] Java program using two classes
    By AZBOY2000 in forum Object Oriented Programming
    Replies: 7
    Last Post: April 21st, 2009, 06:55 AM
  5. Problem in merging two java classes
    By madkris in forum Object Oriented Programming
    Replies: 11
    Last Post: March 16th, 2009, 09:02 AM