Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- * Solution
- package ru.sgu.csit;
- import java.io.*;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- public class Solution {
- public static void main(String[] args) throws IOException {
- try(BufferedReader r = new BufferedReader(new FileReader(new File("group.txt"))))
- {
- String str;
- Integer i2 = null;
- ArrayList<Student> student = null;
- Group group = new Group(student, i2);
- SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMYYYY");
- while((str=r.readLine())!=null)
- {
- String[] strParts = str.split(" ");
- if (strParts.length != 1)
- {
- Integer i1 = 0;
- try
- {
- i1 = new Integer(strParts[0]);
- }
- catch (NumberFormatException e)
- {
- System.err.println("Неверный формат строки!");
- }
- Student b = new Student(strParts[1], strParts[2], strParts[3], i1, dateFormat.parse(strParts[4]));
- if (group.isEmpty())
- {
- ArrayList<Student> arr = new ArrayList<Student>();
- arr.add(b);
- group.setStudents(arr);
- }else
- group.addStudents(b);
- }
- else
- {
- if(!group.isEmpty())
- System.out.println("Саааамый старший студент: " + group.getOldestStudent());
- else
- System.out.println("Group of students is empty");
- group.setId(i2);
- group.setStudents(student);
- try
- {
- i2 = new Integer(str);
- }
- catch (NumberFormatException e)
- {
- System.err.println("Неверный формат строки!");
- }
- group.setId(i2);
- }
- System.out.println(str);
- }
- if(!group.isEmpty())
- System.out.println("Саааамый старший студент: " + group.getOldestStudent());
- else
- System.out.println("Group of students is empty");
- }
- catch (IOException | ParseException e)
- {
- e.printStackTrace();
- }
- }
- }
- *Group
- package ru.sgu.csit;
- import java.util.ArrayList;
- public class Group {
- private ArrayList<Student> students;
- private Integer id;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public ArrayList<Student> getStudents() {
- return students;
- }
- public void setStudents(ArrayList<Student> students) {
- this.students = students;
- }
- public void addStudents(Student st){
- this.students.add(st);
- }
- public Group(ArrayList<Student> students, Integer id) {
- this.students = students;
- this.id = id;
- }
- public Student getOldestStudent()
- {
- Student maxx = students.get(0);
- for (int i = 1; i < this.students.size();i++) {
- if (maxx.getBirthday().compareTo(students.get(i).getBirthday()) > 0)
- {
- maxx = students.get(i);
- }
- }
- return maxx;
- }
- @Override
- public String toString() {
- return "Group{" +
- "students=" + students +
- ", id=" + id +
- '}';
- }
- public boolean isEmpty()
- {
- if (this.getStudents() == null)
- return true;
- else
- return false;
- }
- }
- *Student
- package ru.sgu.csit;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- public class Student {
- private String lastName;
- private String firstName;
- private String secondName;
- private Integer studentId;
- private Date birthday;
- public Date getBirthday() {
- return birthday;
- }
- public Student(String Name, String FName, String SName, Integer Id, Date DR) {
- this.lastName = Name;
- this.firstName = FName;
- this.secondName = SName;
- this.studentId = Id;
- this.birthday = DR;
- }
- @Override
- public String toString() {
- SimpleDateFormat DataFormat = new SimpleDateFormat("ddMMYYYY");
- //DataFormat.format(new Date())
- return this.studentId + " " + this.lastName + " " + this.firstName + " " + this.secondName + " " + DataFormat.format(this.birthday);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement