accountio.java

来自「本程序用Java语言描述了一个基本的银行管理系统」· Java 代码 · 共 44 行

JAVA
44
字号
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 + =
减小字号Ctrl + -
显示快捷键?