Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.45 KB | None | 0 0
  1. package homeworkLab9;
  2.  
  3. import java.text.CollationKey;
  4. import java.util.ArrayList;
  5. import java.util.Locale;
  6. import java.util.Collections;
  7. import java.util.Comparator;
  8. import java.util.List;
  9. import java.text.Collator;
  10.  
  11. public class ZadLab9 {
  12.  
  13.     public static void main(String[] args) {
  14.         User firstUser = new User("Gabriel" , "Mladenov" , 35 , "GabMl" , "****");
  15.         User secondUser = new User("Darin" , "Duchev" , 47 , "DarinDuchev" , "5661616");
  16.         User thirdUser = new User("Kaloqn" , "Dimitrov" , 39 , "KalDimitrov" , "94651616");
  17.         UsersGroup GroupUsers = new UsersGroup("Group Users: ");
  18.         GroupUsers.addNewUser(firstUser);
  19.         GroupUsers.addNewUser(secondUser);
  20.         GroupUsers.addNewUser(thirdUser);
  21.        
  22.         System.out.println("Print Group Users: ");
  23.         GroupUsers.toString();
  24.        
  25.         System.out.println("Sort Group Users by First name: ");
  26.         GroupUsers.sortfirstName();
  27.         System.out.println("Print Group Users: ");
  28.         GroupUsers.toString();
  29.        
  30.         System.out.println("Sort Group Users by Last name: ");
  31.         GroupUsers.sortlastName();
  32.         System.out.println("Print Group Users: ");
  33.         GroupUsers.toString();
  34.        
  35.         System.out.println("Sort Group Users by Username: ");
  36.         GroupUsers.sortusername();
  37.         System.out.println("Print Group Users: ");
  38.         GroupUsers.toString();
  39.     }
  40. }
  41.  
  42. class Person implements Comparable<Person> {
  43.     private final String firstName;
  44.     private final String lastName;
  45.     private static final Collator collator = Collator.getInstance(Locale.getDefault());
  46.     private final CollationKey sortLastNameKey;
  47.     private final CollationKey sortFirstNameKey;
  48.     private int age;
  49.    
  50.     public Person(String fName, String lName, int iage){
  51.           this.firstName = fName;
  52.           this.lastName = lName;
  53.           this.age = iage;
  54.           sortLastNameKey=collator.getCollationKey(
  55.                   lName.toUpperCase() + fName.toUpperCase());
  56.           sortFirstNameKey=collator.getCollationKey(
  57.             fName.toUpperCase() + lName.toUpperCase());
  58.     }
  59.    
  60.     public CollationKey getFirstNameKey() {
  61.         return sortFirstNameKey;
  62.     }
  63.     public CollationKey getLastNameKey() {
  64.         return sortLastNameKey;
  65.     }
  66.    
  67.     public String get_firstName() {
  68.         return firstName;
  69.     }
  70.  
  71.     public String get_lastName() {
  72.         return lastName;
  73.     }
  74.    
  75.     public int get_age() {
  76.         return this.age;
  77.     }
  78.  
  79.   @Override
  80.     public int compareTo(Person  obj) {
  81.       return Integer.compare(this.age, obj.age);
  82.     }  
  83.   @Override
  84.     public String toString() {
  85.       String i = this.firstName + " " + this.lastName + " " + this.age;
  86.       return i;
  87.     }
  88. }
  89.  
  90. class LastNameComparator implements Comparator<Person> {
  91.     public int compare(Person person, Person anotherPerson) {
  92.         return ((person.getLastNameKey().compareTo(anotherPerson.getLastNameKey())));
  93.     }
  94. }
  95.  
  96. class FirstNameComparator implements Comparator<Person> {
  97.     public int compare(Person person, Person anotherPerson) {
  98.         return (person.getFirstNameKey().compareTo(anotherPerson.getFirstNameKey()));
  99.     }
  100. }
  101.  
  102. class User extends Person implements Comparable<Person> {
  103.     private String strUsername;
  104.     private String strPassword;
  105.  
  106.     public User(String fName , String lName , int iage , String gUsername , String gPassword) {
  107.       super(fName,lName,iage);
  108.       this.strUsername = gUsername;
  109.       this.strPassword = gPassword;
  110.     }
  111.    
  112.     public String get_userame() {
  113.       return this.strUsername;
  114.     }
  115.     public String get_password() {
  116.       return this.strPassword;
  117.     }
  118.  
  119.     public boolean equals(Object ob) {
  120.       User i = (User)ob;
  121.        return(this.strUsername.equals(i.get_userame()) && this.strPassword.equals(i.get_password()) && this.get_age()==i.get_age());        
  122.     }
  123.  
  124.     public String toString() {
  125.         String i = this.strUsername + " " + this.get_firstName() + " " + this.get_lastName() + " " + this.get_age();
  126.          return i;
  127.     }
  128.  
  129.     public int compareTo(User ob) {
  130.       String i = this.strUsername + this.strPassword + this.get_age();
  131.       String j = ob.get_userame() + ob.get_password() + ob.get_age();
  132.       return i.compareTo(j);
  133.     }
  134. }
  135.  
  136. class UsersGroup {
  137.     private String strGroupName;
  138.     private List<User> strCollectionUsers = new ArrayList<User>();
  139.    
  140.     public UsersGroup(String getGroupName) {
  141.         this.strGroupName=getGroupName;  
  142.     }
  143.    
  144.     public void addNewUser(User gCollectionUsers) {
  145.         this.strCollectionUsers.add(gCollectionUsers);  
  146.     }
  147.    
  148.     public void sortfirstName() {
  149.         FirstNameComparator firstName = new FirstNameComparator();
  150.         Collections.sort(this.strCollectionUsers , firstName);
  151.     }
  152.    
  153.     public void sortlastName() {
  154.         LastNameComparator lastName = new LastNameComparator();
  155.         Collections.sort(this.strCollectionUsers , lastName);
  156.     }
  157.    
  158.     public void sortusername() {
  159.         Comparator<User> user = new Comparator<User>() {
  160.             @Override
  161.             public int compare(User firstUser , User secondUser) {
  162.                 return  firstUser.compareTo(secondUser);
  163.             }
  164.         };
  165.         Collections.sort(this.strCollectionUsers, user);
  166.     }
  167.    
  168.     public String toString() {
  169.         System.out.println("Group name: "+ this.strGroupName);
  170.        
  171.         for (User n : strCollectionUsers) {
  172.             System.out.println(n.toString());
  173.         }
  174.         return " ";
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement