-3
public abstract class BankAccount
{

    private String firstName;
    private String surname;
    private static int nxtID = 1000;
    private int accNo;
    private String address;
    private String phnNo;
    protected double overdraftLimit;
//    private PassBookAccount p;
//    private InvestmentAccount i;




    public BankAccount(String f,String l,String ad,String p)
    {
        this.accNo = nxtID++;
        this.firstName = f;
        this.surname = l;
        this.address = ad;
        this.phnNo = p;
        this.overdraftLimit = 1000;


    }


    public String getfName()
    {
        return this.firstName;
    }

    public String getsName()
    {
        return this.surname;
    }

    public String getPhn()
    {
        return this.phnNo;
    }

    public int getAccNo()
    {
        return this.accNo;
    }

    public String getAddress()
    {
        return this.address;
    }


    public abstract void withdraw(double d);
    public abstract void deposit(double d);

    public void updateOverDraft(double ol)
    {
        this.overdraftLimit = ol;
    }

    @Override
    public String toString()
    {
        return "\n#Account NO\t\t#First Name\t\t#Surname\t\t#Phone No\t\t#Address\n" + this.accNo+ "\t\t" +this.firstName
                +"\t\t" + this.surname + "\t\t" + this.phnNo + "\t\t" + this.address ;
    }

}

public abstract class SavingsAccount extends BankAccount
{


    private double WITHDRAW_FEE = 5.00;


    public SavingsAccount(String f,String l,String ad,String p)
    {
        super(f,l,ad,p);

    }

    public abstract void addInterest();


    public double getWithFee()
    {
       return this.WITHDRAW_FEE;
    }


}

public class ChequeAccount extends BankAccount
{

     private double openingbalance;
    //private double overdraftLimit;
    private double currentBal;


    public ChequeAccount(String f,String s,String ad,String p,double op,double ol)
    {
        super(f,s,ad,p);
        this.openingbalance = op;
        if(ol > super.overdraftLimit)
        {
            super.overdraftLimit = ol;
        }
        this.currentBal = this.openingbalance;
    }

    public double getCurrentBal() {
        return currentBal;
    }

    public void setCurrentBal(double currentBal) {
        this.currentBal = currentBal;
    }

    @Override
    public void deposit(double d)
    {
        this.currentBal += d;
        JOptionPane.showMessageDialog(null, "Your account balance is $:" + this.currentBal );
    }

    @Override
    public void withdraw(double d)
    {
        double maxOverDraft = -(super.overdraftLimit);
        double tempBal = this.currentBal;

        tempBal -= d;
        if(tempBal >= maxOverDraft)
        {
            this.currentBal  = tempBal;
            JOptionPane.showMessageDialog(null, "Your account balance is $:" + this.currentBal);
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Your don't have sufficient money!!");
        }




    }

     public String toString()
    {
        return super.toString() + "\nCurrent Balance:" + this.currentBal + "\nOverdraft  Limit:" + this.overdraftLimit+"\n";
    }

}

public class PassBookAccount extends SavingsAccounts
{

     private double passInterest = 0.05;
    private double openingBlance;
    private double currentBalance;


    public PassBookAccount(String f,String l,String ad,String p,double op)
    {
        super(f,l,ad,p);
        this.openingBlance = op;
        this.currentBalance = this.openingBlance;

    }

    @Override
    public void addInterest()
    {
        this.currentBalance += this.currentBalance * this.passInterest; 
    }

    public double getBalance()
    {
        return this.openingBlance;
    }

    @Override
    public void deposit(double d)
    {
        this.currentBalance += d;
        JOptionPane.showMessageDialog(null, "Your account balance is $:" + this.currentBalance );
    }

    @Override
    public void withdraw(double d)
    {
        if(this.currentBalance > d)
        {
            this.currentBalance -= d + super.getWithFee();
            JOptionPane.showMessageDialog(null, "Your account balance is $:" + this.currentBalance );
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Your don't have sufficient money!!");
        }
    }

    public double getPassInt()// retun PassbookAccount interest rate;
    {
        return this.passInterest;
    }


    public String toString()
    {
        return super.toString() + "\nCurrent Balance:" + this.currentBalance + "\n";
    }

}

public class InvestmentAccount extends SavingsAccount
{

    private double investInterest = 0.06;
    private double openingBlance;
    private double currentBal;
    private double minDep;
    private double minWith;


    public InvestmentAccount(String f,String s,String ad,String p,double op)
    {
        super(f,s,ad,p);
        this.openingBlance = op;
        this.currentBal = this.openingBlance;
        this.minDep = 1000;
        this.minWith = 1000;
    }


    @Override
    public void deposit(double d)
    {
        this.currentBal += d;
        JOptionPane.showMessageDialog(null, "Your account balance is $:" + this.currentBal );
    }

    @Override
    public void withdraw(double d)
    {
        if(this.currentBal > d)
        {
            this.currentBal -= d + super.getWithFee();
            JOptionPane.showMessageDialog(null, "Your account balance is $:" + this.currentBal );
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Your don't have sufficient money!!");
        }
    }

    @Override
    public void addInterest()
    {
        if(this.currentBal > 100000)
        {
            this.investInterest = 0.08;
            this.currentBal += this.currentBal * this.investInterest; 
        }
        else
        {
            this.currentBal += this.currentBal * this.investInterest; 
        }
    }

    public double getMinDep()
    {
        return this.minDep;
    }

    public double getMinWith()
    {
        return this.minWith;
    }

    public double getInvInt() //return Investment account interest rate
    {
        return this.investInterest;
    }


    public String toString()
    {
        return super.toString() + "\nCurrent Balance:" + this.currentBal + "\n";
    }

}

import javax.swing.*;
import java.util.*;
public class Interface
{

     private static ArrayList<BankAccount> bnk = new ArrayList<BankAccount>();


    private static  PassBookAccount p;
    private static  InvestmentAccount invs;
    private static  ChequeAccount ch;

    public static void main(String[] args) 
    {
        int choice  = mainMenu();

        while(choice != 8)
        {
            switch (choice)
            {
                case 1: 
                    int choice2 = createAccount();
                    subMenu(choice2);
                    break;
                case 2:
                    deposit();
                    break;
                case 3:
                    withdraw();
                    break;
                case 4:
                    updateOverdraft();
                    break;
                case 5:
                    addInterest();
                    break;
                case 6:
                    printBalance();
                    break;
                case 7:
                    printAll();
                    break;
                case -99:
                    JOptionPane.showMessageDialog(null, "You must Enter value 1 - 8",
                                                  "Error", JOptionPane.ERROR_MESSAGE);
                    break;
                case -100:
                    JOptionPane.showMessageDialog(null, "You clicked cancel");
                    break;
                case -101:
                    JOptionPane.showMessageDialog(null, "You must Enter value 1 - 8",
                                                  "Error", JOptionPane.ERROR_MESSAGE);
                    break;
                default:
                    JOptionPane.showMessageDialog(null, "Error - enter value again",
                                                  "Error", JOptionPane.ERROR_MESSAGE);
                    break;
            }//end switch
            choice  = mainMenu();
        }//end while  
        System.exit(0);
    }


    public static int mainMenu()
    {

        String menu = "Chadstone Community bank Menu\n\n1. Create Account\n2. Deposite\n3. Withdraw\n4. Update Overdrafed\n5. Add Interest\n.6 Print Balance\n7. Print All\n8. Quit ";
        String userSelection = JOptionPane.showInputDialog(null, menu, "Chadstone Community Bank",
                                                           JOptionPane.INFORMATION_MESSAGE);
        int option = validateSelection(userSelection);
        return option;
    }


    public static int validateSelection(String userSelection)
    {
        //clicked cancel btn
        if(userSelection == null)

            return -100;
        //click enter without any value - zero   
        if(userSelection.length() < 1)

            return -99;
        //entered more than 1 character
        if(userSelection.length() > 1)
            return -101;
        //entered less than 1 or greater than 8
        if((int)userSelection.charAt(0) < 49 || (int)userSelection.charAt(0) > 56 )
            return -101;
        else
            return Integer.parseInt(userSelection);

    } 

    //sub menu
    public static int createAccount()
    {

        String subMenu = "Chadstone Community bank Menu\n\n1. Create Cheque account\n2. Create PassBook account\n3. Create Investment account\n4. Return to Main Menu";
        String userSelection2 = JOptionPane.showInputDialog(null,subMenu, "Chadstone Community Bank",
                                                           JOptionPane.INFORMATION_MESSAGE);
        int option2 = validateSelection(userSelection2);
        return option2;
    }

    //Deposit cash to an account
    public static void deposit()
    {
         String strAcc = JOptionPane.showInputDialog("Please Enter The Account number:");
        while(!(Validation.validateAccountNo(strAcc)))
        {
            strAcc = JOptionPane.showInputDialog("Error!!\nRe-Enter the Account Number");
        }
        int acc = Integer.parseInt(strAcc);

        String strDep = JOptionPane.showInputDialog("Enter the Deposit amount");
         while(!(Validation.validateDeposit(strDep)))
        {
            strDep = JOptionPane.showInputDialog("Error!!\nRe-Enter the Deposit amount");
        }
        double amount = Double.parseDouble(strDep);
        int i = 0;
        boolean flag =  false;

        if(bnk.isEmpty())
        {
            JOptionPane.showMessageDialog(null, "There are no accounts created");
        }
        else
        {
            for(BankAccount b: bnk)
            {

                if(b.getAccNo() == acc)
                {
                    b = bnk.get(i);
                    if(b instanceof ChequeAccount)
                    {
                        b.deposit(amount);
                    }
                    else if(b instanceof PassBookAccount)
                    {
                        b.deposit(amount);
                    }
                    else
                    {
                        b.deposit(amount);
                    }
                    flag = true;
                    break;

                }
                else
                {
                    i++;
                }

            }
            if(flag == false)
            {
                JOptionPane.showMessageDialog(null, "Wrong Account number!!");

            }
        }

    }

    //Withdraw cash from an account
    public static void withdraw()
    {
        String strAcc = JOptionPane.showInputDialog("Please Enter The Account number:");
        while(!(Validation.validateAccountNo(strAcc)))
        {
            strAcc = JOptionPane.showInputDialog("Error!!\nRe-Enter the Account Number");
        }
        int acc = Integer.parseInt(strAcc);

        String strWith = JOptionPane.showInputDialog("Enter the withdraw amount");
         while(!(Validation.validateWithdraw(strWith)))
        {
            strWith = JOptionPane.showInputDialog("Error!!\nRe-Enter the Withdraw amount");
        }
        double withdraw = Double.parseDouble(strWith);
        int i = 0;
        boolean flag =  false;

        if(bnk.isEmpty())
        {
            JOptionPane.showMessageDialog(null, "There are no accounts created");
        }
        else
        {
            for(BankAccount b: bnk)
            {

                if(b.getAccNo() == acc)
                {
                    b = bnk.get(i);
                    if(b instanceof ChequeAccount)
                    {
                        b.withdraw(withdraw);
                    }
                    else if(b instanceof PassBookAccount)
                    {
                        b.withdraw(withdraw);
                    }
                    else
                    {
                         b.withdraw(withdraw);
                    }
                    flag = true;
                    break;

                }
                else
                {
                    i++;
                }

            }
            if(flag == false)
            {
                JOptionPane.showMessageDialog(null, "Wrong Account number!!","Error",JOptionPane.ERROR_MESSAGE);

            }
        }

    }

    //update the overdraftlimit
    public static void updateOverdraft()
    {
       String strAcc = JOptionPane.showInputDialog("Please Enter The Account number:");
        while(!(Validation.validateAccountNo(strAcc)))
        {
            strAcc = JOptionPane.showInputDialog("Error!!\nRe-Enter the Account Number");
        }
        int acc = Integer.parseInt(strAcc);

        String strUpOverDrft = JOptionPane.showInputDialog("Enter the new Overdraft limit");
        while(!(Validation.validateOverDraftLimit(strUpOverDrft)))
        {
            strUpOverDrft = JOptionPane.showInputDialog("Error!!\nRe-Enter the Overdraft limit");
        }
        double ol =  Double.parseDouble(strUpOverDrft);

        int i = 0;
        boolean flag =  false;

        if(bnk.isEmpty())
        {
           JOptionPane.showMessageDialog(null, "There are no accounts created","Error",JOptionPane.ERROR_MESSAGE);
        }
        else
        {
            for(BankAccount b: bnk)
            {
               if(b.getAccNo() == acc)
               {
                  b = bnk.get(i);
                  if(b instanceof ChequeAccount)
                  {
                      b.updateOverDraft(ol);
                  }
                  flag = true;
                  break;

                }
                else
                {
                  i++;
                }

              }

            if(flag == false)
            {
                JOptionPane.showMessageDialog(null, "Wrong Account number!!","Error",JOptionPane.ERROR_MESSAGE);

            }
        }
    }

    //adding interest to all savings accounts
    public static void addInterest()
    {
        for(int i = 0; i < bnk.size(); i++)
        {
            BankAccount b = bnk.get(i);
            if(b instanceof PassBookAccount)
            {
                p.addInterest();

            }
            else if(b instanceof InvestmentAccount)
            {
                invs.addInterest();
            }
        }
    }

    //Print only a single account
    public static void printBalance()
    {
       String strAcc = JOptionPane.showInputDialog("Please Enter The Account number:");
        while(!(Validation.validateAccountNo(strAcc)))
        {
            strAcc = JOptionPane.showInputDialog("Error!!\nRe-Enter the Account Number");
        }
        int acc = Integer.parseInt(strAcc);
        int i = 0;
        boolean flag =  false;

        if(bnk.isEmpty())
        {
            JOptionPane.showMessageDialog(null, "There are no accounts created","Error",JOptionPane.ERROR_MESSAGE);
        }
        else
        {
          for(BankAccount b: bnk)
          {

             if(b.getAccNo() == acc)
             {
                  b = bnk.get(i);
                  if(b instanceof ChequeAccount)
                  {
                      JTextArea txt = new JTextArea("#ChequeBookAccount#\n"+b.toString());
                      JOptionPane.showMessageDialog(null,txt);
                  }
                  else if(b instanceof PassBookAccount)
                  {
                      JTextArea txt = new JTextArea("#PassBookAccount\n"+b.toString());
                      JOptionPane.showMessageDialog(null,txt);
                  }
                  else if(b instanceof InvestmentAccount)
                  {
                      JTextArea txt = new JTextArea("#InvestmentAccount#\n"+b.toString());
                      JOptionPane.showMessageDialog(null,txt);

                  }
                  flag = true;
                  break;
            }
             else
             {
                i++;
             }     
          }
          if(flag == false)
          {
             JOptionPane.showMessageDialog(null, "Wrong Account number!!","Error",JOptionPane.ERROR_MESSAGE);

          }
        }

    }

    //Print all the account in the arraylist
    public static void printAll()
    {
        String cheque = "";
        String passBook = "";
        String investment = "";

        for(int i = 0; i < bnk.size(); i++) 
        {
            BankAccount b = bnk.get(i);
            if(b instanceof ChequeAccount)
            {
                cheque += b.toString(); 
            }
            else if(b instanceof PassBookAccount)
            {
                passBook += b.toString();
            } 
            else
            {
                investment += b.toString();
            }
        }
        JTextArea txt = new JTextArea("--ChequeAccounts--\n"+cheque+"--PassBookAccount--\n"+passBook+"--InvestmentAccounts--\n"+investment);
        JOptionPane.showMessageDialog(null,txt);
    }

    //Create Cheque Account
    public static void chequeAccount ()
    {
        String fname = JOptionPane.showInputDialog("Enter the first name:");
        while(!(Validation.validateAccountName(fname)))//Validte First name
        {
            fname = JOptionPane.showInputDialog("Error!!\nRe-Enter the first name");
        }

        String sname = JOptionPane.showInputDialog("Enter the surname:");
        while(!(Validation.validateAccountName(fname)))//Validate Surname
        {
            fname = JOptionPane.showInputDialog("Error!!\nRe-Enter the last name");
        }

        String ad = JOptionPane.showInputDialog("Enter the Address:");
        while(!(Validation.validateAddress(ad)))//Validate Address
        {
            ad = JOptionPane.showInputDialog("Error!!\nRe-Enter the Address:");
        }

        String phn = JOptionPane.showInputDialog("Enter the phn number:");
        while(!(Validation.validatePhoneNo(phn)))//Validate phn No
        {
            phn = JOptionPane.showInputDialog("Error!!\nRe-Enter the phn number:");
        }

        String OBalance = JOptionPane.showInputDialog("Enter the Opening Balance");
        while(!(Validation.validationOpeningBalance(OBalance)))//Validate Opening Balance
        {
            OBalance = JOptionPane.showInputDialog("Error!!\nRe-Enter the Opening Balance");
        }
        double b = Double.parseDouble(OBalance);

        String overDraft = JOptionPane.showInputDialog("Enter the Overdraft Limit");
        while(!(Validation.validateOverDraftLimit(overDraft)))//Validate Overdraft limit
        {
            overDraft = JOptionPane.showInputDialog("Error!!\nRe-Enter the Overdraft Limit");
        }
        double ol = Double.parseDouble(overDraft);

        bnk.add(new ChequeAccount(fname,sname,ad,phn,b,ol));//adding entered data to the array list
        JOptionPane.showInputDialog(null,"Account Created Successfully!!","Successfull",JOptionPane.INFORMATION_MESSAGE);
    }

    //Create PassBook Account
    public static void passBookAccount ()
    {
         String fname = JOptionPane.showInputDialog("Enter the first name:");
        while(!(Validation.validateAccountName(fname)))//Validte First name
        {
            fname = JOptionPane.showInputDialog("Error!!\nRe-Enter the first name");
        }

        String sname = JOptionPane.showInputDialog("Enter the surname:");
        while(!(Validation.validateAccountName(fname)))//Validate Surname
        {
            fname = JOptionPane.showInputDialog("Error!!\nRe-Enter the last name");
        }

        String ad = JOptionPane.showInputDialog("Enter the Address:");
        while(!(Validation.validateAddress(ad)))//Validate Address
        {
            ad = JOptionPane.showInputDialog("Error!!\nRe-Enter the Address:");
        }

        String phn = JOptionPane.showInputDialog("Enter the phn number:");
        while(!(Validation.validatePhoneNo(phn)))//Validate phn No
        {
            phn = JOptionPane.showInputDialog("Error!!\nRe-Enter the phn number:");
        }

        String OBalance = JOptionPane.showInputDialog("Enter the Opening Balance");
        while(!(Validation.validationOpeningBalance(OBalance)))//Validate Opening Balance
        {
            OBalance = JOptionPane.showInputDialog("Error!!\nRe-Enter the Opening Balance");
        }
        double b = Double.parseDouble(OBalance);

        bnk.add(new PassBookAccount(fname,sname,ad,phn,b));//adding entered data to the array list
        JOptionPane.showInputDialog(null,"Account Created Successfully!!","Successfull",JOptionPane.INFORMATION_MESSAGE);
    }

    //Create Investment Account
    public static void  investmentAccount()
    {
         String fname = JOptionPane.showInputDialog("Enter the first name:");
        while(!(Validation.validateAccountName(fname)))//Validte First name
        {
            fname = JOptionPane.showInputDialog("Error!!\nRe-Enter the first name");
        }

        String sname = JOptionPane.showInputDialog("Enter the surname:");
        while(!(Validation.validateAccountName(fname)))//Validate Surname
        {
            fname = JOptionPane.showInputDialog("Error!!\nRe-Enter the last name");
        }

        String ad = JOptionPane.showInputDialog("Enter the Address:");
        while(!(Validation.validateAddress(ad)))//Validate Address
        {
            ad = JOptionPane.showInputDialog("Error!!\nRe-Enter the Address:");
        }

        String phn = JOptionPane.showInputDialog("Enter the phn number:");
        while(!(Validation.validatePhoneNo(phn)))//Validate phn No
        {
            phn = JOptionPane.showInputDialog("Error!!\nRe-Enter the phn number:");
        }

        String OBalance = JOptionPane.showInputDialog("Enter the Opening Balance");
        while(!(Validation.validationOpeningBalance(OBalance)))//Validate Opening Balance
        {
            OBalance = JOptionPane.showInputDialog("Error!!\nRe-Enter the Opening Balance");
        }
        double b = Double.parseDouble(OBalance);

        bnk.add(new InvestmentAccount(fname,sname,ad,phn,b));//adding entered data to the array list
        JOptionPane.showInputDialog(null,"Account Created Successfully!!","Successfull",JOptionPane.INFORMATION_MESSAGE);
    }

    //Sub menu
    public static void subMenu(int choice2)
    {
        while(choice2 != 4)
        {
            switch (choice2)
            {
                case 1:
                    chequeAccount ();
                    break;
                case 2:
                    passBookAccount ();
                    break;
                case 3:
                    investmentAccount();
                    break;
                case -99:
                    JOptionPane.showMessageDialog(null, "You must Enter value 1 - 4",
                                          "Error", JOptionPane.ERROR_MESSAGE);
                break;
                case -100:
                    JOptionPane.showMessageDialog(null, "You clicked cancel",
                                          "Error", JOptionPane.ERROR_MESSAGE);
                break;
                case -101:
                    JOptionPane.showMessageDialog(null, "You entered too many characters",
                                          "Error", JOptionPane.ERROR_MESSAGE);
                break;
               default:
                    JOptionPane.showMessageDialog(null, "Error - enter value again",
                                          "Error", JOptionPane.ERROR_MESSAGE);
                break;    

            }
            choice2 = createAccount();

        }

    }


}
4

2 回答 2

1

要回答您的具体问题:

if(b instanceof PassBookAccount)
{
    // At this point you know b is an instance of PassBookAccount

    PassBookAccount pb = (PassBookAccount) b;

    // call methods on pb
}

但是不清楚你的意思是什么,public class interface所以我只是假设你的意思是public class MyMainClass.

于 2012-06-03T03:09:39.143 回答
0

您应该能够创建接口类的对象,(尽管我不知道您为什么要这样命名)返回存储在数组中的值并addInterest()通过参数将值传递给方法。那就是如果我理解这个问题。

于 2012-06-03T03:51:48.200 回答