0

//My Extend Student class

public class ExtendStudent {
//data members
private String name;
private String email;

//constructor
public ExtendStudent(String StudentName, String StudentEmail){
    name = StudentName;
    email = StudentEmail;
}

//method
public String getName(){
    return name;
}

public String getEmail(){
    return email;
}

}

//My Extend Library Card class

public class ExtendLibraryCard {
//data members
private ExtendStudent owner;
private int numBorBooks;
private String expDate;
private int expDay;
private int expMonth;
private int expYear;
private boolean active;
private int thisYear;
private int thisMonth;
private int thisDay;

//constructor
public ExtendLibraryCard() {
    numBorBooks = 0;
    expDate = null;
    expMonth = 0;
    expYear = 0;
    setActive(true);
}

//methods
//set the owner be the student
public void setOwner(ExtendStudent student) {
    owner = student;
}

//get the name of the owner (which is the student)
public String getOwnerName() {
    return owner.getName();
}

//get the email of the owner
public String getOwnerEmail(){
    return owner.getEmail();
}

//number of books borrowed
public void totalBooksBorrowed(int totalBooks) {
        numBorBooks = numBorBooks + totalBooks;
}

//get number of books borrowed
public int getNumBorBooks() {
    return numBorBooks;
}

//print out
public String toStringExpDate() {
    return "Expiration Date: " + expDay + "/" + expMonth + "/" + expYear;
}

//expiration date
public void setExpDate(int expDay, int expMonth, int expYear) {
    this.expDay = expDay;
    this.expMonth = expMonth;
    this.expYear = expYear;
}

public int getExpMonth() {
    return expMonth;
}

public void setExpMonth(int expMonth) {
    this.expMonth = expMonth;
}

public int getExpYear() {
    return expYear;
}

public void setExpYear(int expYear) {
    this.expYear = expYear;
}

public int getExpDay() {
    return expDay;
}

public void setExpDay(int expDay) {
    this.expDay = expDay;
}

//set active
public void setActive(boolean state) {
    active = state;
}

//getter and setter of the current time
public int getThisYear() {
    return thisYear;
}

public void setThisYear(int thisYear) {
    this.thisYear = thisYear;
}

public int getThisMonth() {
    return thisMonth;
}

public void setThisMonth(int thisMonth) {
    this.thisMonth = thisMonth;
}

public int getThisDay() {
    return thisDay;
}

public void setThisDay(int thisDay) {
    this.thisDay = thisDay;
}

public void testing(){
    if(active = false)
        System.out.println("Your card is out of date. Please buy a new one or you will not be allowed to enter the library!");
    else
        System.out.println(toString());
}

//print out every single info to the card
public String toString(){
    return "Owner Name: " + getOwnerName() + "\n" +
            "Owner Email: " +getOwnerEmail() + "\n" +
            "Number of books borrowed: " + getNumBorBooks() + "\n" +
            "Today: " + getThisDay() + "/" + getThisMonth() + "/" + getThisYear() + "\n" +
            toStringExpDate();
}

}

//My Extend Librarian

public class ExtendLibrarian {
public static void main(String[] args) {
    ExtendStudent student = new ExtendStudent("SKT Faker", "fakerskt@yahoo.com");
    ExtendLibraryCard card = new ExtendLibraryCard();

    card.setOwner(student);
    card.getOwnerName();

    card.totalBooksBorrowed(20);

    card.setExpDate(20, 11, 2019);
    card.setThisDay(10);
    card.setThisMonth(11);
    card.setThisYear(2019);

    if (card.getThisYear() > card.getExpYear()) {
        card.setActive(false);
    } else if (card.getThisYear() == card.getExpYear()) {
        if (card.getThisMonth() > card.getExpMonth()) {
            card.setActive(false);
        }
    } else if (card.getThisYear() == card.getExpYear()) {
        if (card.getThisMonth() == card.getExpMonth()) {
            if (card.getThisDay() > card.getExpDay()) {
                card.setActive(false);
            }
        }
    }
    else {
        card.setActive(true);
    }

    card.testing();


}

}

So the thing is let just say my expiration day is Nov 20th 2019, and if today is Nov 21st 2019, the code will print out "Your card is out of date", but then it's not. Can somebody help me please, thank you.

P/s: Sorry if my English is terrible

Michael Nguyen
  • 83
  • 2
  • 4
  • 11

1 Answers1

0

First of all in your ExtendLibraryCard Class change this line

if(active = false)

to

if (active == false)

Also in your main Librarian class change the conditions from else if to if

if (card.getThisYear() > card.getExpYear()) {
        card.setActive(false);
    }
    if (card.getThisYear() == card.getExpYear()) {
        if (card.getThisMonth() > card.getExpMonth()) {
            card.setActive(false);
        }
    }
    if (card.getThisYear() == card.getExpYear()) {
        if (card.getThisMonth() == card.getExpMonth()) {
            if (card.getThisDay() > card.getExpDay()) {
                card.setActive(false);
            }
        }
    } else
        card.setActive(true);

    card.testing();
}

Let me give you a dry run what's happening in your code: When you have set thisYear= 2019 and ExpYear= 2019, your first else if statement is satisfied, since card.getThisYear() == card.getExpYear(), it doesn't matter even if both the month is same, greater or lesser(i.e it doesn't matter if, nested if is satisfied or not), because your getThisYear is equal to ExpThisYear and thus, 1st else if condition is satisfied, it won't check the final else if condition (which has your date checking nested if condition). Thus setActive(false) isn't being executed. Also since the else if condtion of this block

else if (card.getThisYear() == card.getExpYear()) {
        if (card.getThisMonth() > card.getExpMonth()) {
            card.setActive(false);
        }
    }

has been executed and it's returning true for the fist part year==year, even your else block won't be executed.

Thus it is necessary for you to change your condition to if statements because else if once condition has been met won't check other conditions unlike if.

Link : Read this for if vs else if condition vs else

Hope it helps :)