Java BufferedReader Switch大小写存储在数组中的同一行



我正试图循环遍历一个文件,将文件中的"accounts"读取到一个名为accounts的数组列表中。这是文件:

c 1111 1234 703.92
x 2222 1234 100.00
s 3333 1234 200.08

我知道它正在循环到下一行,因为我打印了id,它从1111到2222再到3333,但在底部,当我打印帐户的tostring时,它们都是同一行(文件中的"s"行)。为什么会发生这种情况?

Bank.main:

private static ArrayList<Account> accounts = new ArrayList<Account>();
public static void main(String[] args)
{
    //if incorrect # or bankfile can't be opened, print to system error
    //Usage: java Bank bankFile [batchFile]
    if(args.length > 2 || args.length == 0){
        System.err.println("Usage: java Bank bankFile [batchFile]");
    }
    else{
        if(args.length == 2){ //batch mode
            processAccounts(args[0]);
            //BankBatch.processBatch(args);
            close(args[0]);
        }
        else if(args.length == 1){ // gui mode
        }
    }
}

Bank.prrocessAccounts:

private static void processAccounts(String filepath){
    BufferedReader accountReader;
    File accountFile = new File(filepath);
    if(accountFile.isDirectory()){
        System.err.println("Usage: java Bank bankFile [batchFile]");
        System.exit(1);
    }
    if(accountFile.exists()){
        try{
            accountReader = new BufferedReader(new FileReader(accountFile));
            String line = accountReader.readLine();
            while(line != null && line != ""){//in case there are extra empty lines
                String[] s = line.split(" "); // parts of an account line: type, id, pin, balance
                if(s.length != 4) break; //incorrect format, shouldn't happen
                int id = Integer.parseInt(s[1]);
                System.out.println(line);
                int pin = Integer.parseInt(s[2]);
                double balance = Double.parseDouble(s[3]);
                switch(s[0]){ //account type
                    case "x": 
                        System.out.println("case x");
                        accounts.add(new checkingAccount(id, pin, balance));
                        break;
                    case "c":
                        System.out.println("case c");
                        accounts.add(new cdAccount(id, pin, balance));
                        break;
                    case "s":
                        System.out.println("case s");
                        accounts.add(new savingsAccount(id, pin, balance));
                        break;
                }
                line = accountReader.readLine();
            }
            System.out.println(accounts.get(0).toString());
            System.out.println(accounts.get(1).toString());
            System.out.println(accounts.get(2).toString());
            accountReader.close();
        }
        catch(FileNotFoundException e){
            //this shouldn't happen since we check to see if it exists
        }
        catch (IOException e) {
        }
    }
}

Account.java:

public abstract class Account {
public Account(int id, int pin, double bal){
    Account.balance = bal;
    Account.pin = pin;
    Account.id = id;
}
public String toString(){
    return (acctType + " " + id + " " + pin + " " + balance);
}

扩展帐户的示例,所有帐户都相似:

public class checkingAccount extends Account {
public checkingAccount(int id, int pin, double bal){
    super(id, pin, bal);
    setAcctType('x');
    setMin(50);
}

/**
 * apply montly interest
 * checkings accounts have no interest, only penalties for having less than minimum
 */
@Override
public double applyInterest() { //only penalty with a checking account
    if (getBalance() > 5){
        withdraw(5);
        return -5;
    }
    else{
        double change =  Math.round((getBalance() * 0.10) * 100.0) / 100.0; //10% of balance, round to 2 decimal places
        withdraw(change);
        return -change;
    }
}
}

这是我得到的输出:

c 1111 1234 703.92
case c
x 2222 1234 100.00
case x
s 3333 1234 200.08
case s
s 3333 1234 200.08
s 3333 1234 200.08
s 3333 1234 200.08
========= Final Bank Data ==========
Acount Type   Account   Balance   
-----------   -------   -------
 Saving      3333    200.08
 Saving      3333    200.08
 Saving      3333    200.08
====================================

查看你的Account.java,你可能已经将其中的变量设置为静态的(但我不确定,因为它们的声明在你的代码片段中丢失了)。

也就是说,每次构建Account时,这些静态变量都会设置为新的变量。因此,每次访问每个对象中的这些变量时,它们都是相同的。

如果我所做的所有这些假设都是正确的,那么您所要做的就是从Account变量声明中删除static关键字,并将访问器从Account.something更改为this.something

最新更新