import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Person implements Comparable { private String name; private String surname; private int age; private int height; private double weight; private static List lista = new ArrayList(); public static int liczba; public static int numberOfPerson() { return liczba; } public static void addPerson(String name, String surname, int age, int height, double weight) { Person person = new Person(name, surname, age, height, weight); Person.lista.add(person); liczba++; } public static List getAllPerson() { return lista; } public static void searchPerson() { // switch name,surname,age,height,weight Scanner read = new Scanner(System.in); Scanner nameScanner=new Scanner(System.in); int choice = 1; System.out.println(choice); do { System.out.println("Find a person by"); System.out.println("Enter your choice"); System.out.println("0.Exit"); System.out.println("1.Name"); System.out.println("2.Surname"); System.out.println("3.Age"); System.out.println("4.Height"); System.out.println("5.Weight"); String choicename; choice=read.nextInt(); switch (choice) { case 0: System.out.println("0.Exit"); break; case 1: System.out.println("1.Name"); System.out.println("Enter Name"); choicename = nameScanner.nextLine(); for (Person person : lista) { if (person.getName().equalsIgnoreCase(choicename)) System.out.println(person); } break; case 2: System.out.println("2.Surname"); System.out.println("Enter Surname"); choicename = nameScanner.nextLine(); for (Person person : lista) { if (person.getSurname().equalsIgnoreCase(choicename)) System.out.println(person); } break; case 3: System.out.println("3.Age"); System.out.println("Enter Age"); choicename = nameScanner.nextLine(); for (Person person : lista) { if (person.getAge()==Integer.parseInt(choicename)) System.out.println(person); } break; case 4: System.out.println("4.Height"); choicename = nameScanner.nextLine(); for (Person person : lista) { if (person.getHeight()==Integer.parseInt(choicename)) System.out.println(person); } break; case 5: System.out.println("5.Weight"); choicename = nameScanner.nextLine(); for (Person person : lista) { if (person.getWeight()==Integer.parseInt(choicename)) System.out.println(person); } break; default: System.out.println("Incorrect number.Try again.Enter 0 to exit"); break; } } while (choice > 1 && choice < 6); //System.out.println(choice); read.close(); nameScanner.close(); } public static void minPerson(){ Scanner read = new Scanner(System.in); Scanner nameScanner=new Scanner(System.in); int choice = 1; System.out.println(choice); do { System.out.println("Min a person by"); System.out.println("Enter your choice"); System.out.println("0.Exit"); System.out.println("1.Name"); System.out.println("2.Surname"); System.out.println("3.Age"); System.out.println("4.Height"); System.out.println("5.Weight"); String choicename; choice=read.nextInt(); switch (choice) { case 0: System.out.println("0.Exit"); break; case 1: System.out.println("1.Name"); Collections.sort(Person.lista,new ComparatorPersonName()); System.out.println(Person.lista.get(0)); break; case 2: System.out.println("2.Surname"); Collections.sort(Person.lista,new ComparatorPersonSurname()); System.out.println(Person.lista.get(0)); break; case 3: System.out.println("3.Age"); Collections.sort(Person.lista,new ComparatorPersonAge()); System.out.println(Person.lista.get(0)); break; case 4: System.out.println("4.Height"); Collections.sort(Person.lista,new ComparatorPersonHeight()); System.out.println(Person.lista.get(0)); break; case 5: System.out.println("5.Weight"); Collections.sort(Person.lista,new ComparatorPersonWeight()); System.out.println(Person.lista.get(0)); break; default: System.out.println("Incorrect number.Try again.Enter 0 to exit"); break; } } while (choice > 1 && choice < 6); //System.out.println(choice); read.close(); nameScanner.close(); } public static void maxPerson(){ Scanner read = new Scanner(System.in); Scanner nameScanner=new Scanner(System.in); int choice = 1; System.out.println(choice); do { System.out.println("Max a person by"); System.out.println("Enter your choice"); System.out.println("0.Exit"); System.out.println("1.Name"); System.out.println("2.Surname"); System.out.println("3.Age"); System.out.println("4.Height"); System.out.println("5.Weight"); String choicename; choice=read.nextInt(); switch (choice) { case 0: System.out.println("0.Exit"); break; case 1: System.out.println("1.Name"); Collections.sort(Person.lista,new ComparatorPersonName()); System.out.println(Person.lista.get(lista.size()-1)); break; case 2: System.out.println("2.Surname"); Collections.sort(Person.lista,new ComparatorPersonSurname()); System.out.println(Person.lista.get(lista.size()-1)); break; case 3: System.out.println("3.Age"); Collections.sort(Person.lista,new ComparatorPersonAge()); System.out.println(Person.lista.get(lista.size()-1)); break; case 4: System.out.println("4.Height"); Collections.sort(Person.lista,new ComparatorPersonHeight()); System.out.println(Person.lista.get(lista.size()-1)); break; case 5: System.out.println("5.Weight"); Collections.sort(Person.lista,new ComparatorPersonWeight()); System.out.println(Person.lista.get(lista.size()-1)); break; default: System.out.println("Incorrect number.Try again.Enter 0 to exit"); break; } } while (choice > 1 && choice < 6); //System.out.println(choice); read.close(); nameScanner.close(); } public static void sortPerson(){ Scanner read = new Scanner(System.in); Scanner nameScanner=new Scanner(System.in); int choice = 1; System.out.println(choice); do { System.out.println("Sort a person by"); System.out.println("Enter your choice"); System.out.println("0.Exit"); System.out.println("1.Name"); System.out.println("2.Surname"); System.out.println("3.Age"); System.out.println("4.Height"); System.out.println("5.Weight"); String choicename; choice=read.nextInt(); switch (choice) { case 0: System.out.println("0.Exit"); break; case 1: System.out.println("1.Name"); Collections.sort(Person.lista,new ComparatorPersonName()); System.out.println(Person.lista); break; case 2: System.out.println("2.Surname"); Collections.sort(Person.lista,new ComparatorPersonSurname()); System.out.println(Person.lista); break; case 3: System.out.println("3.Age"); Collections.sort(Person.lista,new ComparatorPersonAge()); System.out.println(Person.lista); break; case 4: System.out.println("4.Height"); Collections.sort(Person.lista,new ComparatorPersonHeight()); System.out.println(Person.lista); break; case 5: System.out.println("5.Weight"); Collections.sort(Person.lista,new ComparatorPersonWeight()); System.out.println(Person.lista); break; default: System.out.println("Incorrect number.Try again.Enter 0 to exit"); break; } } while (choice > 1 && choice < 6); //System.out.println(choice); read.close(); nameScanner.close(); } @Override public String toString() { return "Person [name=" + name + ", surname=" + surname + ", age=" + age + ", height=" + height + ", weight=" + weight + "]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSurname() { return surname; } public void setSurname(String surname) { this.surname = surname; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } public static void deletePerson(){ Scanner read = new Scanner(System.in); Scanner nameScanner=new Scanner(System.in); int choice = 1; System.out.println(choice); do { System.out.println("Delete a person by"); System.out.println("Enter your choice"); System.out.println("0.Exit"); System.out.println("1.Name"); System.out.println("2.Surname"); System.out.println("3.Age"); System.out.println("4.Height"); System.out.println("5.Weight"); String choicename; choice=read.nextInt(); switch (choice) { case 0: System.out.println("0.Exit"); break; case 1: System.out.println("1.Name"); System.out.println("Enter Name"); choicename = nameScanner.nextLine(); for (Person person : lista) { if (person.getName().equalsIgnoreCase(choicename)) lista.remove(person); } break; case 2: System.out.println("2.Surname"); System.out.println("Enter Surname"); choicename = nameScanner.nextLine(); for (Person person : lista) { if (person.getSurname().equalsIgnoreCase(choicename)) lista.remove(person); } break; case 3: System.out.println("3.Age"); System.out.println("Enter Age"); choicename = nameScanner.nextLine(); for (Person person : lista) { if (person.getAge()==Integer.parseInt(choicename)) lista.remove(person); } break; case 4: System.out.println("4.Height"); choicename = nameScanner.nextLine(); for (Person person : lista) { if (person.getHeight()==Integer.parseInt(choicename)) lista.remove(person); } break; case 5: System.out.println("5.Weight"); choicename = nameScanner.nextLine(); for (Person person : lista) { if (person.getWeight()==Integer.parseInt(choicename)) lista.remove(person); } break; default: System.out.println("Incorrect number.Try again.Enter 0 to exit"); break; } } while (choice > 1 && choice < 6); //System.out.println(choice); read.close(); nameScanner.close(); System.out.println(Person.getAllPerson()); } public static void main(String[] args) { Person.addPerson("MichaƂ", "Makowski", 33, 175, 82); Person.addPerson("Tomasz", "Kowalski", 38, 186, 92); Person.addPerson("Dorota", "Jankowska", 30, 176, 58); String wybor = "0"; Scanner readLoop = new Scanner(System.in); Scanner read = new Scanner(System.in); String name1; String surname1; int age1; int height1; double weight1; do { System.out.println("-----------"); Person.clearConsole(); System.out.println("Enter your choice"); System.out.println("0.Exit"); System.out.println("1.Add new person"); System.out.println("2.Search for person"); System.out.println("3.Delete a person"); System.out.println("4.Search min"); System.out.println("5.Search max"); System.out.println("6.Sort list of persons"); System.out.println("7.View a list of persons"); wybor = read.nextLine(); switch (Integer.parseInt(wybor)) { case 0: System.out.println("Your choose EXIT"); break; case 1: System.out.println("Your choice 1"); System.out.println("Give name:"); name1=readLoop.nextLine(); System.out.println("Give surname:"); surname1=readLoop.nextLine(); System.out.println("Give age:"); age1=Integer.parseInt(readLoop.nextLine()); System.out.println("Give height:"); height1=Integer.parseInt(readLoop.nextLine()); System.out.println("Give weight:"); weight1=Integer.parseInt(readLoop.nextLine()); Person.addPerson(name1, surname1, age1, height1, weight1); break; case 2: System.out.println("Your choice Search for Person"); Person.searchPerson(); break; case 3: System.out.println("Your choice 3 Delete a Person"); Person.deletePerson(); break; case 4: System.out.println("Your choice 4 Search min Person"); Person.minPerson(); break; case 5: System.out.println("Your choice 5 Search max Person"); Person.maxPerson(); break; case 6: System.out.println("Your choice 6 Sort Persons by params"); //Collections.sort(Person.lista); Person.sortPerson(); System.out.println(lista); break; case 7: System.out.println("Your choice 7 View a list of Persons"); if (Person.liczba > 0) System.out.println(Person.lista); else System.out.println("The list is empty"); break; default: System.out.println("Incorrect number.Try again.Enter 0 to exit"); break; } } while (Integer.parseInt(wybor) > 0 && Integer.parseInt(wybor) < 8); readLoop.close(); read.close(); } public static void clearConsole() { try { final String os = System.getProperty("os.name"); if (os.contains("Windows")) { Runtime.getRuntime().exec("cls"); } else { Runtime.getRuntime().exec("clear"); } } catch (final Exception e) { } } @Override public int compareTo(Person o) { if (this.getSurname().compareTo(o.getSurname())<0)return -1; else if (this.getSurname().compareTo(o.getSurname())>0)return 1; return 0; } public Person(String name, String surname, int age, int height, double weight) { super(); setName(name); setSurname(surname); setAge(age); setHeight(height); setWeight(weight); } } import java.util.Comparator; public class ComparatorPersonHeight implements Comparator{ @Override public int compare(Person p1, Person p2) { if (p1.getHeight()p2.getHeight()) return 1; return 0; } } import java.util.Comparator; public class ComparatorPersonName implements Comparator{ @Override public int compare(Person p1, Person p2) { if (p1.getName().compareTo(p2.getName())<0) return -1; else if (p1.getName().compareTo(p2.getName())>0) return 1; return 0; } } import java.util.Comparator; public class ComparatorPersonSurname implements Comparator{ @Override public int compare(Person p1, Person p2) { if (p1.getSurname().compareTo(p2.getSurname())<0) return -1; else if (p1.getSurname().compareTo(p2.getSurname())>0) return 1; return 0; } } import java.util.Comparator; public class ComparatorPersonWeight implements Comparator{ @Override public int compare(Person p1, Person p2) { if (p1.getWeight()p2.getWeight()) return 1; return 0; } }