-4

我被困在为帐户创建对象中,我不知道问题出在哪里。我的输出仅显示最近输入的值。我想查看我创建的打印详细信息(尤其是不同的 id),但我不知道该怎么做。

请检查我的代码,因为我认为我的循环中有一个错误,在某种程度上显示了不需要显示的显示消息。

银行系统.java:

import java.io.*;
import java.util.Scanner;

public class BankSystem {

    public static void main(String[] args)  throws IOException {
        Scanner in = new Scanner(System.in);
        BankAccount account[] = new BankAccount[5];
        BankClient client[] = new BankClient[5];
        BankSystem myBankSystem = new BankSystem();
        String MainMenu;

        do {
            System.out.println("");
            System.out.println("------PLP BANK SYSTEM------");
            System.out.println("Main Menu: ");
            System.out.println("[A]ccount Management" );
            System.out.println("[C]lient Management" );
            System.out.println("[Q]uit" );
            System.out.println("");
            System.out.print("Please select letter: ");
            MainMenu = in.nextLine();

            switch (MainMenu.toLowerCase()) {
                case "a": 
                    System.out.println("");
                    System.out.println("------PLP BANK SYSTEM------");
                    System.out.println("Account Management:" );
                    System.out.println("[N]ew Account" );
                    System.out.println("[A]Apply Interest to All Accounts" );
                    System.out.println("[L]ist All Accounts" );
                    System.out.println("[F]ind an Account" );
                    System.out.println("[D]eposit to an Account" );
                    System.out.println("[W]ithdraw from an Account" );
                    System.out.println("[R]eturn to Main Menu" );
                    System.out.println("");
                    System.out.print("Please select letter: " );
                    String AccountManagement = in.nextLine(); 

                    switch (AccountManagement.toLowerCase()) {
                        case "n": //NEW ACCOUNT    
                            System.out.print("Please input your desired ID number: " );
                            int id = in.nextInt();
                            System.out.print("Please input your desired Balance: " );
                            double balance = in.nextDouble();
                            System.out.print("Please input your desired Interest Rate: " );
                            double interestRate = in.nextDouble();

                            // IS THE PROBLEM IS HERE ON MY LOOP?                                 
                            for (int i=0; i<account.length; i++) {
                                account[i] = new BankAccount(id, balance, interestRate);
                            }        

                            break;                                               
                        case "a": //APPLY INTEREST TO ALL ACCOUNTS
                            System.out.println("For all accounts, compute and compound:" );
                            System.out.println("[M]onthly" );
                            System.out.println("[Q]uarterly" );
                            System.out.println("[A]nnually");
                            System.out.println("[C]ancel");

                            break;                        
                        case "l": //LIST ID NUMBERS OF ALL ACCOUNTS
                            System.out.println("List of all Accounts: ");
                            account[1].printDetails(); // OR THE PROBLEM IS HERE?
                            account[2].printDetails();

                            break;                            
                        case "f": //FIND ACCOUNT
                            System.out.println("Enter ID number: " );

                            break;                            
                        case "d": //DEPOSIT
                            System.out.println("Enter ID number: " );
                            System.out.println("Enter Deposit amount: " );

                            break;                            
                        case "w": //WITHDRAW
                            System.out.println("Enter ID number: " );                              
                            System.out.println("Enter Withdraw amount: " );

                            break;                        
                        case "r": // RETURN
                            break;                      
                    }                                                                 
                case "c":
                    System.out.println("");
                    System.out.println("------PLP BANK SYSTEM------");
                    System.out.println("Client Management:");
                    System.out.println("[N]ew Client" );
                    System.out.println("[L]List All Clients" );
                    System.out.println("[F]ind a Client" );
                    System.out.println("[R]eturn to Main Menu" );
                    System.out.println("");
                    System.out.print("Please select letter: " );
                    String ClientManagement = in.nextLine();

                    switch (ClientManagement.toLowerCase()) {                            
                        case "n": //NEW CLIENT                                
                            System.out.println("Enter ID number: " );
                            System.out.println("Please input your Name: " );
                            System.out.println("Please input your account ID number: " );

                            break;                                
                        case "l": //LIST ALL CLIENT
                            break;                                   
                        case "f": //FIND A CLIENT
                            System.out.println("Enter ID number: " );

                            break;                           
                        case "r":
                            break;                                
                        default: 
                            System.out.println("Invalid entry, Please try again!"); 
                            break;                             
                    }                                                                               
                default: 
                    System.out.println("Invalid entry, Please try again!");  
            }
        } while (!MainMenu.equals("q"));

        System.out.println("Thank you for using my program!"); 
    }
}

银行账户.java:

public class BankAccount {
    private double balance;
    private double interestRate;
    private int id;

    public BankAccount (int id, double initialDeposit, double initialIntRate){
        //Constructor

        this.id=id;
        this.balance=initialDeposit;
        this.interestRate=initialIntRate;      
    }

    public double getBalance(){
        return balance;
    }

    public double getInterestRate(){
        return interestRate;
    }

    public int getIDNumber(){
        return id;
    }

    public void printDetails() {
        System.out.println("ID Number is: " +id );
        System.out.println("Current balance is: "+balance );
        System.out.println("Interest rate is: "+interestRate+"%");
    }

    public double computeMonthlyInterest(){        
        interestRate=balance*interestRate;            
        return interestRate;
    }  

    public void applyMonthlyInterest(){
        this.balance=balance+interestRate;        
    }  

    public void applyQuarterlyInterest(){
        this.balance=balance+(interestRate*3);
    }  

    public void applyAnnualInterest(){
        this.balance=balance+(interestRate*12);        
    }  

    public void deposit(double amount){
        this.balance += amount;
    }

    public boolean withdraw (double amount) {
        if (amount>balance) {
            System.out.println("Withdraw amount is more than balance, Please try again.");
            return false;
        }
        else 
        {
            this.balance -= amount;
            return true;
        }
    }
}
4

2 回答 2

2

你真的应该考虑把你的代码分解成子程序;将其分解将有助于划分不同的功能。

至于你的错误;我认为您是说所有帐户都具有相同的价值,而您并不期望如此。

创建新帐户时,您的代码如下:

for (int i=0; i<account.length; i++) {
    account[i] = new BankAccount(id, balance, interestRate);
}

每次运行该行时,都会将数组中的每个元素替换为新的 BankAccount 对象。

于 2014-10-02T17:27:09.337 回答
0

您的问题是您用最新输入的银行账户填写了整个银行账户数组。

for (int i=0; i<account.length; i++) {
account[i] = new BankAccount(id, balance, interestRate);

}

相反,在收到银行账户信息后,您应该只创建一个新的 BankAccount,并且数组的索引应该是当前银行账户数量 +1。

因此,在 do 语句之前创建一个变量来存储银行账户的当前金额。

int amountOfBankAccounts = 0;

并在 for 的位置使用它并删除 for

if(amountOfBankAccounts < 5) {
  account[amountOfBankAccounts++] = new BankAccount(id, balance, interestRate);
} else {
  System.out.println("Can not create a new account anymore, array is full!");
}

那应该解决它。一旦可行,我建议您花一些时间来创建函数,以使您的代码更具可读性和可重用性。

于 2014-10-02T17:29:30.870 回答