Java BufferedReader Switch case 存储在数组中的同一行
Java BufferedReader Switch case same line stored in array
我正在尝试遍历文件以将 "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.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
====================================
查看您的 Account.java,您似乎已经将其中的变量设为静态(但我不确定,因为您的代码段中缺少它们的声明)。
意思是,每次您构造一个帐户时,这些静态变量都会被设置为新的。因此,每次在每个对象中访问这些变量时,它们都是相同的。
如果我所做的所有这些假设都是正确的,那么您所要做的就是从您的帐户变量声明中删除 static
关键字并将访问器从 Account.something
更改为 this.something
.
我正在尝试遍历文件以将 "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.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
====================================
查看您的 Account.java,您似乎已经将其中的变量设为静态(但我不确定,因为您的代码段中缺少它们的声明)。
意思是,每次您构造一个帐户时,这些静态变量都会被设置为新的。因此,每次在每个对象中访问这些变量时,它们都是相同的。
如果我所做的所有这些假设都是正确的,那么您所要做的就是从您的帐户变量声明中删除 static
关键字并将访问器从 Account.something
更改为 this.something
.