Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package class_2;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
- import java.util.Scanner;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class Class_2 {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = null;
- try {
- reader = new BufferedReader(new FileReader("C:\\Users\\L-PC\\Desktop\\лекции и все подобное\\Технология программирования\\Викины\\классы 2\\class_2\\catalog.txt"));
- } catch (FileNotFoundException ex) {
- Logger.getLogger(Class_2.class.getName()).log(Level.SEVERE, null, ex);
- }
- String line;
- List<String> lines;
- lines = new ArrayList<>();
- while ((line = reader.readLine()) != null) {
- lines.add(line);
- }
- ArrayList<Student> students = new ArrayList<>();
- for (int i = 0; i < lines.size(); i++) {
- String[] fields = lines.get(i).split(",");
- Student student = new Student();
- student.fname = fields[0];
- student.lname = fields[1];
- student.year = Integer.parseInt(fields[2]);
- student.numbers = Long.parseLong(fields[3]);
- students.add(student);
- }
- String choice;
- do {
- String fname, ch, lname;
- int year;
- long numbers;
- System.out.println("Выберите действие\n1.Вывести список студентов\n2.Добавить студента в список\n3.Удалить студента\n4.Отсортировать список по определенным полям\n5.Найти студента(-ов) по имени или году рождения\n6.Выход");
- Scanner scan = new Scanner(System.in);
- choice = scan.nextLine();
- switch (choice) {
- case "1":
- System.out.println("Весь список:");
- for (int i = 0; i < students.size(); i++) {
- students.get(i).output();
- }
- break;
- case "2":
- Student student = new Student();
- System.out.println("Введите имя: ");
- fname = scan.nextLine();
- student.fname = fname;
- System.out.println("Введите фамилию: ");
- lname = scan.nextLine();
- student.lname = lname;
- System.out.println("Введите год рождения: ");
- year = scan.nextInt();
- student.year = year;
- System.out.println("Введите номер телефона: ");
- numbers = scan.nextLong();
- student.numbers = numbers;
- // System.out.println(numbers);
- students.add(student);
- break;
- case "3":
- System.out.println("Введите имя студента, которого хотите удалить из списка:");
- fname = scan.nextLine();
- for (int i = 0; i < students.size(); i++) {
- if (students.get(i).fname.equals(fname)) {
- students.remove(i);
- break;
- }
- }
- break;
- case "4":
- System.out.println("По какому полю сортировать?:\n1. По имени.\n2. По году рождения");
- ch = scan.nextLine();
- if (ch.equals("1")) {
- Collections.sort(students, new Comparator<Student>() {
- @Override
- public int compare(Student o1, Student o2) {
- return o1.fname.compareTo(o2.fname);
- }
- });
- }
- if (ch.equals("2")) {
- Student buf;
- for (int i = students.size() - 1; i >= 1; i--) {
- for (int j = 0; j < i; j++) {
- if (students.get(j + 1) != null) {
- if (students.get(j).year > students.get(j + 1).year) {
- buf = students.get(j);
- students.set(j, students.get(j + 1));
- students.set(j + 1, buf);
- }
- }
- }
- }
- }
- for (int i = 0; i < students.size(); i++) {
- if (students.get(i) != null) {
- students.get(i).output();
- }
- }
- break;
- case "5":
- System.out.println("По какому полю искать?:\n1. По имени.\n2. По году рождения");
- ch = scan.nextLine();
- if (ch.equals("1")) {
- System.out.println("Введите имя студента:");
- fname = scan.nextLine();
- for (int i = 0; i < students.size(); i++) {
- if (students.get(i) != null) {
- if (students.get(i).fname.equals(fname)) {
- students.get(i).output();
- }
- }
- }
- }
- if (ch.equals("2")) {
- System.out.println("Введите год рождения:");
- year = scan.nextInt();
- for (int i = 0; i < students.size(); i++) {
- if (students.get(i) != null) {
- if (students.get(i).year == year) {
- students.get(i).output();
- }
- }
- }
- }
- break;
- }
- } while (choice.equals("6") != true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement