Marcel J
Marcel J

Reputation: 1

How to compare 2 objects that are created in different subclasses

I looked everywhere and haven't seen anything like this. I'm fairly new to java and have a homework assignment. The assignment is a Library. It has Books and dvd's, new and used. Im tying to compare an object to determine the correct fine.

Im basically asking the program, hey, is this a book or a dvd? and then hey, is this a new book or dvd, or is this an older one.

public double isLate(LocalDate argDateCheckedOut, LIBRARYMATERIALS argMaterial)
{ 
    LocalDate dateCheckedIn =  LocalDate.now();
    double diff=ChronoUnit.DAYS.between(argDateCheckedOut, dateCheckedIn);

    double fine = 0;

    if(argMaterial.equals(BOOK))
    {
        //calculate fine for book
        if(argMaterial.getisNew() == true)
        {
            if(diff > 21)
            {
                fine = 1 + .25*diff;
            }

        }
        else if(argMaterial.getisNew() == false)
        {
            if(diff > 28)
            {
                fine = .25 * diff;
            }
        }
    }

    else if(argMaterial.equals(DVD))
    {
        //calculate fine for dvd
        if(argMaterial.getisNew() == true)
        {
            if(diff > 3)
            {
                fine = 5 + diff;
            }

        }
        else if(argMaterial.getisNew() == false)
        {
            if(diff > 7)
            {
                fine = diff;
            }
        }

    }
    return fine;
}

Upvotes: 0

Views: 89

Answers (2)

Marcel J
Marcel J

Reputation: 1

I found an easy fix after talking to my professor over email. I'm sure your answer would've worked but I was looking for more of a... simpler fix.

Here it is. We used the instanceof to compare the objects, fines work accordingly.

double fine = 0;

if(argMaterial instanceof BOOK)
{
    //calculate fine for book
    if(argIsNew == true)
    {
        if(diff > 21)
        {
            fine = 1 + .25*diff;
        }

    }
    else if(argIsNew == false)
    {
        if(diff > 28)
        {
            fine = .25 * diff;
        }
    }
}

else if(argMaterial instanceof DVD)
{
    //calculate fine for dvd
    if(argIsNew == true)
    {
        if(diff > 3)
        {
            fine = 5 + diff;
        }

    }
    else if(argIsNew == false)
    {
        if(diff > 7)
        {
            fine = diff;
        }
    }

}
    return fine;

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

I would consider wiring this differently. I'd create a parent class or interface for Book and DVD (say called "LibraryResource") a public double getFine() method, perhaps make it abstract, and then I'd have Book and DVD override the parent method. Let the object calculate its own fine rather than make things difficult for yourself.

For example, if you had such a parent class:

public abstract class LibraryResource {
    private LocalDate checkOutDate;
    private boolean newResource;

    abstract public double calculateFine(); // **** make this abstract ****

    public LibraryResource(LocalDate checkOutDate, boolean newResource) {
        this.checkOutDate = checkOutDate;
        this.newResource = newResource;
    }

    public boolean isNewResource() {
        return newResource;
    }

    public void setNewResource(boolean newResource) {
        this.newResource = newResource;
    }

    public LocalDate getCheckOutDate() {
        return checkOutDate;
    }

}

And then both Book and Dvd could have their own formulas for calculating a fine. For example, the Book class could look something like:

public class Book extends LibraryResource {    
    private static final long SHORT_OVER_DUE_DIFF = 21;
    private static final long LONG_OVER_DUE_DIFF = 28;

    public Book(LocalDate checkOutDate, boolean newResource) {
        super(checkOutDate, newResource);
    }

    @Override
    public double calculateFine() {
        double fine = 0.0;
        LocalDate dateCheckedIn = LocalDate.now();
        long diff = ChronoUnit.DAYS.between(getCheckOutDate(), dateCheckedIn);

        if (isNewResource()) {
            if (diff > SHORT_OVER_DUE_DIFF) {
                fine = 1 + 0.25 * diff;
            } else if (diff > LONG_OVER_DUE_DIFF) {

                // .... ?? 

            }
        } else {
            // else it is NOT new
            if (diff > SHORT_OVER_DUE_DIFF) {

                // .... ??

            } else if (diff > LONG_OVER_DUE_DIFF) {
                fine = 0.25 * diff;                
            }
        }
        return fine;
    }

}

Upvotes: 1

Related Questions