I made a Bill class that uses a Money class and a Date class. I used the driver bellow and noticed that after I change the amount and dueDate following the first set of println statements, that all three objects(bill1, bill2, and bill3) get the values. Is there any way to prevent this?
public class MyDriver{ public static void main(String [] args){ //Construct Bills Money amount = new Money(25); Date dueDate = new Date(3, 30, 2007); Bill bill1 = new Bill(amount, dueDate, "The phone company"); Bill bill2 = new Bill(bill1); // bill created from a already made bill System.out.println("Bill objects output:"); System.out.println(bill1); System.out.println(bill2); bill2.setDueDate(new Date(5, 30, 2007)); // change the date of a bill amount.setMoney(31, 99); // these two lines set ALL amount and dueDate values to the same. Why? dueDate.setDay(29); Bill bill3 = new Bill(amount, dueDate, "The record company"); System.out.println(bill1); System.out.println(bill2); System.out.println(bill3); } }
This is my Bill class. If you need to see the Money and Date class let me know.
public class Bill{ //Variables/Data Money amount; Date dueDate; Date paidDate; String originator; //Methods public Bill(Money amount, Date dueDate, String originator){ setAmount(amount); setDueDate(dueDate); setOriginator(originator); } public Bill(Bill toCopy){ setAmount(toCopy.getAmount()); setDueDate(toCopy.getDueDate()); setOriginator(toCopy.getOriginator()); } public Money getAmount(){ return amount; } public Date getDueDate(){ return dueDate; } public String getOriginator(){ return originator; } public boolean isPaid(){ if(!(paidDate == null)){ return true; }else{ return false; } } public void setPaid(Date onDay){ //if(dueDate > paidDate){ // System.out.println("The due date has passed"); //}else{ paidDate = onDay; //} } public void setUnpaid(){ paidDate = null; } public void setDueDate(Date nextDate){ dueDate = nextDate; } public void setAmount(Money amountGiven){ //if(amountGiven > 0){ //how else should i be protecting this? amount = amountGiven; //} } public void setOriginator(String originatorGiven){ originator = originatorGiven; } public String toString(){ if(paidDate== null){ if(originator == null){ return "An unknown company owes " + amount + " by the due date " + dueDate; }else{ return originator + " owes " + amount + " by the due date " + dueDate; } }else{ if(originator == null){ return "An unknown company owes " + " paid " + amount + " by the due date " + dueDate + " it was paid on " + paidDate; }else{ return originator + " paid " + amount + " by the due date " + dueDate + " it was paid on " + paidDate; } } } /** public boolean equals(Bill toCompare){ //null check if (toCompare == null){ return false; }else{ if(!getClass().equals(toComplare.getClass())){ return false; }else if( // ){ ************************************************************************* return getOriginator() == toCompare.getOriginator() && getDueDate() == toCompare.getDueDate() && getAmount() == toCompare.getAmount() } } } */ }