Skip to content

Instantly share code, notes, and snippets.

@kryvoboker
Last active August 9, 2020 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kryvoboker/4f143b0d6d3ec98a61b5703a890bdea9 to your computer and use it in GitHub Desktop.
Save kryvoboker/4f143b0d6d3ec98a61b5703a890bdea9 to your computer and use it in GitHub Desktop.
HomeWork7(Generics and Collections)(Group modifier)
public class DeleteStudentException extends Exception {
@Override
public String getMessage() {
return "Этот студент уже изгнан из группы или Вам кажется, что в группе студентов больше чем 10!";
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
/**
*
* @author kamaz
*/
public class Group implements Voenkomat {
private ArrayList<Student> student = new ArrayList<>();
private int count = 10;
public Group() {
super();
}
public ArrayList<Student> getStudent() {
return student;
}
public void setStudent(ArrayList<Student> student) {
this.student = student;
}
public void setStudent(Student st) throws GroupException {
int i = student.size();
if (i >= count) {
throw new GroupException();
}
student.add(st);
return;
}
public void writeGroup(File file) throws FileNotFoundException {
StringBuffer bf = new StringBuffer();
try (PrintWriter pw = new PrintWriter(file)) {
for (int i = 0; i < student.size(); i++) {
bf.append(student.get(i).getName() + " " + student.get(i).getSurname() + " "
+ student.get(i).getSurnameTwo() + " " + student.get(i).getAge() + " "
+ student.get(i).isSex());
bf.append(System.lineSeparator());
}
pw.print(bf);
System.out.println("Writed");
} catch (FileNotFoundException e) {
System.out.println("File not found!!!");
}
}
public Group returnGroup(File file) throws GroupException, IOException {
Group gpTwo = new Group();
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String str = " ";
for (; (str = br.readLine()) != null;) {
String[] arr = str.split(" ");
String name = arr[0];
String surname = arr[1];
String surnameTwo = arr[2];
int age = Integer.parseInt(arr[3]);
boolean sex = Boolean.valueOf(arr[4]);
Student st = new Student(name, surname, surnameTwo, age, sex);
gpTwo.setStudent(st);
}
} catch (FileNotFoundException e) {
System.out.println("File not found!");
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
} catch (IOException e) {
System.out.println("ERROR");
}
return gpTwo;
}
public void deleteStudent(int index) throws DeleteStudentException {
try {
int size = student.size();
if (size <= index) {
throw new DeleteStudentException();
}
student.remove(index);
System.out.println("Студент с места: " + (index) + " изгнан из группы");
} catch (DeleteStudentException e) {
System.out.println(e.getMessage());
}
}
public String foundStudent(int index) {
return student.get(index).getSurname();
}
@Override
public ArrayList<Student> getVon() {
ArrayList<Student> list = new ArrayList<>();
for (int i = 0; i < student.size(); i += 1) {
if (student.get(i).getAge() >= 18 && student.get(i).isSex() == true) {
list.add(student.get(i));
}
}
return list;
}
public void sortByParametrs() {
Scanner sc = new Scanner(System.in);
System.out.println("Выберите за каким параметром хотите отсортировать студентов (name, surname, surname Two, age, sex): ");
String parametr = sc.nextLine();
try {
if (parametr.equals("name")) {
student.sort(Comparator.comparing(Student::getName));
}
if (parametr.equals("surname")) {
student.sort(Comparator.comparing(Student::getSurname));
}
if (parametr.equals("surname Two")) {
student.sort(Comparator.comparing(Student::getSurnameTwo));
}
if (parametr.equals("age")) {
student.sort(Comparator.comparing(Student::getAge));
}
if (parametr.equals("sex")) {
student.sort(Comparator.comparing(Student::isSex));
} else {
throw new NotCorrectParametrException();
}
} catch (NotCorrectParametrException e) {
System.out.println(e.getMessage());
}
}
public void setNewStudent(int index) throws GroupException, Exception {
try {
Scanner sc = new Scanner(System.in);
System.out.println("Введите name");
String name = sc.nextLine();
System.out.println("Введите surname");
String surname = sc.nextLine();
System.out.println("Введите surnameTwo");
String surnameTwo = sc.nextLine();
System.out.println("Введите age");
int age = sc.nextInt();
System.out.println("Введите sex");
boolean sex = sc.nextBoolean();
Student stud = new Student(name, surname, surnameTwo, age, sex);
int i = student.size();
if (i >= count) {
throw new GroupException();
}
student.add(index, stud);
System.out.println("Студент внесен в список: " + stud);
} catch (GroupException e) {
System.out.println(e.getMessage());
} catch (Exception e) {
System.out.println("Вы ввели не корректные параметры");
}
}
@Override
public String toString() {
student.sort(Comparator.comparing(Student::getAge));
String n = String.valueOf(student);
return "Group sorted: " + n;
}
// @Override
// public String toString() {
// return "Group{" + "student=" + student + '}';
// }
}
public class GroupException extends Exception {
@Override
public String getMessage() {
return "В этой группе больше студентов не может быть!";
}
}
public class Human {
private String name;
private String surname;
private String surnameTwo;
private int age;
private boolean sex;
public Human(String name, String surname, String surnameTwo, int age, boolean sex) {
this.name = name;
this.surname = surname;
this.surnameTwo = surnameTwo;
this.age = age;
this.sex = sex;
}
public Human() {
super();
}
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 String getSurnameTwo() {
return surnameTwo;
}
public void setSurnameTwo(String surnameTwo) {
this.surnameTwo = surnameTwo;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student: " + name + ", " + surname + ", " + surnameTwo + ", " + age + ", " + sex + "; ";
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws FileNotFoundException, IOException, GroupException,
DeleteStudentException, NotCorrectParametrException, Exception {
Group gp = new Group();
Student one = new Student("Ivan", "Petrov", "Vasilievich", 18, true);
Student two = new Student("Anna", "Petrova", "Igorevna", 17, false);
Student three = new Student("Olia", "Vetrova", "Pavlovna", 21, false);
Student four = new Student("Igor", "Shvedenko", "Rostyslavovych", 19, true);
Student five = new Student("Roman", "Kryvobok", "Valerievich", 15, true);
Student six = new Student("Kostiantyn", "Khalyk", "Stepanovych", 31, true);
Student seven = new Student("Tatyana", "Bigun", "Oleksandrovna", 26, false);
Student eight = new Student("Anastasia", "Isachenko", "Valentinovna", 25, false);
Student nine = new Student("Viacheslav", "Zhyzhula", "Olegovych", 14, true);
Student ten = new Student("Kamila", "Tanchyk", "Kostiantynovna", 21, false);
Student eleven = new Student("Misha", "Bogatyriov", "Ivanovych", 30, true);
Student twelve = new Student("Oksana", "Fedorinenko", "Bogdanovna", 30, false);
gp.setStudent(one);
gp.setStudent(two);
gp.setStudent(three);
gp.setStudent(four);
gp.setStudent(five);
gp.setStudent(six);
gp.setStudent(seven);
gp.setStudent(eight);
gp.setStudent(nine);
gp.setStudent(ten);
// gp.setStudent(eleven);
// gp.setStudent(twelve);
System.out.println("Group: " + gp);
File file = new File("a.txt");
file.createNewFile();
gp.writeGroup(file);
Group gpTwo = gp.returnGroup(file);
System.out.println(gpTwo);
System.out.println();
gp.deleteStudent(0);
System.out.println(gp);
System.out.println();
gp.deleteStudent(11);
System.out.println(gp);
System.out.println();
System.out.println(gp.foundStudent(0));
System.out.println();
System.out.println("Военкомат = " + gp.getVon());
System.out.println();
// gp.sortByParametrs();
// System.out.println("Sort = " + gp);
// gp.setNewStudent(4);
// System.out.println(gp);
System.out.println();
System.out.println(gp);
}
}
public class NotCorrectParametrException extends Exception {
@Override
public String getMessage() {
return "Вы ввели не корректный параметр сортировки!";
}
}
public class Student extends Human {
public Student(String name, String surname, String surnameTwo, int age, boolean sex) {
super(name, surname, surnameTwo, age, sex);
}
public Student() {
super();
}
@Override
public String toString() {
return super.toString(); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void setSex(boolean sex) {
super.setSex(sex); //To change body of generated methods, choose Tools | Templates.
}
@Override
public boolean isSex() {
return super.isSex(); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void setAge(int age) {
super.setAge(age); //To change body of generated methods, choose Tools | Templates.
}
@Override
public int getAge() {
return super.getAge(); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void setSurnameTwo(String surnameTwo) {
super.setSurnameTwo(surnameTwo); //To change body of generated methods, choose Tools | Templates.
}
@Override
public String getSurnameTwo() {
return super.getSurnameTwo(); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void setSurname(String surname) {
super.setSurname(surname); //To change body of generated methods, choose Tools | Templates.
}
@Override
public String getSurname() {
return super.getSurname(); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void setName(String name) {
super.setName(name); //To change body of generated methods, choose Tools | Templates.
}
@Override
public String getName() {
return super.getName(); //To change body of generated methods, choose Tools | Templates.
}
}
import java.util.ArrayList;
/**
*
* @author kamaz
*/
public interface Voenkomat {
public ArrayList<Student> getVon();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment