I know this probably doesn't make any sense, but I'm going to try. I have a GUI I created for a checkbook balancing program. And I have an account class with a few extended classes (savingsAccount, checkingAccount, etc). I want to use the GUI I created with the account classes mentioned above. For example when a user enters a deposit amount, it'll pass through the Account class and I want it to return the balance into the textbox in the GUI... Do I need another class or can I just add methods to the GUI? God, I'm confusing myself. Any help's appreciated, guys... Thanks!

Wait...the GUI already extends JFrame, so I couldn't extend the Account class too. Right?

I don't know if you guys need the code or not, but I'm posting it in hopes of getting some responses before I go to bed for the night.

//GUI Code

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JOptionPane;

public class AccountGUI extends JFrame
{
    private JTextField sbal;
    private JTextField deposit;
    private JTextField check;
    private JTextField cbal;
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;

    public AccountGUI()
    {
        super("Texting Fields");
        setLayout(new FlowLayout());
        
        label1 = new JLabel("Beginning Balance");
        add(label1);

        sbal = new JTextField("Enter Starting Balance: ");
        add(sbal);
        
        label2 = new JLabel("Deposit Amount");
        add(label2);

        deposit = new JTextField("Enter Deposit Amount: ");
        add(deposit);
        
        label3 = new JLabel("Check Amount");
        add(label3);
        
        check = new JTextField("Enter Check Amount: ");
        add(check);
        
        label4 = new JLabel("Ending Balance");
        add(label4);

        cbal = new JTextField("Ending Balance: ");
        cbal.setEditable(false);
        add(cbal);

        TextFieldHandler handler = new TextFieldHandler();
        sbal.addActionListener(handler);
        deposit.addActionListener(handler);
        check.addActionListener(handler);
    }
    private class TextFieldHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            String string = "";

            if(event.getSource() == sbal)
                string = String.format("sbal: %s", event.getActionCommand());

            else if (event.getSource() == deposit)
                string = String.format("deposit: %s", event.getActionCommand());

            else if (event.getSource() == check)
                string = String.format("check: %s", event.getActionCommand());

            JOptionPane.showMessageDialog(null, string);
        }
    }
}




//Account classes: Account, SavingsAccount, CheckingAccount

public abstract class Account
{
        private double balance;

        public Account()
        {
                setBalance(0);
        }

        public Account(double bal)
        {
                setBalance(bal);
        }

        public void setBalance(double bal)
        {
                if (balance<0)
                        balance = 0;
                else
                        balance = bal;
        }

        public double getBalance()
        {
                return balance;
        }

        public abstract boolean debit(double amount);

        public abstract void credit(double amount);

}


public class CheckingAccount extends Account
{
        private double fee;

        public CheckingAccount(double bal, double f)
        {
                super(bal);
                setFee(f);
        }

        public void setFee(double f)
        {
                fee = f;
        }

        public double getFee()
        {
                return fee;
        }

        public boolean debit(double amount)
        {       if(getFee() >= 0)
                {
                        if(((amount + getFee()) > getBalance()) || (amount<0))
                                return false;
                        else
                        {
                                setBalance(getBalance() - (amount + getFee()));
                                return true;
                        }
                }
                else
                        return false;
        }

        public void credit(double amount)
        {
                if(amount> 0)
                        setBalance(getBalance() + amount);
        }
}

public class SavingsAccount extends Account
{
        private double interestRate;

        private double interest;

        public SavingsAccount(double bal, double interestRate)
        {
                super(bal);
                setInterestRate(interestRate);
        }

        public void setInterestRate(double rate)
        {
                interestRate = rate;
        }

        public double getInterestRate()
        {
                return interestRate;
        }

        public double calculateInterest()
        {
                interest = (getInterestRate()) * getBalance();
                return interest;

        }

        public boolean debit(double amount)
        {
                if((amount > getBalance()) || (amount<0))
                        return false;
                else
                {
                        setBalance(getBalance() - amount);
                        return true;
                }
        }

        public void credit(double amount)
        {
                if(amount> 0)
                        setBalance(getBalance() + amount);
        }
}

Really guys, ANY help would be better than none. Someone said I need to create a new object to extend from (or something like that). I really have no idea what they meant. At this point, I'm thinking this is really simple, and I'm just incredibly stupid. :(

I've included a screen shot of my window. This is what is supposed to happen: A user inputs a value in the Starting Balance textbox. It is stored as an Account using the setBalance() function, and shows up in the Ending Balance textbox. Then when a user enters a deposit, it is added to the account via the credit() function, and the updated balance is shown in the Ending Balance textbox. And finally, when a user enters a check, the amount is deducted via the debit() function and the updated balance is shown in the Ending Balance textbox.

Thank you so much for replying. I've been working on this fruitlessly for the past four hours. I attempted to talk to my professor today, but he was "out to lunch". I've sent him an email, but, of course, no response.

Attachments asst9pic.jpg 19.04 KB
This article has been dead for over six months. Start a new discussion instead.