2024-06-30 00:35:52 +01:00
|
|
|
package cc.fascinated.bat.features.birthday;
|
|
|
|
|
2024-07-01 01:12:32 +01:00
|
|
|
import cc.fascinated.bat.common.Serializable;
|
|
|
|
import com.google.gson.Gson;
|
2024-06-30 00:35:52 +01:00
|
|
|
import lombok.Getter;
|
|
|
|
import lombok.Setter;
|
2024-07-01 01:12:32 +01:00
|
|
|
import org.bson.Document;
|
2024-06-30 00:35:52 +01:00
|
|
|
|
2024-06-30 01:03:10 +01:00
|
|
|
import java.util.Calendar;
|
2024-06-30 00:35:52 +01:00
|
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Fascinated (fascinated7)
|
|
|
|
*/
|
|
|
|
@Getter
|
|
|
|
@Setter
|
2024-07-01 01:12:32 +01:00
|
|
|
public class UserBirthday extends Serializable {
|
2024-06-30 00:35:52 +01:00
|
|
|
/**
|
|
|
|
* The user's birthday
|
|
|
|
*/
|
|
|
|
private Date birthday;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the birthday should be hidden
|
|
|
|
*/
|
|
|
|
private boolean hidden;
|
2024-06-30 01:03:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the age of the user
|
|
|
|
*
|
|
|
|
* @return the age of the user
|
|
|
|
*/
|
|
|
|
public int calculateAge() {
|
|
|
|
Calendar birthdayCalendar = Calendar.getInstance();
|
|
|
|
birthdayCalendar.setTime(this.getBirthday());
|
|
|
|
Calendar today = Calendar.getInstance();
|
|
|
|
int age = today.get(Calendar.YEAR) - birthdayCalendar.get(Calendar.YEAR);
|
|
|
|
|
|
|
|
// Check if the birthday hasn't occurred yet this year
|
|
|
|
if (today.get(Calendar.DAY_OF_YEAR) < birthdayCalendar.get(Calendar.DAY_OF_YEAR)) {
|
|
|
|
age--;
|
|
|
|
}
|
|
|
|
return age;
|
|
|
|
}
|
2024-07-01 01:12:32 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void load(Document document, Gson gson) {
|
|
|
|
this.birthday = document.getDate("birthday");
|
|
|
|
this.hidden = document.getBoolean("hidden", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Document serialize(Gson gson) {
|
|
|
|
Document document = new Document();
|
|
|
|
document.put("birthday", this.birthday);
|
|
|
|
document.put("hidden", this.hidden);
|
|
|
|
return document;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void reset() {
|
|
|
|
|
|
|
|
}
|
2024-06-30 00:35:52 +01:00
|
|
|
}
|