我试图在文件中循环阅读"帐户"从文件到名为accounts
的arraylist。这是文件:
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.processAccounts:
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
====================================
答案 0 :(得分:1)
查看您的Account.java,看起来您可能已将变量置于静态(但我不确定,因为它们的声明在您的代码段中丢失)。
意思是,每次构造一个帐户时,这些静态变量都会设置为新的。因此,每次访问每个对象中的变量时,它们都是相同的。
如果我所做的所有这些假设都属实,那么您只需从帐户变量声明中删除static
关键字,并将访问者从Account.something
更改为{{1} }。