Last active
June 30, 2020 12:00
-
-
Save kryvoboker/229a2db4a345a52f77e5eee63bdad06e to your computer and use it in GitHub Desktop.
Students
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
public class DeleteStudentException extends Exception { | |
@Override | |
public String getMessage() { | |
return "Этот студент уже изгнан из группы!"; | |
} | |
} |
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
import java.util.Arrays; | |
/** | |
* | |
* @author kamaz | |
*/ | |
public class Group { | |
private Student[] array = new Student[10]; | |
public Group(Student[] array) { | |
this.array = array; | |
} | |
public Group() { | |
super(); | |
} | |
public Student[] getArray() { | |
return array; | |
} | |
public void setArray(Student[] array) { | |
this.array = array; | |
} | |
public void setStudent(Student st, int index) throws GroupException, GroupExceptionTwo { | |
int indexTwo = 10; | |
try { | |
for (int i = 0; i < array.length; i++) { | |
if ((i == index) && (array[i] == null)) { | |
array[i] = st; | |
System.out.println("Место студента " + st + " в группе: " + (i + 1)); | |
} else if ((i == index) && (array[i] != null)) { | |
throw new GroupExceptionTwo(); | |
} else if (indexTwo == index && (indexTwo++) == index) { | |
throw new GroupException(); | |
} | |
} | |
} catch (GroupExceptionTwo e) { | |
System.out.println(e.getMessage()); | |
} catch (GroupException e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
public void deleteStudent(int index) throws DeleteStudentException { | |
try { | |
for (int i = 0; i < array.length; i++) { | |
if ((i == index) && (array[i] != null)) { | |
array[i] = null; | |
System.out.println("Студент с места: " + (i + 1) + " изгнан из группы"); | |
} else if ((i == index) && (array[i] == null)) { | |
throw new DeleteStudentException(); | |
} | |
} | |
} catch (DeleteStudentException e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
public int countStudentInGroup() { | |
int count = 0; | |
for (int i = 0; i < array.length; i++) { | |
if (array[i] != null) { | |
count++; | |
} | |
} | |
return count; | |
} | |
public String foundStudent(int index) { | |
String n = null; | |
for (int i = 0; i < array.length; i++) { | |
if ((array[i] != null) && (i == index)) { | |
n = array[i].getSurname(); | |
} | |
} | |
return n; | |
} | |
@Override | |
public String toString() { | |
int j = 0; | |
String[] arraySurname = new String[countStudentInGroup()]; | |
for (int i = 0; i < array.length; i++) { | |
if (array[i] != null) { | |
arraySurname[j] = array[i].getSurname(); | |
j++; | |
} | |
} | |
Arrays.sort(arraySurname); | |
return "Group: " + Arrays.toString(arraySurname); | |
} | |
} |
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
public class GroupException extends Exception { | |
@Override | |
public String getMessage() { | |
return "В этой группе больше студентов не может быть!"; | |
} | |
} |
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
public class GroupExceptionTwo extends Exception { | |
@Override | |
public String getMessage() { | |
return "Это место в группе уже занято!"; | |
} | |
} |
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
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 "Human {" + " name = " + name + '}'; | |
} | |
} |
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
public class Main { | |
public static void main(String[] args) throws GroupException, GroupExceptionTwo, DeleteStudentException { | |
Student one = new Student("Ivan", "Petrov", "Vasilievich", 18, true); | |
Student two = new Student("Anna", "Petrova", "Igorevna", 38, 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", 25, 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", 34, 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); | |
Group a = new Group(); | |
a.setStudent(one, 0); | |
a.setStudent(two, 1); | |
a.setStudent(three, 2); | |
a.setStudent(four, 3); | |
a.setStudent(five, 4); | |
a.setStudent(six, 5); | |
a.setStudent(seven, 6); | |
a.setStudent(eight, 7); | |
a.setStudent(nine, 8); | |
a.setStudent(ten, 9); | |
a.setStudent(eleven, 10); | |
a.setStudent(twelve, 9); | |
System.out.println("-------------"); | |
System.out.println(a); | |
System.out.println("-------------"); | |
System.out.println(a.foundStudent(9)); | |
System.out.println("-------------"); | |
a.deleteStudent(0); | |
// a.deleteStudent(0); | |
a.deleteStudent(2); | |
// a.deleteStudent(2); | |
System.out.println(a); | |
} | |
} |
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
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. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment