1

I already tried this:

public static void accountTypeSavings() {
    boolean b = true;
    while (b) {
        String startBalanceString = JOptionPane.showInputDialog(null, "Enter the starting balance in dollars without the dollar sign.");
        try {
            double startBalance = Double.parseDouble(startBalanceString);
            int accountID = 1;
            SavingsAccount accountID = new SavingsAccount(holder, startBalance);
            accountID += 1;
            b = false;
        }
        catch (final Exception ignored) {
            JOptionPane.showMessageDialog(null, "Only enter numbers, please.");
        }
    }
}

Creating an integer and setting the name of the savings account to the integer. But that's giving me the error "Duplicate local variable accountID".

When using my application, I want to get this:

SavingsAccount sa1 = new SavingsAccount(holder, startBalance);
SavingsAccount sa2 = new SavingsAccount(holder, startBalance);
SavingsAccount sa3 = new SavingsAccount(holder, startBalance);

The parameters are variables whose are already initialized!

I want to set the name of the new SavingsAccount to the accountID's value. So if I create a new instance of a SavingsAccount, I want my application to set it's name to sa1. And if I create another one after that, name it sa2, sa3, sa4, etc.

PS: Holder is a string, it contains the name of someone! It's not an integer!

I hope you understand what I mean!

4

4 回答 4

1
int accountID = 1;
SavingsAccount accountID = new SavingsAccount(holder, startBalance);

您不能有 2 个具有相同名称的变量。

于 2013-07-17T09:55:01.663 回答
1

但是如果有第二个储蓄账户,我如何让应用程序创建一个名为 sa2 的储蓄账户,然后创建一个名为 sa3 等的储蓄账户?

声明一个全局 int,比如说 index。

private int index;

在构造函数中将其设置为 1:

this.index = 1;

每次有人创建储蓄账户时,增加指数。

SavingsAccount accountID = new SavingsAccount(holder + index++, startBalance);

这会将 的当前值添加indexholder字符串的末尾,然后增加 的值index

编辑

好的,我明白你的意思了,但是变量不能以数字开头,或者只能以数字命名。相反,为什么不将这些帐户添加到一个array或一个List实现中呢?

List<SavingsAccount> accounts = new ArrayList<SavingsAccount>();
accounts.add(new SavingsAccount("Dave", something);
accounts.add(new SavingsAccount("Bill", somethingElse);

然后要获取每个帐户,您可以使用以下内容:

accounts.get(1);

注意:此列表基于 0。这意味着accounts.get(0);将获得第一项。

于 2013-07-17T09:59:00.240 回答
0

你需要的是一个列表。所以添加另一个静态变量 SavingAccountList

public static List<SavingsAccount> savingsAccountList = new ArrayList<SavingsAccount>();

您可以采用自动递增变量 m 并将其用作静态类变量:

public static int m=0;

public static void accountTypeSavings() {
        boolean b = true;

        holder="holderName"
        while (b) {
            String startBalanceString = JOptionPane.showInputDialog(null, "Enter the starting balance in dollars without the dollar sign.");
            try {
                double startBalance = Double.parseDouble(startBalanceString);
                SavingsAccount sa = new SavingsAccount(holder + m, startBalance);
                m++
                savingsAccountList.add(sa)
                b = false;
            }
            catch (final Exception ignored) {
                JOptionPane.showMessageDialog(null, "Only enter numbers, please.");
            }
        }
    }

您还需要导入 java.util.List。

于 2013-07-17T09:54:14.893 回答
0

您可能希望将节省的费用存储在您的方法之外的列表中我建议一个领域:

private static List<SavingsAccount> savingsAccounts = new ArrayList<SavingsAccount>();

你的方法可能是这样的:

public static void accountTypeSavings() {
    boolean b = true;
    while (b) {
        String startBalanceString = JOptionPane.showInputDialog(null, "Enter the starting balance in dollars without the dollar sign.");
        try {
            double startBalance = Double.parseDouble(startBalanceString);
            savingAccounts.add(new SavingsAccount(holder, startBalance));
            b = false;
        }
        catch (final Exception ignored) {
            JOptionPane.showMessageDialog(null, "Only enter numbers, please.");
        }
    }
}

稍后您可以从列表中访问储蓄账户以修改其属性:

savingAccounts.get(0).addAmount(amount); // something like this to add money to the first account
于 2013-07-17T09:58:26.517 回答