Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Map;
- public class Person {
- private final String _firstName;
- private final String _lastName;
- private final String _dept;
- private final String _phone;
- private final int _age;
- public Person(String firstName, String lastName, String dept, String phone, int age) {
- _firstName = firstName;
- _lastName = lastName;
- _dept = dept;
- _phone = phone;
- _age = age;
- }
- public String getFirstName() {
- return _firstName;
- }
- public String getLastName() {
- return _lastName;
- }
- public String getDept() {
- return _dept;
- }
- public String getPhone() {
- return _phone;
- }
- public int getAge() {
- return _age;
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + _age;
- result = prime * result + ((_dept == null) ? 0 : _dept.hashCode());
- result = prime * result + ((_lastName == null) ? 0 : _lastName.hashCode());
- result = prime * result + ((_phone == null) ? 0 : _phone.hashCode());
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- Person other = (Person) obj;
- if (_age != other._age) {
- return false;
- }
- if (_dept == null) {
- if (other._dept != null) {
- return false;
- }
- }
- else if (!_dept.equals(other._dept)) {
- return false;
- }
- if (_lastName == null) {
- if (other._lastName != null) {
- return false;
- }
- }
- else if (!_lastName.equals(other._lastName)) {
- return false;
- }
- if (_phone == null) {
- if (other._phone != null) {
- return false;
- }
- }
- else if (!_phone.equals(other._phone)) {
- return false;
- }
- return true;
- }
- @Override
- public String toString() {
- return "Person [_firstName=" + _firstName + ", _lastName=" + _lastName + ", _dept=" + _dept + ", _phone=" + _phone + ", _age=" + _age + "]";
- }
- public static void main(String[] args) {
- try {
- Person billy = new Person("Billy", "Johnson", "Acct", "5123", 33);
- Person bobby = new Person("Bobby", "Johnson", "Acct", "5123", 33);
- Map<Person, Integer> bonusForPerson = new HashMap<Person, Integer>();
- bonusForPerson.put(bobby, 10000);
- bonusForPerson.put(billy, 10);
- System.out.println("Person.main: " + bonusForPerson.get(bobby));
- }
- catch (Throwable e) {
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment