Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.65 KB | None | 0 0
  1. package HomeWrok;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class HomeWork {
  7.  
  8.     static class Person{
  9.         private String name;
  10.         private String type;
  11.  
  12.         public Person(String name, String type) {
  13.             this.name = name;
  14.             this.type = type;
  15.         }
  16.  
  17.         public String getName() {
  18.             return name;
  19.         }
  20.  
  21.         public void setName(String name) {
  22.             this.name = name;
  23.         }
  24.  
  25.         public String getType() {
  26.             return type;
  27.         }
  28.  
  29.         public void setType(String type) {
  30.             this.type = type;
  31.         }
  32.  
  33.         @Override
  34.         public String toString() {
  35.             return String.format("%s is %s", this.name, this.type);
  36.         }
  37.     }
  38.  
  39.     public static void main(String[] args) {
  40.  
  41.         List<Person> people = new ArrayList<>();
  42.  
  43.         Person normalPerson1 = new Person("Gosho", "normalPerson");
  44.         Person normalPerson2 = new Person("Pesho", "normalPerson");
  45.         Person professor1 = new Person("Einstein", "Professor");
  46.         Person professor2 = new Person("Tesla", "Professor");
  47.         Person student1 = new Person("Mariika", "Student");
  48.         Person student2 = new Person("Ivancho", "Student");
  49.         Person richStudent1 = new Person("Richy", "RichStudent");
  50.         Person richStudent2 = new Person("GoldyBoy", "RichStudent");
  51.  
  52.         people.add(normalPerson1);
  53.         people.add(normalPerson2);
  54.         people.add(professor1);
  55.         people.add(professor2);
  56.         people.add(student1);
  57.         people.add(student2);
  58.         people.add(richStudent1);
  59.         people.add(richStudent2);
  60.  
  61.         showStats(people);
  62.  
  63.     }
  64.  
  65.     private static void showStats(List<Person> people) {
  66.         int professorCounter = 0;
  67.         int studentCounter = 0;
  68.         int richStudentCounter = 0;
  69.  
  70.         for (Person person : people) {
  71.             if (person.getType().equals("Student")){
  72.                 studentCounter++;
  73.                 study(person);
  74.             }else if (person.getType().equals("Professor")){
  75.                 professorCounter++;
  76.             }else if (person.getType().equals("RichStudent")){
  77.                 richStudentCounter++;
  78.                 study(person);
  79.             }
  80.         }
  81.  
  82.         System.out.printf("Students are: %d%n", studentCounter);
  83.         System.out.printf("Rich Students are: %d%n", richStudentCounter);
  84.         System.out.printf("Professors are: %d%n", professorCounter);
  85.         System.out.printf("List contains: %d people %n", people.size());
  86.     }
  87.  
  88.     private static void study(Person person) {
  89.         System.out.println(person.toString());
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement