Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Optional;
- import java.util.Set;
- public class Person {
- private String firstName;
- private String lastName;
- private int age;
- private Gender sex;
- private Set<BonusCard> bonusCards;
- public enum Gender { MALE, FEMALE }
- public Person(String firstName, String lastName, int age, Gender gender) {
- if (age < 1)
- throw new IllegalArgumentException(); // TODO PersonException
- this.firstName = firstName;
- this.lastName = lastName;
- this.age = age;
- this.sex = gender;
- }
- public String getFirstName() {
- return firstName;
- }
- public String getLastName() {
- return lastName;
- }
- public int getAge() {
- return age;
- }
- public Gender getGender() {
- return sex;
- }
- public Set<BonusCard> getBonusCards() {
- return bonusCards; // mb this will do if it's empty creating new empty set is not necessary
- }
- /**
- * Gets bonus card by the specified type.
- *
- * @param cardType the bonus card type
- * @return bonus card with the specified type
- */
- public Optional<BonusCard> getBonusCardByType(BonusCard.CardType cardType) {
- for (BonusCard bonusCard : bonusCards) {
- if (bonusCard.getType() == cardType)
- return Optional.of(bonusCard);
- }
- return Optional.empty();
- }
- public void addBonusCard(BonusCard bonusCard) {
- if (!bonusCards.contains(bonusCard))
- bonusCards.add(bonusCard);
- }
- public void removeBonusCard(BonusCard bonusCard) {
- if (bonusCards.contains(bonusCard))
- bonusCards.remove(bonusCard);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement