Guest User

Untitled

a guest
May 20th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map;
  3.  
  4. public class Person {
  5. private final String _firstName;
  6. private final String _lastName;
  7. private final String _dept;
  8. private final String _phone;
  9. private final int _age;
  10.  
  11. public Person(String firstName, String lastName, String dept, String phone, int age) {
  12. _firstName = firstName;
  13. _lastName = lastName;
  14. _dept = dept;
  15. _phone = phone;
  16. _age = age;
  17. }
  18.  
  19. public String getFirstName() {
  20. return _firstName;
  21. }
  22.  
  23. public String getLastName() {
  24. return _lastName;
  25. }
  26.  
  27. public String getDept() {
  28. return _dept;
  29. }
  30.  
  31. public String getPhone() {
  32. return _phone;
  33. }
  34.  
  35. public int getAge() {
  36. return _age;
  37. }
  38.  
  39. @Override
  40. public int hashCode() {
  41. final int prime = 31;
  42. int result = 1;
  43. result = prime * result + _age;
  44. result = prime * result + ((_dept == null) ? 0 : _dept.hashCode());
  45. result = prime * result + ((_lastName == null) ? 0 : _lastName.hashCode());
  46. result = prime * result + ((_phone == null) ? 0 : _phone.hashCode());
  47. return result;
  48. }
  49.  
  50. @Override
  51. public boolean equals(Object obj) {
  52. if (this == obj) {
  53. return true;
  54. }
  55. if (obj == null) {
  56. return false;
  57. }
  58. if (getClass() != obj.getClass()) {
  59. return false;
  60. }
  61. Person other = (Person) obj;
  62. if (_age != other._age) {
  63. return false;
  64. }
  65. if (_dept == null) {
  66. if (other._dept != null) {
  67. return false;
  68. }
  69. }
  70. else if (!_dept.equals(other._dept)) {
  71. return false;
  72. }
  73. if (_lastName == null) {
  74. if (other._lastName != null) {
  75. return false;
  76. }
  77. }
  78. else if (!_lastName.equals(other._lastName)) {
  79. return false;
  80. }
  81. if (_phone == null) {
  82. if (other._phone != null) {
  83. return false;
  84. }
  85. }
  86. else if (!_phone.equals(other._phone)) {
  87. return false;
  88. }
  89. return true;
  90. }
  91.  
  92. @Override
  93. public String toString() {
  94. return "Person [_firstName=" + _firstName + ", _lastName=" + _lastName + ", _dept=" + _dept + ", _phone=" + _phone + ", _age=" + _age + "]";
  95. }
  96.  
  97. public static void main(String[] args) {
  98. try {
  99. Person billy = new Person("Billy", "Johnson", "Acct", "5123", 33);
  100. Person bobby = new Person("Bobby", "Johnson", "Acct", "5123", 33);
  101. Map<Person, Integer> bonusForPerson = new HashMap<Person, Integer>();
  102. bonusForPerson.put(bobby, 10000);
  103. bonusForPerson.put(billy, 10);
  104. System.out.println("Person.main: " + bonusForPerson.get(bobby));
  105. }
  106. catch (Throwable e) {
  107. e.printStackTrace();
  108. }
  109. }
  110. }
Add Comment
Please, Sign In to add comment