package cse1030.Feb03;

public class BankAccount 
implements Comparable<BankAccount>
{

	// fields
	
	public static final double INTEREST_RATE = 0.02;

	private static int count = 0;
	
	private double balance;
	
	private int accountNumber;
	
	// constructors
		
	public BankAccount(double balance) {
		
		this.balance = balance;
		BankAccount.count++;
		this.accountNumber = BankAccount.count;
	
	}

	public BankAccount() {
		
		this(0);
		
	}
	
	public BankAccount(BankAccount b) {
		this(b.balance);
	}

	// BankAccount b = new BankAccount(3.50)
	
	// methods
	
	public void deposit(double amount) {
		if (amount >= 0) {
			this.balance += amount;
		}
	}

	public boolean withdraw(double amount) {
		if (this.balance >= amount) {
			this.balance -= amount;
			return true;
		}
		else {
			return false;
		}
	}

	public double getBalance() {
		return this.balance;
	}
	
	public String toString() {
		//[BankAccount:balance=10.56]
		String s = "[BankAccount:accountNumber="+this.accountNumber+",balance="+this.balance+"]";
		return s;
	}
	
	public boolean equals(Object o) {
		
		if ((o == null)||(o.getClass() != this.getClass())) {
			return false;
		} //(A||B) is false then not-A is true and not-B is true
		else { // o != null && o.getClass() == this.getClass()
			BankAccount b = (BankAccount)o;
			if (b.balance != this.balance) {
				return false;
			}
			else if (b.accountNumber != this.accountNumber) {
				return false;
			}
			else { // they are equal
				return true;
			}
		}
	
		
	}
	
	public int hashCode() {
		return (this.toString()).hashCode();
		//return super.hashCode();
	}
	
	public boolean withdraw(double amount, int times) {
		return withdraw(amount*times);
	}

	public static double calculateInterest(BankAccount b) {
		return b.balance * BankAccount.INTEREST_RATE;
	}
	
	public int getAccountNumber() {
		return this.accountNumber;
	}

	public static int getNumberOfAccounts() {
		return BankAccount.count;
	}

	public int compareTo(BankAccount b) { 
		if (this.balance < b.balance){
			return -1;
		}
		else if (this.balance > b.balance){
			return 1;
		}
		else { // they're equal
			return 0;
		}
	}

	
}
