I made a bill class that uses a date and money class to create bills.
I have to be able to check whether the paid date is before (or on) the due date. I am unsure of how to compare the dates.
I am doing this in the public void setPaid method.
Bill Class:
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){ this.setAmount(toCopy.amount); this.setDueDate(toCopy.dueDate); this.setOriginator(toCopy.originator); //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){ 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(toCompare.getClass())){ return false; }else{ Bill toCopy = (Bill) toCompare; return getOriginator() == toCompare.getOriginator() && getDueDate() == toCompare.getDueDate() && getAmount() == toCompare.getAmount() ; } } } }
Date Class:
public class Date{ private int month, day, year; public Date(){ setMonth(1); setDay(1); setYear(1970); } public Date(int month, int day, int year){ setMonth(month); setDay(day); setYear(year); } public Date(Date otherDate){ setMonth(otherDate.getMonth()); setDay(otherDate.getDay()); setYear(otherDate.getYear()); } public int getMonth(){ return month; } public int getDay(){ return day; } public int getYear(){ return year; } public void setMonth(int newMonth){ if(newMonth < 1 || newMonth > 12){ throw new IllegalStateException(String.format("Illegal month: %d", newMonth)); } else{ month = newMonth; } } public void setDay(int newDay){ if(newDay < 1 || newDay > 31){ throw new IllegalStateException(String.format("Illegal day: %d", newDay)); } else{ day = newDay; } } public void setYear(int newYear){ if(newYear < 1970){ throw new IllegalStateException(String.format("Illegal year: %d", newYear)); } else{ year = newYear; } } public void setDate(int month, int day, int year){ setMonth(month); setDay(day); setYear(year); } public String toString(){ return String.format("%d/%d/%d", getMonth(), getDay(), getYear()); } public boolean equals(Object otherObject){ if (otherObject == null){ return false; }else{ if(!getClass().equals(otherObject.getClass())){ return false; }else{ Date otherDate = (Date) otherObject; return getMonth()== otherDate.getMonth() && getDay()== otherDate.getDay() && getYear()== otherDate.getYear(); } } } }