2018 május idegen nyelvű érettségi - Fogadóóra

  ←   Vissza

   

  ↓   Java fájl letöltése



A feladat forráskódja:

		  package fogado;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

class Schedule {
    private String name;
    private LocalTime time;
    private LocalDate reserveDate;
    private LocalTime reserveTime;

    public Schedule(String name1, String name2, int timeHour, int timeMinute, 
            int year, int month, int day, int resHour, int resMinute) {
        this.name = name1 + " " + name2;
        this.time = LocalTime.of(timeHour, timeMinute);
        this.reserveDate = LocalDate.of(year, month, day);
        this.reserveTime = LocalTime.of(resHour, resMinute);
    }

    public String getName() {
        return name;
    }

    public LocalTime getTime() {
        return time;
    }
    
    public LocalDateTime getReserveDateTime() {
        return LocalDateTime.of(reserveDate, reserveTime);
    }
    
    public void print() {
        System.out.println("Tanár neve: " + getName()
                + "\nFoglalt időpont: " + time
                + "\nFoglalás ideje: " + reserveDate.getYear() + "." 
                + reserveDate.getMonthValue() + "." + reserveDate.getDayOfMonth()
                + "-" + reserveTime);
    }
}

/**
 *
 * @author comtur
 */
public class Fogado {

    /**
     * A lista, amiben a bemeneti adatokat tároljuk.
     */
    private static List<Schedule> scheduleList;
    
    /**
     * Felhasználótól való beolvasáshoz.
     */
    private static Scanner sc;
    
    public static void main(String[] args) {
        System.out.println("Info érettségi 2018. 05. idegen nyelvű.");
        task1();
        task2();
        task3();
        task4();
        task5();
        task6();
    }

    private static void task1() {
        taskNum(1);
        String fn = "fogado.txt";
        System.out.println("(Adatok beolvasása \"" + fn + "\" fájlból)");
        try (BufferedReader br = new BufferedReader(new FileReader(fn))) {
            String line;
            scheduleList = new ArrayList<>();
            while ((line = br.readLine()) != null) {
                String[] splittedLine = line.
                        replace(":", " ").
                        replace("-", " ").
                        replace(".", " ").
                        split(" ");
                Schedule s = new Schedule(splittedLine[0], splittedLine[1], 
                        Integer.parseInt(splittedLine[2]), 
                        Integer.parseInt(splittedLine[3]), 
                        Integer.parseInt(splittedLine[4]),
                        Integer.parseInt(splittedLine[5]), 
                        Integer.parseInt(splittedLine[6]), 
                        Integer.parseInt(splittedLine[7]), 
                        Integer.parseInt(splittedLine[8]));
                scheduleList.add(s);
            }
        } catch (IOException ex) {
            System.err.println("Hiba: " + ex);
        }
    }

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

    private static void task2() {
        taskNum(2);
        System.out.println("Foglalások száma: " + scheduleList.size());
    }

    private static void task3() {
        taskNum(3);
        sc = new Scanner(System.in);
        System.out.print("Adjon meg egy nevet: ");
        String name = sc.nextLine();
        
        int sum = 0;
        for (Schedule schedule : scheduleList) {
            if (schedule.getName().toLowerCase().equals(name.toLowerCase())) {
                sum++;
            }
        }
        if (sum > 0) {
            System.out.printf("%s néven %d időpontfoglalás van.\n", name, sum);
        } else {
            System.out.println("A megadott néven nincs időpontfoglalás.");
        }
    }

    private static void task4() {
        taskNum(4);
        boolean valid = false;
        String time = null;
        do {
            System.out.print("Adjon meg egy érvényes időpontot (pl: 17:10): ");
            time = sc.nextLine();
            valid = isValid(time);
        } while (!valid);
        
        String userHour = time.substring(0, 2);
        String userMinute = time.substring(3, 5);
        LocalTime userTime = LocalTime.of(Integer.parseInt(userHour), 
                Integer.parseInt(userMinute));
        
        Set<String> teacherSet = new HashSet<>();
        for (Schedule schedule : scheduleList) {
            if (schedule.getTime().compareTo(userTime) == 0) {
                teacherSet.add(schedule.getName());
            }
        }
        
        List<String> teacherList = new ArrayList<>(teacherSet.size());
        teacherSet.forEach((String name) -> {
            teacherList.add(name);
        });
        
        teacherList.sort((String t1, String t2) -> t1.compareTo(t2));
        
        try (PrintWriter pw = new PrintWriter(userHour + userMinute + ".txt")) {
            teacherList.forEach((teacher) -> {
                System.out.println(teacher);
                pw.println(teacher);
            });
        } catch (FileNotFoundException ex) {
            System.err.println("Hiba: " + ex);
        }
    }

    private static boolean isValid(String time) {
        return time.charAt(0) == '1' && (time.charAt(1) == '6' || time.charAt(1) == '7') && 
                time.charAt(2) == ':' && 
                time.charAt(3) >= '0' && time.charAt(3) <= '5' &&
                time.charAt(4) == '0';
    }

    private static void task5() {
        taskNum(5);
        int firstReserveIndex = 0;
        for (int i = 1; i < scheduleList.size(); i++) {
            if (scheduleList.get(i).getReserveDateTime().isBefore(
                    scheduleList.get(firstReserveIndex).getReserveDateTime())) {
                firstReserveIndex = i;
            }
        }
        scheduleList.get(firstReserveIndex).print();
    }

    private static void task6() {
        taskNum(6);
        Map<LocalTime, Boolean> freeTimeMap = new HashMap<>();
        for (int i = 16; i < 18; i++) {
            for (int j = 0; j < 60; j += 10) {
                freeTimeMap.put(LocalTime.of(i, j), Boolean.TRUE);
            }
        }
        
        String barnaEszter = "Barna Eszter";
        for (Schedule schedule : scheduleList) {
            if (schedule.getName().equals(barnaEszter)) {
                freeTimeMap.put(schedule.getTime(), Boolean.FALSE);
            }
        }
        
        LocalTime lastReserved = null;
        Iterator<LocalTime> freeTimeMapIter = freeTimeMap.keySet().iterator();
        List<LocalTime> freeTimeList = new ArrayList<>();
        while (freeTimeMapIter.hasNext()) {
            LocalTime lt = freeTimeMapIter.next();
            if (!freeTimeMap.get(lt)) {
                if (lastReserved == null || lastReserved.isBefore(lt)) {
                    lastReserved = lt;
                }
            } else {
                freeTimeList.add(lt);
            }
        }
        freeTimeList.sort((LocalTime t1, LocalTime t2) -> t1.compareTo(t2));
        for (LocalTime localTime : freeTimeList) {
            System.out.println(localTime);
        }
        
        System.out.println(barnaEszter + " legkorábban távozhat: " + lastReserved.plusMinutes(10l));
    }
    
}