Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.util.Scanner;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import java.io.File;
- import java.util.*;
- import javax.swing.JOptionPane;
- import java.io.BufferedWriter;
- import java.io.FileWriter;
- import java.util.stream.*;
- import java.nio.file.Paths;
- import java.nio.file.Files;
- import java.text.SimpleDateFormat;
- import java.time.format.*;
- import java.time.*;
- import java.util.Date.*;
- //import java.util.Date.before
- public class testing {
- static String filepath = "C:/werkruimte/kmuren/input/kilometers.csv";
- static Scanner scanner = new Scanner(System.in);
- static String cvsSplitBy = ";";
- static double license1 = 0;
- static ArrayList<String> licenseplateList= new ArrayList<String>();
- static ArrayList<String> begintimeList = new ArrayList<String>();
- static ArrayList<String> endingtimeList = new ArrayList<String>();
- static ArrayList<String> kmperlineList = new ArrayList<String>();
- static Map<String, Double> nachtMap = new HashMap<String, Double>();
- static Map<String, Double> dagMap = new HashMap<String, Double>();
- public static void main(String[] args) {
- BufferedReader br = null;
- String line;
- try {
- br = new BufferedReader(new FileReader(filepath));
- } catch (FileNotFoundException fnfex) {
- System.out.println(fnfex.getMessage() + "Bestand niet gevonden!");
- System.exit(0);
- }
- int counter = 0;
- //this is where we read lines
- try {
- while((line = br.readLine()) != null) {
- String[] splitter = line.split(cvsSplitBy);
- licenseplateList.add(splitter[0]);
- begintimeList.add(splitter[2]);
- endingtimeList.add(splitter[3]);
- kmperlineList.add(splitter[8]);
- counter += 1; //counts how many lines there are in the input file
- }
- System.out.println(licenseplateList.size() + " " + begintimeList.size() + " " + endingtimeList.size() + " " + kmperlineList.size()); //debug
- // for each license plate
- for (int i = 1, n = licenseplateList.size(); i < n; i++) {
- //first look at whether it is considered as day or night time
- try {
- String time1Str = begintimeList.get(i);
- String time2Str = endingtimeList.get(i);
- //if values in the csv file contain for example 6:00 instead of 06:00, change HH:mm to H:mm and uncomment the lines below
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
- //Assert.assertEquals(
- //LocalTime.parse("6:00", formatter),
- //LocalTime.parse("06:00", formatter)
- //);
- LocalTime time1 = LocalTime.parse(time1Str, formatter);
- LocalTime time2 = LocalTime.parse(time2Str, formatter);
- // add date value: start with today
- LocalDate today = LocalDate.now();
- // lower value will be assigned to today
- LocalDateTime dateTime1 = LocalDateTime.of(today, time1);
- // upper value will be assigned same day if it is after lower value
- // otherwise (we crossed date boundary) assign it to tomorrow
- LocalDateTime dateTime2 = time2.isAfter(time1) ?
- LocalDateTime.of(today, time2) : LocalDateTime.of(today.plusDays(1), time2);
- Duration duration = Duration.between(dateTime1, dateTime2);
- Duration halfDuration = duration.dividedBy(2);
- LocalTime result = LocalTime.from(halfDuration.addTo(time1));
- System.out.println(result.format(formatter));
- LocalTime calc = LocalTime.parse("06:00", formatter);
- LocalTime calc2 = LocalTime.parse("17:30", formatter);
- //before and after , if true its night
- boolean before = result.format(formatter).before(calc);
- boolean after = result.format(formatter).after(calc2);
- //if beginning time is before 06:00
- if (before == true) {
- //does our map already contain this license?
- if(nachtMap.containsKey(licenseplateList.get(i))) {
- kmperlineList.get(i).replaceAll(",", ".");
- //then remove that one, add new one but have the sum of the values
- //is this extra ) after ".get(i)" correct?
- Double sum = nachtMap.remove(licenseplateList.get(i)) + Double.parseDouble(kmperlineList.get(i));
- nachtMap.put(licenseplateList.get(i), sum );
- }
- //if it doesnt contain the license yet, just put it in there
- else {
- kmperlineList.get(i).replaceAll(",", ".");
- nachtMap.put(licenseplateList.get(i), Double.parseDouble(kmperlineList.get(i)));
- }
- }
- else {
- //als de tijd na 17:30 is
- if(after == true) {
- nachtMap.put(licenseplateList.get(i), Double.parseDouble(kmperlineList.get(i)));
- }
- //otherwise it's always day
- else {
- //again: does the map already contain this license plate?
- if(dagMap.containsKey(licenseplateList.get(i))) {
- kmperlineList.get(i).replaceAll(",", ".");
- //then get the sum of the values, but only 1 time the key
- Double sum = dagMap.remove(licenseplateList.get(i)) + Double.parseDouble(kmperlineList.get(i));
- dagMap.put(licenseplateList.get(i), sum);
- }
- //if the map does not contain the license plate
- else {
- kmperlineList.get(i).replaceAll(",", ".");
- dagMap.put(licenseplateList.get(i), Double.parseDouble(kmperlineList.get(i)));
- }
- }
- }
- }catch (Exception e) {
- System.out.println("error 1");
- }
- //if(dagMap.containsKey(licenseplateList.get(i))) {
- //dagMap.put(splitter[0], dagMap.get(splitter[0]) + Double.parseDouble(splitter[8]));
- //}
- //else{
- //dagMap.put(licenseplateList.get(i),
- //dagMap.put(splitter[0], dagMap.get(splitter[0]) + Double.parseDouble(splitter[8]));
- //}
- }
- } catch (IOException ioex) {
- System.out.println(ioex.getMessage() + " Error 1");
- } finally {
- System.out.println(dagMap);
- TextOutput();
- System.exit(0);
- }
- //TextOutput();
- }
- public static void TextOutput() {
- File newFile = new File("C:/werkruimte/kmuren/output/output.csv");
- //Bestaat het bestand al?
- if (newFile.exists()) {
- JOptionPane.showMessageDialog(null, "Het bestand bestaat al. Wil je deze verwijderen? ja/nee");
- String antwoord = scanner.nextLine();
- if ((antwoord).equals("ja") ||
- (antwoord).equals("Ja") ||
- (antwoord).equals("JA"))
- {
- try {
- if(newFile.delete()) {
- System.out.println(newFile.getName() + " is verwijderd!");
- }
- else {
- System.out.println("Het bestand kon niet verwijderd worden. Doe dit handmatig.");
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- fileGenerator(newFile);
- }
- else
- {
- JOptionPane.showMessageDialog(null, "Sla het dan eerst op een andere locatie op!");
- }
- }
- else
- {
- fileGenerator(newFile);
- }
- }
- public static void fileGenerator(File newFile) {
- //Dit is ook allemaal om te kijken of het bestand al bestaat, anders komen er errors ondanks eerste if statement!
- try
- {
- newFile.createNewFile();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- //Dit is ook allemaal om te kijken of het bestand al bestaat, anders komen er errors ondanks eerste if statement!
- try
- {
- FileWriter FileW = new FileWriter(newFile);
- BufferedWriter buffW = new BufferedWriter(FileW);
- for ( String key : dagMap.keySet() ) {
- double value = dagMap.get(key);
- buffW.write(key + ";" + value);
- buffW.newLine();
- }
- //hiermee close ik de buffer! Anders kan het niet in meerdere methods gebruikt worden!
- buffW.close();
- JOptionPane.showMessageDialog(null, "Gelukt! Het bestand staat klaar!");
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement