SHARE
TWEET

Untitled

a guest Jun 18th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Home_work_All_Collections {
  2.  
  3.     public static void main(String[] args) {
  4.  
  5.         User user = new User("C", 6, true);
  6.         User user1 = new User("D", 7, true);
  7.         LinkedList<User> list = new LinkedList<>();
  8.         list.add(user);
  9.         list.add(user1);
  10.         for (User list1 : list) {
  11.             System.out.println(list1);
  12.         }
  13.     }
  14. }
  15.      
  16. import java.util.Objects;
  17.  
  18. public class User implements Comparable<User> {
  19.  
  20.     private String name;
  21.     private int age;
  22.     private boolean activated;
  23.  
  24.     public User() {
  25.     }
  26.  
  27.     public User(String name, int age, boolean activated) {
  28.         this.name = name;
  29.         this.age = age;
  30.         this.activated = activated;
  31.     }
  32.  
  33.     public boolean isActivated() {
  34.         return activated;
  35.     }
  36.  
  37.     public void setActivated(boolean activated) {
  38.         this.activated = activated;
  39.     }
  40.  
  41.     public String getName() {
  42.         return name;
  43.     }
  44.  
  45.     public void setName(String name) {
  46.         this.name = name;
  47.     }
  48.  
  49.     public int getAge() {
  50.         return age;
  51.     }
  52.  
  53.     public void setAge(int age) {
  54.         this.age = age;
  55.     }
  56.  
  57.     @Override
  58.     public int hashCode() {
  59.         int hash = 7;
  60.         hash = 79 * hash + Objects.hashCode(this.name);
  61.         hash = 79 * hash + this.age;
  62.         hash = 79 * hash + (this.activated ? 1 : 0);
  63.         return hash;
  64.     }
  65.  
  66.     @Override
  67.     public boolean equals(Object obj) {
  68.         if (obj == null) {
  69.             return false;
  70.         }
  71.         if (getClass() != obj.getClass()) {
  72.             return false;
  73.         }
  74.         final User other = (User) obj;
  75.         if (!Objects.equals(this.name, other.name)) {
  76.             return false;
  77.         }
  78.         if (this.age != other.age) {
  79.             return false;
  80.         }
  81.         if (this.activated != other.activated) {
  82.             return false;
  83.         }
  84.         return true;
  85.     }
  86.  
  87.     @Override
  88.     public String toString() {
  89.         return "User{" + "name=" + name + ", age=" + age + ", activated=" + activated + '}';
  90.     }
  91.  
  92.     @Override
  93.     public int compareTo(User o) {
  94.         int res = name.compareTo(o.getName());
  95.         if (res == 0) {
  96.             if (age < o.getAge()) {
  97.                 return -1;
  98.             }
  99.             if (age > o.getAge()) {
  100.                 return 1;
  101.             }if(age == 0){
  102.                 if(activated == true){
  103.                     return 1;
  104.                 }else
  105.                     return -1;
  106.             }
  107.  
  108.         }
  109.         return res;
  110.     }
  111.  
  112. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top