Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. import java.util.Optional;
  2. import java.util.Set;
  3.  
  4. public class Person {
  5.  
  6. private String firstName;
  7.  
  8. private String lastName;
  9.  
  10. private int age;
  11.  
  12. private Gender sex;
  13.  
  14. private Set<BonusCard> bonusCards;
  15.  
  16. public enum Gender { MALE, FEMALE }
  17.  
  18. public Person(String firstName, String lastName, int age, Gender gender) {
  19. if (age < 1)
  20. throw new IllegalArgumentException(); // TODO PersonException
  21. this.firstName = firstName;
  22. this.lastName = lastName;
  23. this.age = age;
  24. this.sex = gender;
  25. }
  26.  
  27. public String getFirstName() {
  28. return firstName;
  29. }
  30.  
  31. public String getLastName() {
  32. return lastName;
  33. }
  34.  
  35. public int getAge() {
  36. return age;
  37. }
  38.  
  39. public Gender getGender() {
  40. return sex;
  41. }
  42.  
  43. public Set<BonusCard> getBonusCards() {
  44. return bonusCards; // mb this will do if it's empty creating new empty set is not necessary
  45. }
  46.  
  47. /**
  48. * Gets bonus card by the specified type.
  49. *
  50. * @param cardType the bonus card type
  51. * @return bonus card with the specified type
  52. */
  53. public Optional<BonusCard> getBonusCardByType(BonusCard.CardType cardType) {
  54. for (BonusCard bonusCard : bonusCards) {
  55. if (bonusCard.getType() == cardType)
  56. return Optional.of(bonusCard);
  57. }
  58. return Optional.empty();
  59. }
  60.  
  61. public void addBonusCard(BonusCard bonusCard) {
  62. if (!bonusCards.contains(bonusCard))
  63. bonusCards.add(bonusCard);
  64. }
  65.  
  66. public void removeBonusCard(BonusCard bonusCard) {
  67. if (bonusCards.contains(bonusCard))
  68. bonusCards.remove(bonusCard);
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement