Advertisement
memchik

Main

Jun 14th, 2021
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.72 KB | None | 0 0
  1. package class_2;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.Collections;
  9. import java.util.Comparator;
  10. import java.util.List;
  11. import java.util.Scanner;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14.  
  15. public class Class_2 {
  16.  
  17.         public static void main(String[] args) throws IOException {
  18.         BufferedReader reader = null;
  19.         try {
  20.             reader = new BufferedReader(new FileReader("C:\\Users\\L-PC\\Desktop\\лекции и все подобное\\Технология программирования\\Викины\\классы 2\\class_2\\catalog.txt"));
  21.         } catch (FileNotFoundException ex) {
  22.             Logger.getLogger(Class_2.class.getName()).log(Level.SEVERE, null, ex);
  23.         }
  24.         String line;
  25.         List<String> lines;
  26.         lines = new ArrayList<>();
  27.         while ((line = reader.readLine()) != null) {
  28.             lines.add(line);
  29.         }
  30.  
  31.         ArrayList<Student> students = new ArrayList<>();
  32.         for (int i = 0; i < lines.size(); i++) {
  33.             String[] fields = lines.get(i).split(",");
  34.             Student student = new Student();
  35.             student.fname = fields[0];
  36.             student.lname = fields[1];
  37.             student.year = Integer.parseInt(fields[2]);
  38.             student.numbers = Long.parseLong(fields[3]);
  39.             students.add(student);
  40.         }
  41.  
  42.         String choice;
  43.         do {
  44.             String fname, ch, lname;
  45.             int year;
  46.             long numbers;
  47.             System.out.println("Выберите действие\n1.Вывести список студентов\n2.Добавить студента в список\n3.Удалить студента\n4.Отсортировать список по определенным полям\n5.Найти студента(-ов) по имени или году рождения\n6.Выход");
  48.             Scanner scan = new Scanner(System.in);
  49.             choice = scan.nextLine();
  50.             switch (choice) {
  51.                 case "1":
  52.                     System.out.println("Весь список:");
  53.                     for (int i = 0; i < students.size(); i++) {
  54.                         students.get(i).output();
  55.                     }
  56.                     break;
  57.                 case "2":
  58.                     Student student = new Student();
  59.                     System.out.println("Введите имя: ");
  60.                     fname = scan.nextLine();
  61.                     student.fname = fname;
  62.                     System.out.println("Введите фамилию: ");
  63.                     lname = scan.nextLine();
  64.                     student.lname = lname;
  65.                     System.out.println("Введите год рождения: ");
  66.                     year = scan.nextInt();
  67.                     student.year = year;
  68.                     System.out.println("Введите номер телефона: ");
  69.                     numbers = scan.nextLong();
  70.                     student.numbers = numbers;
  71. //                    System.out.println(numbers);
  72.                     students.add(student);
  73.                     break;
  74.                 case "3":
  75.                     System.out.println("Введите имя студента, которого хотите удалить из списка:");
  76.                     fname = scan.nextLine();
  77.                     for (int i = 0; i < students.size(); i++) {
  78.                         if (students.get(i).fname.equals(fname)) {
  79.                             students.remove(i);
  80.                             break;
  81.                         }
  82.                     }
  83.                     break;
  84.                 case "4":
  85.                     System.out.println("По какому полю сортировать?:\n1. По имени.\n2. По году рождения");
  86.                     ch = scan.nextLine();
  87.                     if (ch.equals("1")) {
  88.                         Collections.sort(students, new Comparator<Student>() {
  89.  
  90.                             @Override
  91.                             public int compare(Student o1, Student o2) {
  92.                                 return o1.fname.compareTo(o2.fname);
  93.                             }
  94.                         });
  95.                     }
  96.                     if (ch.equals("2")) {
  97.                         Student buf;
  98.                         for (int i = students.size() - 1; i >= 1; i--) {
  99.                             for (int j = 0; j < i; j++) {
  100.                                 if (students.get(j + 1) != null) {
  101.                                     if (students.get(j).year > students.get(j + 1).year) {
  102.                                         buf = students.get(j);
  103.                                         students.set(j, students.get(j + 1));
  104.                                         students.set(j + 1, buf);
  105.                                     }
  106.                                 }
  107.                             }
  108.                         }
  109.                     }
  110.                     for (int i = 0; i < students.size(); i++) {
  111.                         if (students.get(i) != null) {
  112.                             students.get(i).output();
  113.                         }
  114.                     }
  115.                     break;
  116.                 case "5":
  117.                     System.out.println("По какому полю искать?:\n1. По имени.\n2. По году рождения");
  118.                     ch = scan.nextLine();
  119.                     if (ch.equals("1")) {
  120.                         System.out.println("Введите имя студента:");
  121.                         fname = scan.nextLine();
  122.                         for (int i = 0; i < students.size(); i++) {
  123.                             if (students.get(i) != null) {
  124.                                 if (students.get(i).fname.equals(fname)) {
  125.                                     students.get(i).output();
  126.                                 }
  127.                             }
  128.                         }
  129.                     }
  130.                     if (ch.equals("2")) {
  131.                         System.out.println("Введите год рождения:");
  132.                         year = scan.nextInt();
  133.  
  134.                         for (int i = 0; i < students.size(); i++) {
  135.                             if (students.get(i) != null) {
  136.                                 if (students.get(i).year == year) {
  137.                                     students.get(i).output();
  138.                                 }
  139.                             }
  140.                         }
  141.                     }
  142.                     break;
  143.             }
  144.         } while (choice.equals("6") != true);
  145.     }
  146.     }
  147.    
  148.  
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement