Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- class Person{
- private String name, lastName;
- private int age, id;
- public Person(){
- }
- public void setName(){
- name = JOptionPane.showInputDialog(null, "Enter your first and last name:");
- }
- public String getName(){
- return name;
- }
- public void setLastName(String lastName){
- this.lastName = lastName;
- }
- public String getLastName(){
- return lastName;
- }
- public void setAge(){
- age = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter your age:"));
- }
- public int getAge(){
- return age;
- }
- public void setId(){
- id = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter your Id number:"));
- }
- public int getId(){
- return id;
- }
- public String toString(){
- return "Id number: " + id + ", age: " + age + ", name: " + name;
- }
- }
- class SortAndSearch{
- Person[] person = new Person[6];
- public SortAndSearch(){
- }
- public void setArray(){
- String lastName;
- for(int i = 0; i < person.length; i++){
- person[i] = new Person();
- person[i].setName();
- person[i].setAge();
- person[i].setId();
- }
- for(int i = 0; i < person.length; i++){
- int characterCount = person[i].getName().length();
- for(int j =0; j < characterCount; j++){
- if(person[i].getName().charAt(j) == ' '){
- lastName = person[i].getName().substring(j+1, characterCount);
- person[i].setLastName(lastName);
- }
- }
- }
- }
- public void sortById(){
- Person temp;
- for(int i = 0; i < person.length-1; i++){
- for(int j = 0; j < person.length-i-1; j++){
- if(person[j].getId() > person[j+1].getId()){
- temp = person[j];
- person[j] = person[j+1];
- person[j+1] = temp;
- }
- }
- }
- }
- public void sortByLastName(){
- Person temp;
- for(int i = 0; i < person.length-1; i++){
- for(int j =0; j < person.length-i-1; j++){
- if(person[j].getLastName().compareTo(person[j+1].getLastName()) > 0){
- temp = person[j];
- person[j] = person[j+1];
- person[j+1] = temp;
- }
- }
- }
- }
- public void searchByLastName(char firstLetter){
- String result = "";
- for(int i = 0; i < person.length; i++){
- if(person[i].getLastName().charAt(0) == firstLetter)
- result += person[i] + "\n";
- }
- if(result.equals(""))
- System.out.println("No last names starting with letter " + firstLetter + ".\n");
- else
- System.out.println(result);
- }
- public void searchByAge(int age){
- String result = "";
- for(int i = 0; i < person.length; i++){
- if(person[i].getAge() > age)
- result += person[i] + "\n";
- }
- if(result.equals(""))
- System.out.println("No ages above " + age + ".\n");
- else
- System.out.println(result);
- }
- }
Add Comment
Please, Sign In to add comment