You have problems not with algorithm, but with basic programming principles (for start read about GRASP), etc. with PLO. You wrote a perfectly unreadable code on a very trivial task you couldn't even figure it out yourself. In fact, the challenge was to find medium arithmetic. First, remember the car reads any code. Your main task is to make the code readable to a man. That is why there are all programming paradigms, patters, etc. Reading is what matters! And the first thing that affects her is the rules of title, which is why it is very important to respect them. Classes are written with a big letter, methods and variables with a small one. All names must be meaningful. If you've written an exclusive code from the standpoint of the PLO, but your variables are called a,b,c,d, all your efforts are in vain, and your code needs to be thrown into the urn for the same reason - it's not readable! Next rule : ghetters and grids - well-established and commonly accepted signatures, the netters are always void. You want to do what you've done, use the pattern pooler, but don't change the signatures of commonly accepted methods. There's a lot of reasons. I will call one (the rest of you will understand over time): the behaviour of any engineering system you have built must be intuitively understandable and expected. For example, you want to move all the elements of the collection. You can go through the normal cycle and that's okay. However, you can implement your terator, which will make all the elements differently. The man who will watch your code with the normal cycle will figure it out in seconds. The castom terator will raise a fair question - why did you do that? Maybe a special order of the elements, etc. would complicate reading, similar to the ghetters and the netters, their signatures are as expected and predictable as to push further logic or change the signature. When you learn that, go to the recommended GRASP, and now to practice, your class model, describing school, according to the GRASP information expert, is obliged to have all the necessary methods to work with their fields (the information is being processed where the facility is stored). That's why we're going to have to consider the average arithmetic for our fields and we're not going to consider it as an outward method.import java.util.Locale;
import java.util.stream.DoubleStream;
public class School {
private int number;
private double mathematics;
private double russianLanguage;
private double computerScience;
private double average;
public School(int number, double mathematics, double russianLanguage, double computerScience) {
this.number = number;
this.mathematics = mathematics;
this.russianLanguage = russianLanguage;
this.computerScience = computerScience;
this.average();
}
public final School average() {
this.average = DoubleStream.of(mathematics, russianLanguage, computerScience).average().getAsDouble();
return this;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public double getMathematics() {
return mathematics;
}
public void setMathematics(double mathematics) {
this.mathematics = mathematics;
}
public double getRussianLanguage() {
return russianLanguage;
}
public void setRussianLanguage(double russianLanguage) {
this.russianLanguage = russianLanguage;
}
public double getComputerScience() {
return computerScience;
}
public void setComputerScience(double computerScience) {
this.computerScience = computerScience;
}
public double getAverage() {
return average;
}
@Override
public String toString() {
return String.format(Locale.ENGLISH,"Школа № " + number + " математика - " +
"%.1f" + " русский язык - " + "%.1f" + " инфрматика - " + "%.1f" + " общий средний балл - " + "%.1f",
mathematics, russianLanguage,computerScience,average);
}
}
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Map;
import java.util.HashMap;
import java.util.stream.Collectors;
public class SchoolService {
private final Map<Integer, School> schools;
public SchoolService() {
this.schools = new HashMap<>();
}
public SchoolService add(int number, double mathematics, double russianLanguage, double computerScience) {
if (schools.containsKey(number)) {
final School school = schools.get(number);
school.setMathematics(avarage(school.getMathematics(), mathematics));
school.setRussianLanguage(avarage(school.getRussianLanguage(), russianLanguage));
school.setComputerScience(avarage(school.getComputerScience(), computerScience));
school.average();
}
else schools.put(number, new School(number, mathematics, russianLanguage, computerScience));
return this;
}
private double avarage (double...args) {
return Arrays.stream(args).average().getAsDouble();
}
public Collection<School> get() {
return schools.values();
}
public Collection<School> get(Comparator<School> comparator) {
return schools.values().stream().sorted(comparator).collect(Collectors.toList());
}
}
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
new SchoolService()
.add(32, 80d, 80d, 80d)
.add(71, 91d, 89d, 100d)
.add(1, 15d, 60d, 30d)
.add(32, 100d, 100d, 100d)
.add(1, 75d, 99d, 67d)
.add(17, 78d, 87d, 77d)
.add(89, 66d, 54d, 78d)
.get(Comparator.comparingDouble(School::getAverage).reversed())
.forEach(System.out::println);
}
}
Now it's okay, from the point of view of business logic, yes, but from the point of view of programming, I'd like to draw attention to the designer of the class model, it takes one int and three doubles, which is not very good, because it can be easily confused in the order of the parameters, so first, try to implement your caste biller for this class.