2019 május idegen nyelvű érettségi - Tantárgyfelosztás

  ←   Vissza

   

  ↓   Java fájl letöltése



A feladat forráskódja:

		  package tanfel;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.util.Pair;

class Class implements Comparable<Class> {
    private final int year;
    private final String letter;
    private final boolean wholeYear;

    public Class(String line) throws IllegalArgumentException {
        String[] strArray = line.split("\\."); // regex
        if (strArray.length != 2) {
            throw new IllegalArgumentException();
        }
        this.year = Integer.parseInt(strArray[0]);
        this.letter = strArray[1];
        this.wholeYear = "x".equals(letter);
    }

    @Override
    public int compareTo(Class other) {
        if (this.year == other.year) {
            return this.letter.compareTo(other.letter);
        }
        return this.year - other.year;
    }

    @Override
    public String toString() {
        return year + "." + letter;
    }

    public boolean isWholeYear() {
        return wholeYear;
    }
    
    /**
     * Ez a metódus mutatja meg, hogy a jelen osztály évfolyam szinten 
     * megegyezhet-e a paraméterül adott osztállyal.
     * @param other Egy osztály név. Semmiképp nem "x"-es, azaz a wholeYear-je 
     * false.
     * @return Ez a példány "x"-es és az évfolyam is megegyezik.
     */
    public boolean matchesWith(Class other) {
        if (!this.wholeYear) {
            return false;
        }
        return this.year == other.year;
    }
}

class Teacher implements Comparable<Teacher> {
    private final String name;
    private final String subject;
    private final Class cls;
    private final int lesson;

    public Teacher(String[] lines) throws IllegalArgumentException {
        if (lines.length != 4) {
            throw new IllegalArgumentException();
        }
        this.name = lines[0];
        this.subject = lines[1];
        this.cls = new Class(lines[2]);
        this.lesson = Integer.parseInt(lines[3]);
    }

    @Override
    public int compareTo(Teacher other) {
        return this.name.compareTo(other.name);
    }

    public String getName() {
        return name;
    }

    public String getSubject() {
        return subject;
    }

    public Class getCls() {
        return cls;
    }
    
    public String getClassAsString() {
        return cls.toString();
    }

    public int getLesson() {
        return lesson;
    }
}

/**
 * Info érettségi 2019.05. idegen nyelvű.
 * @author comtur
 */
public class Tanfel {

    private static List<Teacher> container;
    private static Scanner sc;
    
    private final static String INFILE = "beosztas.txt";
    private final static String OUTFILE = "of.txt";
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println("Info érettségi 2019.05. idegen nyelvű.");
        task1();
        task2();
        task3();
        task4();
        task5();
        task6();
        task7();
    }

    private static void task1() {
        taskNum(1);
        System.out.println("(adatok beolvasása)");
        container = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(INFILE))) {
            String line;
            String[] data = new String[4];
            while ((line = br.readLine()) != null) {
                data[0] = line;
                for (int i = 1; i <= 3; i++) {
                    data[i] = br.readLine();
                }
                container.add(new Teacher(data));
            }
        } catch (IOException ex) {
            Logger.getLogger(Tanfel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static void taskNum(int i) {
        System.out.println("\n" + i + ". feladat:");
    }

    private static void task2() {
        taskNum(2);
        System.out.println("A fájlban " + container.size() + " bejegyzés van.");
    }

    private static void task3() {
        taskNum(3);
        int sum = 0;
        for (Teacher teacher : container) {
            sum += teacher.getLesson();
        }
        System.out.println("Az iskolában a heti összóraszám: " + sum);
    }

    private static void task4() {
        taskNum(4);
        sc = new Scanner(System.in);
        System.out.print("Egy tanár neve= ");
        String chosenTeacher = sc.nextLine();
        int sum = 0;
        for (Teacher teacher : container) {
            if (teacher.getName().equals(chosenTeacher)) {
                sum += teacher.getLesson();
            }
        }
        System.out.println("A tanár heti óraszáma: " + sum);
    }

    private static void task5() {
        taskNum(5);
        System.out.println("(adatok fájlba írása)");
        List<Pair<Class, String>> list = new ArrayList<>();
        String leader = "osztalyfonoki";
        for (Teacher teacher : container) {
            if (leader.equals(teacher.getSubject())) {
                list.add(new Pair<>(teacher.getCls(), teacher.getName()));
            }
        }
        Collections.sort(list, new Comparator<Pair<Class, String>>() {
            @Override
            public int compare(Pair<Class, String> obj1, Pair<Class, String> obj2) {
                return obj1.getKey().compareTo(obj2.getKey());
            }
        });
        
        try (PrintWriter pw = new PrintWriter(OUTFILE)) {
            for (Pair<Class, String> pair : list) {
                pw.printf("%s - %s%n", pair.getKey(), pair.getValue());
            }
        } catch (IOException ex) {
            Logger.getLogger(Tanfel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static void task6() {
        taskNum(6);
        System.out.print("Osztály= ");
        String chosenClassAsString = sc.nextLine();
        Class chosenClass = new Class(chosenClassAsString);
        System.out.print("Tantárgy= ");
        String chosenSubject = sc.nextLine();
        boolean found = false;
        int i = 0;
        while (!found && i < container.size()) {
            if (chosenSubject.equals(container.get(i).getSubject()) && 
                    container.get(i).getCls().matchesWith(chosenClass)) {
                found = true;
            }
            i++;
        }
        if (found) {
            System.out.println("Osztályszinten tanulják."); // az egész osztály egyben, van "x"-es bejegyzés.
        } else {
            System.out.println("Csoportbontásban tanulják."); // az osztályok csoportonként, nincs "x"-es bejegyzés.
        }
    }

    private static void task7() {
        taskNum(7);
        Set<String> set = new HashSet<>();
        for (Teacher teacher : container) {
            set.add(teacher.getName());
        }
        System.out.println("Az iskolában " + set.size() + " tanár tanít.");
    }
    
}