Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package homeworkLab9;
- import java.text.CollationKey;
- import java.util.ArrayList;
- import java.util.Locale;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
- import java.text.Collator;
- public class ZadLab9 {
- public static void main(String[] args) {
- User firstUser = new User("Gabriel" , "Mladenov" , 35 , "GabMl" , "****");
- User secondUser = new User("Darin" , "Duchev" , 47 , "DarinDuchev" , "5661616");
- User thirdUser = new User("Kaloqn" , "Dimitrov" , 39 , "KalDimitrov" , "94651616");
- UsersGroup GroupUsers = new UsersGroup("Group Users: ");
- GroupUsers.addNewUser(firstUser);
- GroupUsers.addNewUser(secondUser);
- GroupUsers.addNewUser(thirdUser);
- System.out.println("Print Group Users: ");
- GroupUsers.toString();
- System.out.println("Sort Group Users by First name: ");
- GroupUsers.sortfirstName();
- System.out.println("Print Group Users: ");
- GroupUsers.toString();
- System.out.println("Sort Group Users by Last name: ");
- GroupUsers.sortlastName();
- System.out.println("Print Group Users: ");
- GroupUsers.toString();
- System.out.println("Sort Group Users by Username: ");
- GroupUsers.sortusername();
- System.out.println("Print Group Users: ");
- GroupUsers.toString();
- }
- }
- class Person implements Comparable<Person> {
- private final String firstName;
- private final String lastName;
- private static final Collator collator = Collator.getInstance(Locale.getDefault());
- private final CollationKey sortLastNameKey;
- private final CollationKey sortFirstNameKey;
- private int age;
- public Person(String fName, String lName, int iage){
- this.firstName = fName;
- this.lastName = lName;
- this.age = iage;
- sortLastNameKey=collator.getCollationKey(
- lName.toUpperCase() + fName.toUpperCase());
- sortFirstNameKey=collator.getCollationKey(
- fName.toUpperCase() + lName.toUpperCase());
- }
- public CollationKey getFirstNameKey() {
- return sortFirstNameKey;
- }
- public CollationKey getLastNameKey() {
- return sortLastNameKey;
- }
- public String get_firstName() {
- return firstName;
- }
- public String get_lastName() {
- return lastName;
- }
- public int get_age() {
- return this.age;
- }
- @Override
- public int compareTo(Person obj) {
- return Integer.compare(this.age, obj.age);
- }
- @Override
- public String toString() {
- String i = this.firstName + " " + this.lastName + " " + this.age;
- return i;
- }
- }
- class LastNameComparator implements Comparator<Person> {
- public int compare(Person person, Person anotherPerson) {
- return ((person.getLastNameKey().compareTo(anotherPerson.getLastNameKey())));
- }
- }
- class FirstNameComparator implements Comparator<Person> {
- public int compare(Person person, Person anotherPerson) {
- return (person.getFirstNameKey().compareTo(anotherPerson.getFirstNameKey()));
- }
- }
- class User extends Person implements Comparable<Person> {
- private String strUsername;
- private String strPassword;
- public User(String fName , String lName , int iage , String gUsername , String gPassword) {
- super(fName,lName,iage);
- this.strUsername = gUsername;
- this.strPassword = gPassword;
- }
- public String get_userame() {
- return this.strUsername;
- }
- public String get_password() {
- return this.strPassword;
- }
- public boolean equals(Object ob) {
- User i = (User)ob;
- return(this.strUsername.equals(i.get_userame()) && this.strPassword.equals(i.get_password()) && this.get_age()==i.get_age());
- }
- public String toString() {
- String i = this.strUsername + " " + this.get_firstName() + " " + this.get_lastName() + " " + this.get_age();
- return i;
- }
- public int compareTo(User ob) {
- String i = this.strUsername + this.strPassword + this.get_age();
- String j = ob.get_userame() + ob.get_password() + ob.get_age();
- return i.compareTo(j);
- }
- }
- class UsersGroup {
- private String strGroupName;
- private List<User> strCollectionUsers = new ArrayList<User>();
- public UsersGroup(String getGroupName) {
- this.strGroupName=getGroupName;
- }
- public void addNewUser(User gCollectionUsers) {
- this.strCollectionUsers.add(gCollectionUsers);
- }
- public void sortfirstName() {
- FirstNameComparator firstName = new FirstNameComparator();
- Collections.sort(this.strCollectionUsers , firstName);
- }
- public void sortlastName() {
- LastNameComparator lastName = new LastNameComparator();
- Collections.sort(this.strCollectionUsers , lastName);
- }
- public void sortusername() {
- Comparator<User> user = new Comparator<User>() {
- @Override
- public int compare(User firstUser , User secondUser) {
- return firstUser.compareTo(secondUser);
- }
- };
- Collections.sort(this.strCollectionUsers, user);
- }
- public String toString() {
- System.out.println("Group name: "+ this.strGroupName);
- for (User n : strCollectionUsers) {
- System.out.println(n.toString());
- }
- return " ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement