Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Using ArrayList ( Bank Account )

$
0
0
The first, I'm sorry because I learn English not good.
I have 2 class (SavingAccount & CheckingAccout) extends BankAccount.


Now, I want to create ArrayList<...> include: saving & checking, then save to a file( example: "bank.dat").
I've tried but not success. The first, I create ArrayList<BankAccount>
ArrayList<BankAccount> accounts = new ArrayList<BankAccount>();
accounts.add(new SavingAccount(1, "a", 1000, 0.5));
SavingAccount ba = (SavingAccount) accounts.get(0);
ba.getInterestRate(0.5);




I get the notice: "Exception in thread "main" java.lang.ClassCastException: business.BankAccount cannot be cast to business.SavingAccount
at business.AccountTester.main(AccountTester.java:43)"

Can you help me to use "ba.getInterestRate(0.5)"
Thanks!

package business;

import java.io.Serializable;
import java.util.Date;
import java.util.Calendar;
import java.util.TimeZone;

public class BankAccount implements Serializable{
	
	private int accountNo;
	protected String accountType;
	private String customerName;
	private double balance;
	private Date openedOn;


	public BankAccount() {
		super();
		balance = 0;

	}

	public BankAccount(int accountNo, String cusName, double balance) {
		this.accountNo = accountNo;
		this.customerName = cusName;
		this.balance = balance;
		this.openedOn = Calendar.getInstance(TimeZone.getTimeZone("GMT")).getTime();
	}

	public int getAccountNo() {
		return accountNo;
	}

	public String getAccountType() {
		return accountType;
	}

	public void setAccountType(String accountType) {
		this.accountType = accountType;
	}

	public void setAccountNo(int accountNo) {
		this.accountNo = accountNo;
	}

	public String getCustomerName() {
		return customerName;
	}

	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

	public void setBalance(double balance) {
		this.balance = balance;
	}

	public double getBalance() {
		return balance;
	}

	public Date getOpenedOn() {
		return openedOn;
	}

	public void setOpenedOn(Date openedOn) {
		this.openedOn = openedOn;
	}

	public void deposit(double amount) {
		balance = balance + amount;
	}

	public void withdraw(double amount) {
		balance = balance - amount;
	}

	public void transfer(double amount, BankAccount other) {
		withdraw(amount);
		other.deposit(amount);
	}




package business;

public class SavingAccount extends BankAccount {

	private static final long serialVersionUID = 1L;
	private double interestRate;

	public SavingAccount() {
		super();
		interestRate = 0;
		this.accountType = "Saving";
	}

	public SavingAccount(int accountNo, String cusName, double balance,
			double interestRate) {
		super(accountNo, cusName, balance);
		this.interestRate = interestRate;
		this.accountType = "Saving";
	}

	public double getInterestRate() {
		return interestRate;
	}

	public void setInterestRate(double interestRate) {
		this.interestRate = interestRate;
	}

	public void addInterest() {
		double interest = getBalance() * interestRate / 100;
		deposit(interest);
	}




package business;

public class CheckingAccount extends BankAccount {

	private int transactionCount;

	public CheckingAccount() {
		super();
		transactionCount = 0;
		this.setAccountType("Checking");
	}

	public CheckingAccount(int accountNo, String cusName, double balance) {
		super(accountNo, cusName, balance);
		
		this.transactionCount = 0;
		this.setAccountType("Checking");
	}

	public int getTransactionCount() {
		return transactionCount;
	}

	public void setTransactionCount(int transactionCount) {
		this.transactionCount = transactionCount;
	}



	public void deposit(double amount) {
		transactionCount++;
		super.deposit(amount);
	}

	public void withdraw(double amount) {
		transactionCount++;
		super.withdraw(amount);
	}

	public void deductFees() {
		if (transactionCount > IConstants.FREE_TRANSACTIONS) {
			double fees = IConstants.TRANSACTION_FEE
					* (transactionCount - IConstants.FREE_TRANSACTIONS);
			super.withdraw(fees);
		}
		transactionCount = 0;
	}



package business;


public interface IConstants {
	static final int FREE_TRANSACTIONS = 6;
	static final int TRANSACTION_FEE = 3;
	public static String FILE_NAME = "Bank.dat";

}



Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>