Guest User

Untitled

a guest
Jun 24th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.01 KB | None | 0 0
  1. import javax.swing.*;
  2.  
  3. class Person{
  4.     private String name, lastName;
  5.     private int age, id;
  6.  
  7.     public Person(){
  8.  
  9.     }
  10.     public void setName(){
  11.         name = JOptionPane.showInputDialog(null, "Enter your first and last name:");
  12.     }
  13.     public String getName(){
  14.         return name;
  15.     }
  16.     public void setLastName(String lastName){
  17.         this.lastName = lastName;
  18.     }
  19.     public String getLastName(){
  20.         return lastName;
  21.     }
  22.     public void setAge(){
  23.         age = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter your age:"));
  24.     }
  25.     public int getAge(){
  26.         return age;
  27.     }
  28.     public void setId(){
  29.         id = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter your Id number:"));
  30.     }
  31.     public int getId(){
  32.         return id;
  33.     }
  34.     public String toString(){
  35.         return "Id number: " + id + ", age: " + age + ", name: " + name;
  36.     }
  37. }
  38. class SortAndSearch{
  39.     Person[] person = new Person[6];
  40.  
  41.     public SortAndSearch(){
  42.  
  43.     }
  44.     public void setArray(){
  45.         String lastName;
  46.         for(int i = 0; i < person.length; i++){
  47.             person[i] = new Person();
  48.             person[i].setName();
  49.             person[i].setAge();
  50.             person[i].setId();
  51.         }
  52.         for(int i = 0; i < person.length; i++){
  53.             int characterCount = person[i].getName().length();
  54.             for(int j =0; j < characterCount; j++){
  55.                 if(person[i].getName().charAt(j) == ' '){
  56.                     lastName = person[i].getName().substring(j+1, characterCount);
  57.                     person[i].setLastName(lastName);
  58.                 }
  59.             }
  60.         }
  61.     }
  62.     public void sortById(){
  63.         Person temp;
  64.         for(int i = 0; i < person.length-1; i++){
  65.             for(int j = 0; j < person.length-i-1; j++){
  66.                 if(person[j].getId() > person[j+1].getId()){
  67.                     temp = person[j];
  68.                     person[j] = person[j+1];
  69.                     person[j+1] = temp;
  70.                 }
  71.             }
  72.         }
  73.     }
  74.     public void sortByLastName(){
  75.         Person temp;
  76.         for(int i = 0; i < person.length-1; i++){
  77.             for(int j =0; j < person.length-i-1; j++){
  78.                 if(exchange(person[j],person[j+1])){
  79.                     temp = person[j];
  80.                     person[j] = person[j+1];
  81.                     person[j+1] = temp;
  82.                 }
  83.             }
  84.         }
  85.     }
  86.     private boolean exchange(Person a, Person b){
  87.         int countA = a.getLastName().length();
  88.         int countB = b.getLastName().length();
  89.         for(int i = 0; i < Math.min(countA, countB); i++){
  90.             if(a.getLastName().charAt(i) > b.getLastName().charAt(i))
  91.                 return true;
  92.             if(a.getLastName().charAt(i) < b.getLastName().charAt(i))
  93.                 return false;
  94.         }
  95.         if(countA > countB)
  96.             return true;
  97.         else
  98.             return false;
  99.     }
  100.     public void searchByLastName(char firstLetter){
  101.         String result = "";
  102.         for(int i = 0; i < person.length; i++){
  103.             if(person[i].getLastName().charAt(0) == firstLetter)
  104.                 result += person[i] + "\n";
  105.         }
  106.         if(result.equals(""))
  107.             System.out.println("No last names starting with letter " + firstLetter + ".\n");
  108.         else
  109.             System.out.println(result);
  110.     }
  111.     public void searchByAge(int age){
  112.         String result = "";
  113.         for(int i = 0; i < person.length; i++){
  114.             if(person[i].getAge() > age)
  115.                 result += person[i] + "\n";
  116.         }
  117.         if(result.equals(""))
  118.             System.out.println("No ages above " + age + ".\n");
  119.         else
  120.             System.out.println(result);
  121.     }
  122. }
Add Comment
Please, Sign In to add comment