Created
February 18, 2020 13:51
-
-
Save GlebGomenyuk/f107deacd958c399134b90169959b3da to your computer and use it in GitHub Desktop.
HW
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.g; | |
import java.util.List; | |
import java.io.Serializable; | |
import java.util.Arrays; | |
import java.util.Iterator; | |
import java.util.LinkedList; | |
import Exception.MyDelException; | |
import Exception.MyException; | |
import Exception.MyserchExeption; | |
public class Group implements Serializable { | |
List<Student> studentCollection = new LinkedList<>(); | |
int maxSizeGroup = 10; | |
public Group() { | |
super(); | |
// TODO Auto-generated constructor stub | |
} | |
public Group(List students) { | |
super(); | |
this.studentCollection = students; | |
} | |
public List getStudents() { | |
return studentCollection; | |
} | |
public void setStudents(List students) { | |
this.studentCollection = students; | |
} | |
public void addStudent(Student student) throws MyException { | |
if (student == null) { | |
throw new MyException(); | |
} | |
if (studentCollection.size() >= maxSizeGroup) { | |
throw new MyException(); | |
} | |
studentCollection.add(student); | |
} | |
public void delStudent(int dellIndex) throws MyDelException { | |
if (dellIndex > studentCollection.size()) { | |
throw new MyDelException("Вы пытаетесь удалить студента которого нет в группе "); | |
} | |
if (studentCollection.get(dellIndex) != null) { | |
Student st = studentCollection.get(dellIndex); | |
studentCollection.remove(dellIndex); | |
System.out.println("Student " + st.getName() + " Удален"); | |
} | |
} | |
public Student serch(String name) throws MyserchExeption { | |
for (Student studen : studentCollection) { | |
if (studen.getName().equals(name)) { | |
return studen; | |
} | |
} | |
for (Student studen : studentCollection) { | |
if (!studen.getName().equals(name)) { | |
throw new MyserchExeption(); | |
} | |
} | |
return null; | |
} | |
public void sort() { | |
studentCollection.sort(new StudentComparator()); | |
} | |
@Override | |
public String toString() { | |
sort(); | |
StringBuilder sb = new StringBuilder(); | |
for (Student student : studentCollection) { | |
if (student != null) { | |
sb.append(student); | |
sb.append(System.lineSeparator()); | |
} | |
} | |
return sb.toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.g; | |
import java.io.Serializable; | |
public class Human implements Serializable{ | |
private String name; | |
private String surname; | |
private int age; | |
public Human(String name, String surname, int age) { | |
super(); | |
this.name = name; | |
this.surname = surname; | |
this.age = age; | |
} | |
public Human() { | |
super(); | |
// TODO Auto-generated constructor stub | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getSurname() { | |
return surname; | |
} | |
public void setSurname(String surname) { | |
this.surname = surname; | |
} | |
public int getAge() { | |
return age; | |
} | |
public void setAge(int age) { | |
this.age = age; | |
} | |
public void getInfo() { | |
System.out.println("Human [name=" + name + ", surname=" + surname + ", age=" + age + "]"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.g; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.ObjectInputStream; | |
import java.io.ObjectOutputStream; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.List; | |
import Exception.MyDelException; | |
import Exception.MyException; | |
import Exception.MyserchExeption; | |
public class Main { | |
public static void main(String[] args) throws ClassNotFoundException, NullPointerException { | |
// List<Student> studentCollection = new LinkedList<>(); | |
// studentCollection.add(new Student("Igor", "Brilov", 21, "NAU", "YIB", 2, 231)); | |
// studentCollection.add(new Student("Egor", "Vlasov", 20, "NAU", "YIB", 2, 231)); | |
// studentCollection.add(new Student("Georg", "Manykyan", 20, "NAU", "YIB", 2, 231)); | |
// studentCollection.add(new Student("Boris", "Akynin", 20, "NAU", "YIB", 2, 231)); | |
// studentCollection.add(new Student("Alex", "Clar", 22, "NAU", "YIB", 2, 231)); | |
// studentCollection.add(new Student("Lana", "DelRey", 22, "NAU", "YIB", 2, 231)); | |
// studentCollection.add(new Student("Lina", "Cravetc", 19, "NAU", "YIB", 2, 231)); | |
// studentCollection.add(new Student("Thom", "York", 20, "NAU", "YIB", 2, 231)); | |
// studentCollection.add(new Student("John", "Lenon", 20, "NAU", "YIB", 2, 231)); | |
// studentCollection.add(new Student("Pol", "Jons", 20, "NAU", "YIB", 2, 231)); | |
// studentCollection.add(new Student("Pol", "Jons", 20, "NAU", "YIB", 2, 231)); | |
Student firstStuden = new Student("Igor", "Brilov", 21, "NAU", "YIB", 2, 231); | |
Student secondStuden = new Student("Egor", "Vlasov", 20, "NAU", "YIB", 2, 231); | |
Student thirdStuden = new Student("Georg", "Manykyan", 20, "NAU", "YIB", 2, 231); | |
Student foursStuden = new Student("Boris", "Akynin", 20, "NAU", "YIB", 2, 231); | |
Student fifthStuden = new Student("Alex", "Clar", 22, "NAU", "YIB", 2, 231); | |
Student sixthStuden = new Student("Lana", "DelRey", 22, "NAU", "YIB", 2, 231); | |
Student seventhStuden = new Student("Lina", "Cravetc", 19, "NAU", "YIB", 2, 231); | |
Student eigthStuden = new Student("Thom", "York", 20, "NAU", "YIB", 2, 231); | |
Student ninthStuden = new Student("John", "Lenon", 20, "NAU", "YIB", 2, 231); | |
Student tenthStuden = new Student("Pol", "Jons", 20, "NAU", "YIB", 2, 231); | |
Student eleventhStuden = new Student("Pol", "Jons", 20, "NAU", "YIB", 2, 231); | |
Student eleventhStudenn = new Student(); | |
Group firstGroup = new Group(); | |
try { | |
firstGroup.addStudent(firstStuden); | |
firstGroup.addStudent(firstStuden); | |
firstGroup.addStudent(firstStuden); | |
firstGroup.addStudent(firstStuden); | |
firstGroup.addStudent(foursStuden); | |
firstGroup.addStudent(fifthStuden); | |
firstGroup.addStudent(sixthStuden); | |
firstGroup.addStudent(seventhStuden); | |
firstGroup.addStudent(eigthStuden); | |
firstGroup.addStudent(tenthStuden); | |
firstGroup.addStudent(eleventhStudenn); | |
firstGroup.addStudent(eleventhStuden); | |
firstGroup.addStudent(eleventhStuden); | |
} catch (MyException e) { | |
System.out.println(e.getMessage()); | |
} | |
try { | |
firstGroup.delStudent(224); | |
} catch (MyDelException e) { | |
System.out.println(e.getMessage()); | |
} | |
try { | |
System.out.println("Нашли " + firstGroup.serch("Igor")); | |
System.out.println("Нашли " + firstGroup.serch("yu")); | |
} catch (MyserchExeption e) { | |
System.out.println(e.getMessage()); | |
} | |
try (ObjectOutputStream OOS = new ObjectOutputStream(new FileOutputStream("weri"))) { | |
OOS.writeObject(firstGroup); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
Group f = null; | |
try (ObjectInputStream OIS = new ObjectInputStream(new FileInputStream("weri"))) { | |
f = (Group) OIS.readObject(); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException | ClassNotFoundException e) { | |
e.printStackTrace(); | |
} | |
System.out.println(f); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.g; | |
import java.util.Comparator; | |
public class StudentComparator implements Comparator { | |
@Override | |
public int compare(Object o1, Object o2) { | |
Student st1 = (Student) o1; | |
Student st2 = (Student) o2; | |
if (st1 == null && st2 != null) { | |
return -1; | |
} | |
if (st1 != null && st2 == null) { | |
return 1; | |
} | |
if (st1 == null && st2 == null) { | |
return 0; | |
} | |
if (st1.getSurname().compareToIgnoreCase(st2.getSurname()) > 0) { | |
return 1; | |
} | |
if (st1.getSurname().compareToIgnoreCase(st2.getSurname()) < 0) { | |
return -1; | |
} | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment