Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class Test {
- Scanner sc = new Scanner(System.in);
- List<Person> list = new ArrayList<>();
- public static void main(String[] args) {
- int i = 0;
- int len = 0;
- Scanner sc = new Scanner(System.in);
- Test t = new Test();
- //ArrayList<Person> list = new ArrayList<>();
- try {
- while (true) {
- System.out.println("Enter person's name: [[-1] to stop]");
- String temp = sc.nextLine();
- if (temp.equalsIgnoreCase("-1"))
- break;
- Person p = new Person(temp);
- t.add(p);
- i++;
- len++;
- }
- while (true) {
- System.out.println("\n\n1. View All\n2. Filter\n3. View bio\n4. exit");
- int choice = sc.nextInt();
- switch (choice) {
- case 1:
- for (i = 0; i < len; i++) {
- System.out.println((i + 1) + ". " + t.getObj(i).name());
- }
- System.out.println();
- break;
- case 2:
- System.out.println("Filter by: ");
- System.out.println("1. Age\n2. Gender");
- int choiceFilter = sc.nextInt();
- switch (choiceFilter) {
- case 1:
- t.filterByAge();
- break;
- case 2:
- t.filterByGender();
- break;
- default:
- System.out.println("Kya tapleek hai apko?");
- }
- break;
- case 3:
- System.out.println("Search by: ");
- System.out.println("1. Name\n2. ID");
- switch (sc.nextInt()) {
- case 1:
- int id = t.search();
- t.getObj(id).bio();
- break;
- case 2:
- t.getObj(sc.nextInt() - 1).bio();
- break;
- default:
- System.out.println("Kya tapleek hai apko?");
- }
- break;
- case 4:
- System.exit(0);
- }
- }
- } catch (Exception e) {
- System.out.println(e);
- }
- }
- // since main is satic and list is declared outside static context, THe following to method are used to fetch objects from the list and add objects to the list
- public Person getObj(int i) {
- return list.get(i);
- }
- public void add(Person pe) {
- list.add(pe);
- }
- public void filterByAge() {
- System.out.println("Enter deciding age: ");
- int age = Integer.valueOf(sc.nextLine());
- ArrayList<String> older = new ArrayList<>();
- ArrayList<String> younger = new ArrayList<>();
- for (int i = 0; i < list.size(); i++) {
- if (list.get(i).age() >= age) {
- older.add(list.get(i).name());
- } else {
- younger.add(list.get(i).name());
- }
- }
- System.out.println("Older: \n" + older);
- System.out.println("Younger: \n" + younger);
- }
- public void filterByGender() {
- ArrayList<String> male = new ArrayList<>();
- ArrayList<String> female = new ArrayList<>();
- ArrayList<String> others = new ArrayList<>();
- for (int i = 0; i < list.size(); i++) {
- if (list.get(i).gender().equalsIgnoreCase("Male"))
- male.add(list.get(i).name());
- else if (list.get(i).gender().equalsIgnoreCase("Female"))
- female.add(list.get(i).name());
- else
- others.add(list.get(i).name());
- }
- System.out.println("Male: \n" + male);
- System.out.println("Female: \n" + female);
- System.out.println("Others: \n" + others);
- }
- public int search() {
- System.out.println("Enter name: ");
- String name = sc.nextLine();
- System.out.println(list);
- System.out.println(name);
- int count = 1;
- for (Person i : list) {
- if (i.name().equalsIgnoreCase(name)) {
- return count;
- }
- count++;
- }
- return -1;
- }
- }
- class Person {
- private final String name;
- private final int age;
- private final char gender;
- Scanner sc = new Scanner(System.in);
- public Person(String name) {
- this.name = name;
- System.out.println("How old is " + name);
- this.age = Integer.parseInt(sc.nextLine());
- System.out.println("What is " + name + "'s gender? [m/f/o]");
- this.gender = sc.next().charAt(0);
- }
- public String name() {
- return this.name;
- }
- public int age() {
- return this.age;
- }
- public String gender() {
- switch (this.gender) {
- case 'm':
- return "Male";
- case 'f':
- return "Female";
- case 'o':
- return "Others";
- default:
- return "Confused";
- }
- }
- public void bio() {
- System.out.println("Name: " + this.name() + "\nAge: " + this.age() + "\nGender: " + this.gender());
- }
- public static void main(String[] args){
- Person p = new Person("Dick");
- p.bio();
- }
- }
Add Comment
Please, Sign In to add comment