Before I posted this, I read some of the previous posts and I really don't see anything wrong with my logic. (I'm 3 hours on this already and it's probably gonna kill my happy hour) *I never want to know the answers, I enjoy working hard so maybe if anyone can maybe ask me a question as to what I am trying to achieve that can lead me to think of the answer using your clues or hints. It will be appreciated.* obj2 is not cloning so behind the exception stackTrace, I found that there is a nullpointer exception on the same line which means that obj2 is never getting cloned. Please help me to think a little harder.
package testbankaccount;
/**
*
* @author
*/
public interface Cloneable {
}
My parent class
package testbankaccount;
/**
*
* @author
*/
public abstract class BankAccount implements Cloneable, Comparable {
private double balance;
private int numberofDeposits;
private int numberofWithdrawals;
private double annualInterestRate;
private double monthlyServiceCharges;
private String customerName;
protected BankAccount(){
this(1.0, 1.0);
}
protected BankAccount(double balance, double annualInterestRate){
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}
public void deposit(double deposit){
balance += deposit;
numberofDeposits++;
}
public void withdraw(double withdrawal){
balance -= withdrawal;
numberofWithdrawals++;
}
public void calcInterest(){
double monthlyInterestRate = annualInterestRate/1200;
double monthlyInterest = balance * monthlyInterestRate;
balance += monthlyInterest;
}
public void setMonthlyServiceCharge(double serviceCharge){
monthlyServiceCharges = serviceCharge;
}
public void monthlyProcess(){
balance -= monthlyServiceCharges;
calcInterest();
numberofWithdrawals = 0;
numberofDeposits = 0;
monthlyServiceCharges = 0.0;
}
public void setBalance(double balance){
this.balance = balance;
}
public double getBalance(){
return balance;
}
public int getNumberWithdraws(){
return numberofWithdrawals;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public abstract String toString();
@Override
public abstract boolean equals(Object obj);
}
My subclass
package testbankaccount;
/**
*
* @author Darren Wright
*/
public class SavingsAccount extends BankAccount implements Cloneable, Comparable {
private boolean status;
public SavingsAccount(){
this(1.0,1.0);
}
public SavingsAccount(double balance, double annualInterestRate){
super(balance,annualInterestRate);
}
public void setActivity(){
if (getBalance() > 25.0)
status = true;
else status = false;
}
@Override
public void withdraw(double withdrawal){
if (status = true)
super.withdraw(withdrawal);
}
@Override
public void deposit(double deposit){
if (status = false && (deposit + getBalance()) > 25.0)
{
status = true;
super.deposit(deposit);
}
else if (status = true)
super.deposit(deposit);
}
@Override
public void monthlyProcess(){
double result = 0.0;
if(getNumberWithdraws() >4)
result = getNumberWithdraws() - 4;
setMonthlyServiceCharge(result);
setActivity();
}
@Override
public String toString() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public boolean equals(Object obj) {
return (getBalance() == ((SavingsAccount)obj).getBalance());
}
public int compareTo(Object obj) {
if (getBalance() > ((SavingsAccount)obj).getBalance())
return 1;
else if (getBalance() < ((SavingsAccount)obj).getBalance())
return -1;
else
return 0;
}
}
My test class
package testbankaccount;
/**
*
* @author
*/
public class TestBankAccount{
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws CloneNotSupportedException {
SavingsAccount obj1 = new SavingsAccount(500,8.25);
try{
SavingsAccount obj2 = (SavingsAccount)obj1.clone();
System.out.println(obj1.compareTo(obj2));
System.out.println(obj1.equals(obj2));
}
catch (CloneNotSupportedException ex) {
ex.printStackTrace();
}
}
}
My error output
java.lang.CloneNotSupportedException: testbankaccount.SavingsAccount
at java.lang.Object.clone(Native Method)
at testbankaccount.BankAccount.clone(BankAccount.java:69)
at testbankaccount.SavingsAccount.clone(SavingsAccount.java:60)
at testbankaccount.TestBankAccount.main(TestBankAccount.java:16)
BUILD SUCCESSFUL (total time: 0 seconds)
What am I missing in my thought process? I created the interface, I implemented it and overided it in my super and subclass. My subclass refers super.clone() to the superclass and the super.clone() in the superclass I'm thinking refers to the Object's clone method. I casted correctly in the test class but obj2 winds up being null in both the compareTo and of course the equals. What am I not thinking about?
You shouldn't be creating your own Cloneable
interface. You should use the one that's built-in.