Design and implement an object-oriented program describing two kinds of BANK accounts, FixedFee and ChargeableFee, that differ in the way that fees are charged:
• FixedFee: There is a fixed £5.00 fee at the end of the each month
• ChargeableFee: Each withdrawal costs £0.50. The total fee is calculated and
charged at the end of the month.
The goal for the problem is to use inheritance


I HAVE CREATED A BANK ACCOUNT FOR DEPOSIT AND WITHDRAWL AND THE DETAILS BUT HOW DO I INCLUDE fixed fee and chargeable fee IN MY CODE.....so that is affects the withdrawl

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class GuiAccTest extends Frame implements ActionListener
{
        Label lab=new Label("                                                                                                                                                                 ");
        Label lab1=new Label("                                                                                                                                                                 ");
        TextField t[]=new TextField [5];
        Label l[]=new Label [5];
        Button but=new Button("Create Account");
        Button but1=new Button("Test Account");
        BankAccount b;
        GuiAccTest()
        {
                addWindowListener(new NewWindowAdapter());
                setLayout(new GridLayout(1,10));
                Panel p=new Panel();
                Panel p1=new Panel();
                p.setForeground(new java.awt.Color(0, 0, 0));
                p1.setForeground(Color.black);
                but.addActionListener(this);
                but.setBackground(new java.awt.Color(105, 111, 94));
                but.setFont(new java.awt.Font("badoni mt", 1, 15));
                but1.addActionListener(this);
                but1.setBackground(new java.awt.Color(105, 111, 94));
                but1.setFont(new java.awt.Font("badoni mt", 1, 15));
                p.setLayout(new GridLayout(8,7));
                p1.add(lab1);
                p1.add(lab);
                l[0]=new Label("Name                                      :");
                l[1]=new Label("Account number                   :");
                l[2]=new Label("Initial Balance                       :");
                l[3]=new Label("\nDeposit Amount              :");
                l[4]=new Label("\nWithdraw Amount           :");
                for(int i=0;i<5;i++)
                {
                        t[i]=new TextField(10);
                        p.add(l[i]);
                        p.add(t[i]);
                }
                p.add(but);
                l[0].setFont(new java.awt.Font("badoni mt", 1, 15));
                t[0].setFont(new java.awt.Font("badoni mt", 1, 15));
                l[1].setFont(new java.awt.Font("badoni mt", 1, 15));
                t[1].setFont(new java.awt.Font("badoni mt", 1, 15));
                l[2].setFont(new java.awt.Font("badoni mt", 1, 15));
                t[2].setFont(new java.awt.Font("badoni mt", 1, 15));
                l[3].setFont(new java.awt.Font("badoni mt", 1, 15));
                t[3].setFont(new java.awt.Font("badoni mt", 1, 15));
                l[4].setFont(new java.awt.Font("badoni mt", 1, 15));
                t[4].setFont(new java.awt.Font("badoni mt", 1, 15));
                p.add(but1);
                but1.setVisible(false);
                l[3].setVisible(false);
                l[4].setVisible(false);
                t[3].setVisible(false);
                t[4].setVisible(false);
                add(p);
                add(p1);
        }
        String testAccount(int d_amt,int w_amt)
        {
                String msg;
                b.deposit(d_amt);
                msg="Transaction Succesful";
                try
                {
                        b.withdraw(w_amt);
                }catch(FundsInsufficientException fe)
                {
                        fe=new FundsInsufficientException(b.amount,w_amt);
                        msg=String.valueOf(fe);
                }
                return msg;
        }
        public void actionPerformed(ActionEvent ae)
        {
                String str=ae.getActionCommand();
                if(str.equals("Create Account"))
                {
                        b=new BankAccount(Integer.parseInt(t[1].getText()),Integer.parseInt(t[2].getText()));
                        but1.setVisible(true);
                        l[3].setVisible(true);
                        l[4].setVisible(true);
                        t[3].setVisible(true);
                        t[4].setVisible(true);
                        but.setVisible(false);
                        l[0].setVisible(false);
                        l[1].setVisible(false);
                        l[2].setVisible(false);
                        t[0].setVisible(false);
                        t[1].setVisible(false);
                        t[2].setVisible(false);
                        lab1.setText( "Account : "+b.accnum+", Current Balance : "+b.amount);
                        return;
                }
                else
                {
                        lab.setText(testAccount(Integer.parseInt(t[3].getText()),Integer.parseInt(t[4].getText())));
                        lab1.setText("Account : "+b.accnum+", Current Balance : "+b.amount);
                }
        }
        public static void main(String arg[])
        {
                GuiAccTest at=new GuiAccTest();
                at.setTitle("BANK ACCOUNTS");
                at.setBackground(new java.awt.Color(236, 233, 216));
                at.setSize(1000,300);
                at.setVisible(true);
                at.setLocation(0, 500);
                          }
}
class NewWindowAdapter extends WindowAdapter
{
        public void windowClosing(WindowEvent we)
        {
                System.exit(0);
        }
}
class BankAccount
{
        int accnum;
        int amount;
        BankAccount(int num,int amt)
        {
                accnum=num;
                amount=amt;
        }
        public void deposit(int amt)
        {
                amount=amount+amt;
        }
        public void withdraw(int amt) throws FundsInsufficientException
        {
                if(amt>amount)
                        throw new FundsInsufficientException(amount,amt);
                else
                        amount=amount-amt;
        }
}
class FundsInsufficientException extends Exception
{
        int balance;
        int withdraw_amount;
        FundsInsufficientException(int bal,int w_amt)
        {
                balance=bal;
                withdraw_amount=w_amt;
        }
        public String toString()
        {
                return "Withdraw amount ("+withdraw_amount+") is less than the balance ("+balance+") withdrawal failed .TRY AGAIN";
        }
}

Recommended Answers

All 3 Replies

As the assignment says, you need to use inheritance.

As the assignment says, you need to use inheritance.

yes i have read that...but how do i start....and from which point...like i literally need a start to implement these 2 conditions......

The way to use inheritance is to think what FixedFee and ChargeableFee have in common. Obviously most of the code for the two classes is identical, beside how and when they charge there fees. Assemble all the common information into one class, and use inheritance to only write the changes between the two. What those two have in common? In what they differ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.