Goodiny777

אוספים שיעור כיתה

Feb 7th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. import java.util.Set;
  2. import java.util.TreeSet;
  3.  
  4. import Person.*;
  5. import Comperators.*;
  6.  
  7. public class Main {
  8.     public static void main(String[] args) {
  9.  
  10.         Set[] sortedPerson = new Set[3];
  11.         sortedPerson[0] = new TreeSet(new CompareByHeight());
  12.         sortedPerson[1] = new TreeSet(new CompareByWeight());
  13.         sortedPerson[2] = new TreeSet(new CompareByName());
  14.  
  15.         for (int i = 0; i < sortedPerson.length; i += 1) {
  16.             sortedPerson[i].add(new Person(170, 80, "Jhon"));
  17.             sortedPerson[i].add(new Person(160, 50, "Kate"));
  18.             sortedPerson[i].add(new Person(180, 85, "Bob"));
  19.             sortedPerson[i].add(new Person(175, 70, "Din"));
  20.             sortedPerson[i].add(new Person(166, 60, "Karl"));
  21.         }
  22.         System.out.println("Sorted by height: " + sortedPerson[0]);
  23.         System.out.println("Sorted by weight: " + sortedPerson[1]);
  24.         System.out.println("Sorted by name: " + sortedPerson[2]);
  25.     }
  26. }
  27.  
  28. ======================================================================
  29.  
  30. package Person;
  31.  
  32. public class Person {
  33.     public int height;
  34.     public int weight;
  35.     public String name;
  36.  
  37.     public Person(int height, int weight, String name) {
  38.         this.height = height;
  39.         this.weight = weight;
  40.         this.name = name;
  41.     }
  42.  
  43.     public int getHeight() {
  44.         return height;
  45.     }
  46.  
  47.     public int getWeight() {
  48.         return weight;
  49.     }
  50.  
  51.     public String getName() {
  52.         return name;
  53.     }
  54.  
  55.     @Override
  56.     public String toString() {
  57.         return "Person, " +
  58.                 "height=" + height +
  59.                 ", weight=" + weight +
  60.                 ", name='" + name + '\'';
  61.     }
  62. }
  63.  
  64.  
  65. ======================================================================
  66.  
  67. package Comperators;
  68.  
  69. import Person.Person;
  70. import java.util.Comparator;
  71.  
  72. public class CompareByWeight implements Comparator<Person> {
  73.  
  74.     @Override
  75.     public int compare(Person o1, Person o2) {
  76.         if(o1.getWeight()<o2.getWeight())
  77.             return -1;
  78.         else if (o1.getWeight()>o2.getWeight())
  79.             return 1;
  80.         else
  81.             return 0;
  82.     }
  83. }
  84.  
  85.  
  86. =========================================================================
  87.  
  88. package Comperators;
  89.  
  90. import Person.Person;
  91. import java.util.Comparator;
  92.  
  93. public class CompareByHeight implements Comparator<Person> {
  94.  
  95.     @Override
  96.     public int compare(Person o1, Person o2) {
  97.         if(o1.getHeight()<o2.getHeight())
  98.             return -1;
  99.         else if (o1.getHeight()>o2.getHeight())
  100.             return 1;
  101.         else
  102.             return 0;
  103.     }
  104. }
  105.  
  106.  
  107. ====================================================================
  108.  
  109. package Comperators;
  110.  
  111. import Person.Person;
  112. import java.util.Comparator;
  113.  
  114. public class CompareByName implements Comparator<Person> {
  115.  
  116.     @Override
  117.     public int compare(Person o1, Person o2) {
  118.         return o1.getName().compareTo(o2.getName());
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment