Created
June 10, 2016 12:08
-
-
Save YanchevskayaAnna/f4c7ca25da8ba600252b10242f7a0de6 to your computer and use it in GitHub Desktop.
Group_AleksandrP
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 ClassWork.week1.day2.Student.Comparators; | |
import ClassWork.week1.day2.Student.Student; | |
import java.util.Comparator; | |
/** | |
* Created by dfsdfsddfsdf on 10.06.16. | |
*/ | |
public class AverageMarkComparator implements Comparator<Student> { | |
@Override | |
public int compare(Student o1, Student o2) { | |
if( o1.getAverageMark() - o2.getAverageMark() < 0) return -1; | |
//if( o1.getAverageMark() - o2.getAverageMark() < 1) return 1; | |
//return (int) (o1.getAverageMark() - o2.getAverageMark()); | |
//Yanchevskaya A. | |
if( o1.getAverageMark() - o2.getAverageMark() > 0) return 1; | |
return 0; | |
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 ClassWork.week1.day2.Student; | |
import ClassWork.week1.day2.Student.Comparators.AverageMarkComparator; | |
import ClassWork.week1.day2.Student.Comparators.DateComparator; | |
import ClassWork.week1.day2.Student.Comparators.NameComparator; | |
import ClassWork.week1.day2.Student.Comparators.SurnameComparator; | |
import java.lang.reflect.Array; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Comparator; | |
/** | |
* Created by Pustik41 on 22.05.16. | |
*/ | |
public class Group { | |
private static final int DEFAULT_GROUP_SIZE = 20; | |
private int studentsCounter; //Yanchevskaya A. можно не вести подсчет, это сделает ArrayList | |
private String name; | |
private ArrayList<Student> students; //Yanchevskaya A. абстрагируем private List<Student> students | |
public Group(String name) { | |
this.name = name; | |
this.students = new ArrayList<Student>(DEFAULT_GROUP_SIZE); | |
} | |
public Group(String name, int groupSize) { | |
this.name = name; | |
this.students = new ArrayList<Student>(groupSize); | |
} | |
public Group(String name, ArrayList<Student> students) { //Yanchevskaya A. абстрагируем List<Student> students | |
this.name = name; | |
this.students = students; //Yanchevskaya A. Проверки на null | |
} | |
public boolean addStudent(Student student) { | |
if (student == null) return false; | |
if (!searchStudent(student)) { | |
students.add(student); | |
studentsCounter++; //Yanchevskaya A. Где будет использоваться studentsCounter? У ArrayList уже есть готовый size, при необходимости можно его использовать | |
return true; | |
} | |
return false; | |
} | |
public void showGroup() { | |
for (Student st: students) { | |
System.out.println(st.toString()); | |
} | |
} | |
public void sortStudents(Comparator comparator) { //Yanchevskaya A. См. как в Женя в классе сделал этот метод | |
if(comparator instanceof NameComparator){ //Yanchevskaya A. Код можно сократить, если через или проверить тип компаратора | |
students.sort(new NameComparator()); //Yanchevskaya A. Компаратор по имени чем-то отличается от compareTo в студенте? если нет, компаратор можно не делать и передавать null | |
return; | |
} | |
if(comparator instanceof SurnameComparator){ | |
students.sort(new SurnameComparator()); | |
return; | |
} | |
if(comparator instanceof DateComparator){ | |
students.sort(new DateComparator()); | |
return; | |
} | |
if(comparator instanceof AverageMarkComparator){ | |
students.sort(new AverageMarkComparator()); | |
return; | |
} | |
System.out.println("Not valid Comparator"); | |
} | |
public boolean searchStudent(Student student) { | |
if(student != null) { | |
return students.contains(student); | |
} | |
return false; | |
} | |
public boolean delStudent(Student student) { | |
if (name == null || name.equals("")) { //Yanchevskaya A. name или student? | |
System.out.println("Entered not valid value"); | |
} else { | |
if (searchStudent(student)) { | |
students.remove(student); //Yanchevskaya A. remove сам проверяет есть ли элемент в массиве, search можно не делать, проверки на null также можно не делать | |
studentsCounter--; //Yanchevskaya A. studentsCounter - лишний счетчик | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment