Advertisement
Tevronis

Group

Sep 28th, 2015
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.91 KB | None | 0 0
  1. * Solution
  2.  
  3. package ru.sgu.csit;
  4.  
  5. import java.io.*;
  6. import java.text.ParseException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.ArrayList;
  9. import java.util.Date;
  10.  
  11. public class Solution {
  12.  
  13.     public static void main(String[] args) throws IOException {
  14.         try(BufferedReader r = new BufferedReader(new FileReader(new File("group.txt"))))
  15.         {
  16.             String str;
  17.             Integer i2 = null;
  18.             ArrayList<Student> student = null;
  19.  
  20.             Group group = new Group(student, i2);
  21.  
  22.             SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMYYYY");
  23.             while((str=r.readLine())!=null)
  24.             {
  25.                 String[] strParts = str.split(" ");
  26.                 if (strParts.length != 1)
  27.                 {
  28.  
  29.                     Integer i1 = 0;
  30.  
  31.                     try
  32.                     {
  33.                         i1 = new Integer(strParts[0]);
  34.                     }
  35.                     catch (NumberFormatException e)
  36.                     {
  37.                         System.err.println("Неверный формат строки!");
  38.                     }
  39.                     Student b = new Student(strParts[1], strParts[2], strParts[3], i1, dateFormat.parse(strParts[4]));
  40.                     if (group.isEmpty())
  41.                     {
  42.                         ArrayList<Student> arr = new ArrayList<Student>();
  43.                         arr.add(b);
  44.                         group.setStudents(arr);
  45.                     }else
  46.                     group.addStudents(b);
  47.                 }
  48.                 else
  49.                 {
  50.                     if(!group.isEmpty())
  51.                         System.out.println("Саааамый старший студент: " + group.getOldestStudent());
  52.                     else
  53.                         System.out.println("Group of students is empty");
  54.  
  55.                     group.setId(i2);
  56.                     group.setStudents(student);
  57.                     try
  58.                     {
  59.                         i2 = new Integer(str);
  60.                     }
  61.                     catch (NumberFormatException e)
  62.                     {
  63.                         System.err.println("Неверный формат строки!");
  64.                     }
  65.                     group.setId(i2);
  66.                 }
  67.                 System.out.println(str);
  68.             }
  69.             if(!group.isEmpty())
  70.                 System.out.println("Саааамый старший студент: " + group.getOldestStudent());
  71.             else
  72.                 System.out.println("Group of students is empty");
  73.  
  74.         }
  75.         catch (IOException | ParseException e)
  76.         {
  77.             e.printStackTrace();
  78.         }
  79.  
  80.     }
  81. }
  82.  
  83. *Group
  84.  
  85. package ru.sgu.csit;
  86.  
  87. import java.util.ArrayList;
  88.  
  89. public class Group {
  90.     private ArrayList<Student> students;
  91.     private Integer id;
  92.  
  93.     public Integer getId() {
  94.         return id;
  95.     }
  96.     public void setId(Integer id) {
  97.         this.id = id;
  98.     }
  99.     public ArrayList<Student> getStudents() {
  100.         return students;
  101.     }
  102.     public void setStudents(ArrayList<Student> students) {
  103.         this.students = students;
  104.     }
  105.     public void addStudents(Student st){
  106.         this.students.add(st);
  107.     }
  108.     public Group(ArrayList<Student> students, Integer id) {
  109.         this.students = students;
  110.         this.id = id;
  111.     }
  112.  
  113.     public Student getOldestStudent()
  114.     {
  115.         Student maxx = students.get(0);
  116.         for (int i = 1; i < this.students.size();i++) {
  117.             if (maxx.getBirthday().compareTo(students.get(i).getBirthday()) > 0)
  118.             {
  119.                 maxx = students.get(i);
  120.             }
  121.         }
  122.         return maxx;
  123.     }
  124.  
  125.     @Override
  126.     public String toString() {
  127.         return "Group{" +
  128.                 "students=" + students +
  129.                 ", id=" + id +
  130.                 '}';
  131.     }
  132.  
  133.     public boolean isEmpty()
  134.     {
  135.         if (this.getStudents() == null)
  136.             return true;
  137.         else
  138.             return false;
  139.     }
  140.  
  141. }
  142.  
  143. *Student
  144.  
  145. package ru.sgu.csit;
  146.  
  147. import java.text.SimpleDateFormat;
  148. import java.util.Date;
  149.  
  150. public class Student {
  151.     private String lastName;
  152.     private String firstName;
  153.     private String secondName;
  154.     private Integer studentId;
  155.     private Date birthday;
  156.  
  157.     public Date getBirthday() {
  158.         return birthday;
  159.     }
  160.  
  161.     public Student(String Name, String FName, String SName, Integer Id, Date DR) {
  162.         this.lastName = Name;
  163.         this.firstName = FName;
  164.         this.secondName = SName;
  165.         this.studentId = Id;
  166.         this.birthday = DR;
  167.     }
  168.  
  169.     @Override
  170.     public String toString() {
  171.         SimpleDateFormat DataFormat = new SimpleDateFormat("ddMMYYYY");
  172.         //DataFormat.format(new Date())
  173.         return this.studentId + " " + this.lastName + " " + this.firstName + " " + this.secondName + " " + DataFormat.format(this.birthday);
  174.     }
  175.  
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement