📄 accountio.java
字号:
package banking.io;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import banking.domain.Account;
import banking.domain.CheckingAccount;
import banking.domain.SavingsAccount;
public class AccountIO {
final static int TYPE_SAVINGS = 1;
final static int TYPE_CHECKING = 2;
public static void writeAccount(Account account, DataOutput output)
throws IOException {
if (account instanceof SavingsAccount) {
output.writeInt(AccountIO.TYPE_SAVINGS);
output.writeDouble(((SavingsAccount) account).getInterestRate());
} else if (account instanceof CheckingAccount) {
output.writeInt(AccountIO.TYPE_CHECKING);
output.writeDouble(((CheckingAccount) account)
.getOverdraftProtection());
}
output.writeDouble(account.getBalance());
}
public static Account readAccount(DataInput input) throws IOException {
int type = input.readInt();
if (type == AccountIO.TYPE_CHECKING) {
double overdraftProtection = input.readDouble();
double balance = input.readDouble();
return new CheckingAccount(balance, overdraftProtection);
} else if (type == AccountIO.TYPE_SAVINGS) {
double interestRate = input.readDouble();
double balance = input.readDouble();
return new SavingsAccount(balance, interestRate);
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -