2006 február emelt érettségi - Telefonszámla

  ←   Vissza

   

  ↓   Java fájl letöltése



A feladat forráskódja:

		  package szamla;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;

class Call {
	
    private Date from;
    private Date to;
    private String phoneNumber;

    public Call(int h1, int m1, int s1, int h2, int m2, int s2, String phoneNumber) {
        this.from = new Date(0, 0, 0, h1, m1, s1);
        this.to = new Date(0, 0, 0, h2, m2, s2);
        this.phoneNumber = phoneNumber;
    }
    
    /**
     * Hívás hossza, percben
     * @return hossz
     */
    public int getDuration() {
        long durMillis = to.getTime() - from.getTime();
        int durMinutes = ((int)(durMillis / 1000 / 60));
        if (durMillis % (1000 * 60) != 0) {
            durMinutes++;
        }
        return durMinutes;
    }
    
    /**
     * Mobil hívás-e
     * @return mobil-e
     */
    public boolean isMobile() {
        return (phoneNumber.startsWith("39") || phoneNumber.startsWith("41") || phoneNumber.startsWith("71"));
    }
    
    /**
     * Csúcsidőben van-e
     * @return csúcsidőben van
     */
    public boolean isInHighZone() {
        boolean highZone = false;
        if ((from.after(new Date(0, 0, 0, 7, 0, 0)) || from.equals(new Date(0, 0, 0, 7, 0, 0)))
                && from.before(new Date(0, 0, 0, 18, 0, 0))) {
            highZone = true;
        }
        return highZone;
    }
    
    public double getPrice() {
        double[][] prices = {{30.0, 15.0}, {69.175, 46.675}};
        return getDuration() * (isMobile() ? (isInHighZone() ? prices[1][0] : prices[1][1])
                : (isInHighZone() ? prices[0][0] : prices[0][1]));
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }
}

/**
 *
 * @author comtur
 */
public class Szamla {
    
    private static Scanner sc;
    private static List<Call> callList;

    public static void main(String[] args) {
        task1();
        task2();
        task3456();
    }

    private static void task1() {
        taskNum(1);
        sc = new Scanner(System.in);
        System.out.print("Adjon meg egy telefonszámot: ");
        Call call = new Call(0, 0, 0, 0, 0, 0, sc.nextLine());
        System.out.println("A megadott szám " + (call.isMobile() ? "" : "nem ") + "mobil.");
    }

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

    private static void task2() {
        taskNum(2);
        System.out.print("Kérek egy időpontot a beszélgetés kezdetére, szóközzel elválasztva (óra perc mperc): ");
        String start = sc.nextLine();
        System.out.print("Kérek egyet a beszélgetés végére is (óra perc mperc): ");
        String end = sc.nextLine();
        String[] callTimes = (start + " " + end).split(" ");
        int[] times = new int[callTimes.length];
        for (int i = 0; i < callTimes.length; i++) {
            times[i] = Integer.parseInt(callTimes[i]);
        }
        Call call = new Call(times[0], times[1], times[2], times[3], times[4], times[5], null);
        System.out.printf("A beszélgetés %d perces.%n", call.getDuration());
    }

    private static void task3456() {
        taskNum(3);
        System.out.println("(Adatok beolvasása, majd \"percek.txt\" állomány létrehozása)");
        
        int inHighZone = 0;
        int notInHighZone = 0;
        int mobileCalls = 0;
        int wireCalls = 0;
        double sumPrice = 0;
        
        try (BufferedReader br = new BufferedReader(new FileReader("HIVASOK.TXT"))) {
            callList = new ArrayList<>();
            String line = null;
            while ((line = br.readLine()) != null) {
                String[] callTimes = line.split(" ");
                int[] times = new int[callTimes.length];
                for (int i = 0; i < callTimes.length; i++) {
                    times[i] = Integer.parseInt(callTimes[i]);
                }
                String callNumber = br.readLine();
                Call call = new Call(times[0], times[1], times[2], times[3], times[4], times[5], callNumber);
                callList.add(call);
                
                if (call.isInHighZone()) {
                    inHighZone++;
                    sumPrice += call.getPrice();
                } else {
                    notInHighZone++;
                }
                if (call.isMobile()) {
                    mobileCalls += call.getDuration();
                } else {
                    wireCalls += call.getDuration();
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(Szamla.class.getName()).log(Level.SEVERE, null, ex);
            System.err.println("Hiba! " + ex.getMessage());
        }
        try (PrintWriter pw = new PrintWriter("percek.txt")) {
            for (Call call : callList) {
                pw.println(call.getDuration() + " " + call.getPhoneNumber());
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Szamla.class.getName()).log(Level.SEVERE, null, ex);
            System.err.println("Hiba! " + ex.getMessage());
        }
        
        taskNum(4);
        System.out.printf("Csúcsidőben %d, csúcsidőn kívül pedig %d hívás történt.%n",
                inHighZone, notInHighZone);
        
        taskNum(5);
        System.out.printf("A felhasználó %d percet beszélt mobilszámokkal és %d percet vezetékessel.%n",
                mobileCalls, wireCalls);
        
        taskNum(6);
        System.out.printf("A felhasználónak %.3f Ft díjat kell fizetnie a csúcsidős hívások után.%n",
                sumPrice);
        int ceil = (int)Math.ceil(sumPrice);
        System.out.printf("Egész forintra felfelé kerekítve ez %d Ft.%n", ceil);
    }
}