thecoderenroute

Biodata

Apr 17th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.86 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class Test {
  6.  
  7.     Scanner sc = new Scanner(System.in);
  8.     List<Person> list = new ArrayList<>();
  9.  
  10.     public static void main(String[] args) {
  11.         int i = 0;
  12.         int len = 0;
  13.         Scanner sc = new Scanner(System.in);
  14.         Test t = new Test();
  15.         //ArrayList<Person> list = new ArrayList<>();
  16.         try {
  17.             while (true) {
  18.                 System.out.println("Enter person's name: [[-1] to stop]");
  19.                 String temp = sc.nextLine();
  20.                 if (temp.equalsIgnoreCase("-1"))
  21.                     break;
  22.                 Person p = new Person(temp);
  23.                 t.add(p);
  24.                 i++;
  25.                 len++;
  26.             }
  27.             while (true) {
  28.                 System.out.println("\n\n1. View All\n2. Filter\n3. View bio\n4. exit");
  29.                 int choice = sc.nextInt();
  30.                 switch (choice) {
  31.                     case 1:
  32.                         for (i = 0; i < len; i++) {
  33.                             System.out.println((i + 1) + ". " + t.getObj(i).name());
  34.                         }
  35.                         System.out.println();
  36.                         break;
  37.                     case 2:
  38.                         System.out.println("Filter by: ");
  39.                         System.out.println("1. Age\n2. Gender");
  40.                         int choiceFilter = sc.nextInt();
  41.                         switch (choiceFilter) {
  42.                             case 1:
  43.                                 t.filterByAge();
  44.                                 break;
  45.                             case 2:
  46.                                 t.filterByGender();
  47.                                 break;
  48.                             default:
  49.                                 System.out.println("Kya tapleek hai apko?");
  50.                         }
  51.                         break;
  52.                     case 3:
  53.                         System.out.println("Search by: ");
  54.                         System.out.println("1. Name\n2. ID");
  55.                         switch (sc.nextInt()) {
  56.                             case 1:
  57.                                 int id = t.search();
  58.                                 t.getObj(id).bio();
  59.                                 break;
  60.                             case 2:
  61.                                 t.getObj(sc.nextInt() - 1).bio();
  62.                                 break;
  63.                             default:
  64.                                 System.out.println("Kya tapleek hai apko?");
  65.                         }
  66.                         break;
  67.                     case 4:
  68.                         System.exit(0);
  69.                 }
  70.             }
  71.         } catch (Exception e) {
  72.             System.out.println(e);
  73.         }
  74.     }
  75.  
  76.     // 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
  77.     public Person getObj(int i) {
  78.         return list.get(i);
  79.     }
  80.  
  81.     public void add(Person pe) {
  82.         list.add(pe);
  83.     }
  84.  
  85.     public void filterByAge() {
  86.         System.out.println("Enter deciding age: ");
  87.         int age = Integer.valueOf(sc.nextLine());
  88.         ArrayList<String> older = new ArrayList<>();
  89.         ArrayList<String> younger = new ArrayList<>();
  90.         for (int i = 0; i < list.size(); i++) {
  91.             if (list.get(i).age() >= age) {
  92.                 older.add(list.get(i).name());
  93.             } else {
  94.                 younger.add(list.get(i).name());
  95.             }
  96.         }
  97.         System.out.println("Older: \n" + older);
  98.         System.out.println("Younger: \n" + younger);
  99.     }
  100.  
  101.     public void filterByGender() {
  102.         ArrayList<String> male = new ArrayList<>();
  103.         ArrayList<String> female = new ArrayList<>();
  104.         ArrayList<String> others = new ArrayList<>();
  105.         for (int i = 0; i < list.size(); i++) {
  106.             if (list.get(i).gender().equalsIgnoreCase("Male"))
  107.                 male.add(list.get(i).name());
  108.             else if (list.get(i).gender().equalsIgnoreCase("Female"))
  109.                 female.add(list.get(i).name());
  110.             else
  111.                 others.add(list.get(i).name());
  112.         }
  113.         System.out.println("Male: \n" + male);
  114.         System.out.println("Female: \n" + female);
  115.         System.out.println("Others: \n" + others);
  116.     }
  117.  
  118.     public int search() {
  119.         System.out.println("Enter name: ");
  120.         String name = sc.nextLine();
  121.         System.out.println(list);
  122.         System.out.println(name);
  123.         int count = 1;
  124.         for (Person i : list) {
  125.             if (i.name().equalsIgnoreCase(name)) {
  126.                 return count;
  127.             }
  128.             count++;
  129.         }
  130.         return -1;
  131.     }
  132. }
  133.  
  134. class Person {
  135.     private final String name;
  136.     private final int age;
  137.     private final char gender;
  138.     Scanner sc = new Scanner(System.in);
  139.  
  140.     public Person(String name) {
  141.         this.name = name;
  142.         System.out.println("How old is " + name);
  143.         this.age = Integer.parseInt(sc.nextLine());
  144.         System.out.println("What is " + name + "'s gender? [m/f/o]");
  145.         this.gender = sc.next().charAt(0);
  146.     }
  147.  
  148.     public String name() {
  149.         return this.name;
  150.     }
  151.  
  152.     public int age() {
  153.         return this.age;
  154.     }
  155.  
  156.     public String gender() {
  157.         switch (this.gender) {
  158.             case 'm':
  159.                 return "Male";
  160.             case 'f':
  161.                 return "Female";
  162.             case 'o':
  163.                 return "Others";
  164.             default:
  165.                 return "Confused";
  166.         }
  167.     }
  168.  
  169.     public void bio() {
  170.         System.out.println("Name: " + this.name() + "\nAge: " + this.age() + "\nGender: " + this.gender());
  171.     }
  172.     public static void main(String[] args){
  173.         Person p = new Person("Dick");
  174.         p.bio();
  175.     }
  176. }
Add Comment
Please, Sign In to add comment