为什么我的构造函数被“不应”填充

时间:2018-09-21 04:10:33

标签: java

首先,对于广泛的标题,我感到抱歉,我不确定如何措辞。无论如何,我正在制定一个允许多个人拥有多个帐户的银行计划。但是,每当一个新人创建一个帐户时,都会将该帐户添加到其他所有人中,这就是为什么。我已经尽力想了一切,但没有运气。我知道可能不需要管理类,但这是我尝试解决的问题之一。另外,最后的两节课用于支票和储蓄帐户,一旦解决此问题,我将完成这些课程。 任何帮助都感激不尽:)

import java.util.ArrayList;
import javax.swing.*;

class Bank implements Comparable {

private double bal;
private int accountNumber;
private String accountName;

public Bank() {
}

void display_details() {
    System.out.println("Account Number : " + accountNumber);
    System.out.println("Account Balance : " + bal + "\n\n\n");
    JOptionPane.showMessageDialog(null,
            "Account Name:" + accountName + "\nAccount Number : " + accountNumber + "\nAccount Balance : " + bal,
            "You own " + FinancialFolk.Bank.size() + " account[s]", JOptionPane.INFORMATION_MESSAGE);
}

public void deposit(double depositAmount) {
    bal += depositAmount;
    JOptionPane.showMessageDialog(null, "Your new balance is: $" + bal);
}

public void withdraw(double withdrawAmount) {
    if (withdrawAmount > bal) {
        JOptionPane.showMessageDialog(null, "Insufficient Funds");
    } else {
        bal -= withdrawAmount;
        JOptionPane.showMessageDialog(null, "Your new balance is: $" + bal);
    }
}

public void transfer() {

}

@Override
public int compareTo(Object arg0) {
    // TODO Auto-generated method stub
    return 0;
}

public double getBal() {
    return bal;
}

public void setBal(double baL) {
    bal = baL;
}

public int getAccountNumber() {
    return accountNumber;
}

public void setAccountNumber(int accountNumbe) {
    accountNumber = accountNumbe;
}

public String getAccountName() {
    return accountName;
}

/**
 * @param accountName the accountName to set
 */
public void setAccountName(String accountName) {
    this.accountName = accountName;
}

}

class FinancialFolk implements PasswordVerifivation {
private String accountUsername, accountHolderName, accountPassword;
protected ArrayList<Bank> accounts = new ArrayList<>();
protected static ArrayList<FinancialFolk> Bank = new ArrayList<>();
private int memberTime;

public FinancialFolk(String username, String name, String password) {
    accountUsername = username;
    accountHolderName = name;
    accountPassword = password;
}

public FinancialFolk() {
    accountUsername = "";
    accountHolderName = "";
    accountPassword = "";
}

public void passwordCreation() {
    accountPassword = JOptionPane.showInputDialog("Please choose a secure password for your account.");
}

public boolean passwordVerify(String attemptedPassword) {
    return accountPassword.equals(attemptedPassword);

}

public boolean createNewPerson() { // Creates a new FinancialFolk.
    accountHolderName = (JOptionPane.showInputDialog("Please enter your name."));
    if (accountHolderName == null)
        return false;
    String Username = (JOptionPane.showInputDialog("Please enter a username for your account."));
    for (FinancialFolk fk : Bank) {
        if (fk.getAccountUsername().equals(Username)) {
            JOptionPane.showMessageDialog(null, "That username is taken!");
            return false;
        }
    }
    accountUsername = Username;
    passwordCreation();
    return true;
}

public void createNewAccount() { // Adds an account to an existing FinancialFolk.
    Bank check = new Checking();
    Bank save = new Savings();
    String[] buttons = { "Checking", "Savings" };
    int acc = JOptionPane.showOptionDialog(null, "What type of account would you like to make?", "Bank",
            JOptionPane.PLAIN_MESSAGE, 1, null, buttons, buttons[1]);
    switch (acc) {
    case 0:
        check.setAccountName((JOptionPane.showInputDialog("Please enter a name for this account.")));
        check.setAccountNumber((int) (Math.random() * 3000 + 1091));
        accounts.add(check);
        check.display_details();
        break;
    case 1:
        save.setAccountName((JOptionPane.showInputDialog("Please enter a name for this account.")));
        save.setAccountNumber((int) (Math.random() * 3310 + 789));
        accounts.add(save);
        save.display_details();
        break;
    }
}

public static void main(String[] args) {
    Bank user = new Bank();
    FinancialFolk FF = new FinancialFolk();
    boolean exit = false;
    do {
        String[] buttons = { "Create Account For The First Time", "Login", "Exit Bank", "" };
        int rc = JOptionPane.showOptionDialog(null, "What would you like to do?", "Bank", JOptionPane.PLAIN_MESSAGE,
                3, null, buttons, buttons[3]);
        switch (rc) {
        case 0: // create account
            if (FF.createNewPerson()) {
                Bank.add(FF);
                Bank.get(Bank.size() - 1).createNewAccount();
            }
            break;
        case 1: // login
            boolean logout = false;
            boolean incun = true;

            String an = JOptionPane.showInputDialog("Enter your username.\n");
            String ps = JOptionPane.showInputDialog("Enter your acccount password.\n");
            for (FinancialFolk x : Bank) {
                if (x.getAccountUsername().equals(an)) {
                    incun = false;
                    if (x.passwordVerify(ps)) {
                        do {
                            String[] buttons2 = { "View all accounts.", "Deposit into an account",
                                    "Withdraw from your account", "Change password", "Create another bank account",
                                    "Logout" };
                            int rc2 = JOptionPane.showOptionDialog(null, "What would you like to do?", "Bank",
                                    JOptionPane.PLAIN_MESSAGE, 3, null, buttons2, buttons2[4]);
                            switch (rc2) {
                            case 0: // all accounts
                                int networth = 0;
                                for (Bank acct : FF.accounts) {
                                    acct.display_details();
                                    networth += acct.getBal();
                                }
                                JOptionPane.showMessageDialog(null,
                                        "Your account total between all accounts: $" + networth);
                                break;
                            case 1: { // deposit
                                if (x.accounts.size() == 0)
                                    JOptionPane.showMessageDialog(null, "You do not have any accounts!");
                                else if (x.accounts.size() == 1) {
                                    x.accounts.get(0).deposit(Integer.parseInt(JOptionPane
                                            .showInputDialog("Enter how many dollars you want to deposit.\n")));
                                } else {
                                    JOptionPane.showMessageDialog(null, x.accounts.size());
                                    String a = (JOptionPane
                                            .showInputDialog("Enter the account name to deposit into.\n"));
                                    for (Bank act : x.accounts) {
                                        if (act.getAccountName().equalsIgnoreCase(a))
                                            act.deposit(Integer.parseInt(JOptionPane.showInputDialog(
                                                    "Enter how many dollars you want to deposit.\n")));
                                    }
                                }
                            }
                                break;
                            case 2:
                                if (x.accounts.size() == 0)
                                    JOptionPane.showMessageDialog(null, "You do not have any accounts!");
                                else if (x.accounts.size() == 1) {
                                    x.accounts.get(0).withdraw(Integer.parseInt(JOptionPane
                                            .showInputDialog("Enter how many dollars you want to withdraw.\n")));
                                } else {
                                    String a = (JOptionPane
                                            .showInputDialog("Enter the account name to withdraw from.\n"));
                                    for (Bank act : x.accounts) {
                                        if (act.getAccountName().equalsIgnoreCase(a))
                                            act.deposit(Integer.parseInt(JOptionPane.showInputDialog(
                                                    "Enter how many dollars you want to withdraw.\n")));
                                    }
                                }
                                break;
                            case 3:
                                x.passwordSet((JOptionPane.showInputDialog("Enter your new password.\n")));
                                break;
                            case 4:
                                x.createNewAccount();
                                break;
                            case 5:
                                logout = true;
                                break;
                            }
                        } while (!logout);
                    } else
                        incun = true;
                }
            }
            if (incun)
                JOptionPane.showMessageDialog(null, "Invalid Username or password");
            break;
        case 2:
            JOptionPane.showMessageDialog(null, "Thank you for using the JacoBank");
            exit = true;
            break;
        case 3:
            System.out.print(Bank.size());
            System.out.print(FF.accounts.size());
            break;

        }
    } while (!exit);
}

public String getAccountHolderName() {
    return accountHolderName;
}

public void setAccountHolderName(String accountHolderName) {
    this.accountHolderName = accountHolderName;
}

public String getAccountUsername() {
    return accountUsername;
}

@Override
public void passwordSet(String password) {
    accountPassword = password;

}
}

class Manage {

}

class Checking extends Bank {
private int checkCount;
}

class Savings extends Bank {
}

2 个答案:

答案 0 :(得分:3)

protected static ArrayList<Bank> accounts = new ArrayList<>();

实例变量上下文中static的定义是该类的所有实例(在本例中为“ FinancialFolk”)共享变量的相同副本。这就是为什么您在创建新帐户时看到为每个人添加的帐户的原因。

换句话说:由于关键字static,每个“ FinancialFolk”对象的“ accounts”变量都指向堆中完全相同的arraylist。

答案 1 :(得分:1)

FinancialFolk FF =新的FinancialFolk();

上一行应处于do-while循环中。您仅创建了一个对象,并为所有用户帐户对同一对象进行了更改。