Advertisement
Guest User

Untitled

a guest
May 18th, 2018
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.25 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.util.Scanner;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9. import java.io.File;
  10. import java.util.*;
  11. import javax.swing.JOptionPane;
  12. import java.io.BufferedWriter;
  13. import java.io.FileWriter;
  14. import java.util.stream.*;
  15. import java.nio.file.Paths;
  16. import java.nio.file.Files;
  17. import java.text.SimpleDateFormat;
  18. import java.time.format.*;
  19. import java.time.*;
  20. import java.util.Date.*;
  21. //import java.util.Date.before
  22.  
  23.  
  24. public class testing {
  25.  
  26. static String filepath = "C:/werkruimte/kmuren/input/kilometers.csv";
  27. static Scanner scanner = new Scanner(System.in);
  28. static String cvsSplitBy = ";";
  29. static double license1 = 0;
  30. static ArrayList<String> licenseplateList= new ArrayList<String>();
  31. static ArrayList<String> begintimeList = new ArrayList<String>();
  32. static ArrayList<String> endingtimeList = new ArrayList<String>();
  33. static ArrayList<String> kmperlineList = new ArrayList<String>();
  34. static Map<String, Double> nachtMap = new HashMap<String, Double>();
  35. static Map<String, Double> dagMap = new HashMap<String, Double>();
  36.  
  37.  
  38. public static void main(String[] args) {
  39.  
  40. BufferedReader br = null;
  41. String line;
  42.  
  43.  
  44.  
  45. try {
  46. br = new BufferedReader(new FileReader(filepath));
  47.  
  48. } catch (FileNotFoundException fnfex) {
  49.  
  50. System.out.println(fnfex.getMessage() + "Bestand niet gevonden!");
  51. System.exit(0);
  52. }
  53. int counter = 0;
  54. //this is where we read lines
  55.  
  56. try {
  57.  
  58.  
  59. while((line = br.readLine()) != null) {
  60. String[] splitter = line.split(cvsSplitBy);
  61.  
  62. licenseplateList.add(splitter[0]);
  63. begintimeList.add(splitter[2]);
  64. endingtimeList.add(splitter[3]);
  65. kmperlineList.add(splitter[8]);
  66.  
  67. counter += 1; //counts how many lines there are in the input file
  68.  
  69.  
  70. }
  71.  
  72. System.out.println(licenseplateList.size() + " " + begintimeList.size() + " " + endingtimeList.size() + " " + kmperlineList.size()); //debug
  73.  
  74.  
  75. // for each license plate
  76. for (int i = 1, n = licenseplateList.size(); i < n; i++) {
  77.  
  78. //first look at whether it is considered as day or night time
  79.  
  80.  
  81. try {
  82.  
  83. String time1Str = begintimeList.get(i);
  84. String time2Str = endingtimeList.get(i);
  85.  
  86. //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
  87. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
  88. //Assert.assertEquals(
  89. //LocalTime.parse("6:00", formatter),
  90. //LocalTime.parse("06:00", formatter)
  91. //);
  92.  
  93. LocalTime time1 = LocalTime.parse(time1Str, formatter);
  94. LocalTime time2 = LocalTime.parse(time2Str, formatter);
  95.  
  96. // add date value: start with today
  97. LocalDate today = LocalDate.now();
  98. // lower value will be assigned to today
  99. LocalDateTime dateTime1 = LocalDateTime.of(today, time1);
  100. // upper value will be assigned same day if it is after lower value
  101. // otherwise (we crossed date boundary) assign it to tomorrow
  102. LocalDateTime dateTime2 = time2.isAfter(time1) ?
  103. LocalDateTime.of(today, time2) : LocalDateTime.of(today.plusDays(1), time2);
  104.  
  105. Duration duration = Duration.between(dateTime1, dateTime2);
  106. Duration halfDuration = duration.dividedBy(2);
  107. LocalTime result = LocalTime.from(halfDuration.addTo(time1));
  108.  
  109. System.out.println(result.format(formatter));
  110.  
  111.  
  112. LocalTime calc = LocalTime.parse("06:00", formatter);
  113. LocalTime calc2 = LocalTime.parse("17:30", formatter);
  114.  
  115. //before and after , if true its night
  116. boolean before = result.format(formatter).before(calc);
  117. boolean after = result.format(formatter).after(calc2);
  118.  
  119. //if beginning time is before 06:00
  120. if (before == true) {
  121.  
  122. //does our map already contain this license?
  123. if(nachtMap.containsKey(licenseplateList.get(i))) {
  124. kmperlineList.get(i).replaceAll(",", ".");
  125.  
  126. //then remove that one, add new one but have the sum of the values
  127. //is this extra ) after ".get(i)" correct?
  128. Double sum = nachtMap.remove(licenseplateList.get(i)) + Double.parseDouble(kmperlineList.get(i));
  129. nachtMap.put(licenseplateList.get(i), sum );
  130. }
  131. //if it doesnt contain the license yet, just put it in there
  132. else {
  133. kmperlineList.get(i).replaceAll(",", ".");
  134. nachtMap.put(licenseplateList.get(i), Double.parseDouble(kmperlineList.get(i)));
  135. }
  136. }
  137. else {
  138.  
  139. //als de tijd na 17:30 is
  140. if(after == true) {
  141.  
  142.  
  143. nachtMap.put(licenseplateList.get(i), Double.parseDouble(kmperlineList.get(i)));
  144.  
  145. }
  146. //otherwise it's always day
  147. else {
  148.  
  149. //again: does the map already contain this license plate?
  150. if(dagMap.containsKey(licenseplateList.get(i))) {
  151.  
  152. kmperlineList.get(i).replaceAll(",", ".");
  153.  
  154. //then get the sum of the values, but only 1 time the key
  155. Double sum = dagMap.remove(licenseplateList.get(i)) + Double.parseDouble(kmperlineList.get(i));
  156. dagMap.put(licenseplateList.get(i), sum);
  157. }
  158.  
  159. //if the map does not contain the license plate
  160. else {
  161.  
  162. kmperlineList.get(i).replaceAll(",", ".");
  163. dagMap.put(licenseplateList.get(i), Double.parseDouble(kmperlineList.get(i)));
  164. }
  165. }
  166. }
  167.  
  168. }catch (Exception e) {
  169. System.out.println("error 1");
  170. }
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179. //if(dagMap.containsKey(licenseplateList.get(i))) {
  180.  
  181. //dagMap.put(splitter[0], dagMap.get(splitter[0]) + Double.parseDouble(splitter[8]));
  182.  
  183.  
  184. //}
  185. //else{
  186.  
  187.  
  188. //dagMap.put(licenseplateList.get(i),
  189. //dagMap.put(splitter[0], dagMap.get(splitter[0]) + Double.parseDouble(splitter[8]));
  190.  
  191.  
  192. //}
  193.  
  194.  
  195. }
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204. } catch (IOException ioex) {
  205. System.out.println(ioex.getMessage() + " Error 1");
  206. } finally {
  207.  
  208.  
  209. System.out.println(dagMap);
  210. TextOutput();
  211. System.exit(0);
  212.  
  213. }
  214. //TextOutput();
  215.  
  216. }
  217.  
  218. public static void TextOutput() {
  219. File newFile = new File("C:/werkruimte/kmuren/output/output.csv");
  220. //Bestaat het bestand al?
  221.  
  222. if (newFile.exists()) {
  223.  
  224. JOptionPane.showMessageDialog(null, "Het bestand bestaat al. Wil je deze verwijderen? ja/nee");
  225. String antwoord = scanner.nextLine();
  226.  
  227. if ((antwoord).equals("ja") ||
  228. (antwoord).equals("Ja") ||
  229. (antwoord).equals("JA"))
  230. {
  231.  
  232. try {
  233.  
  234. if(newFile.delete()) {
  235.  
  236. System.out.println(newFile.getName() + " is verwijderd!");
  237.  
  238. }
  239. else {
  240.  
  241. System.out.println("Het bestand kon niet verwijderd worden. Doe dit handmatig.");
  242.  
  243. }
  244.  
  245. }catch(Exception e){
  246.  
  247. e.printStackTrace();
  248. }
  249. fileGenerator(newFile);
  250. }
  251. else
  252. {
  253.  
  254. JOptionPane.showMessageDialog(null, "Sla het dan eerst op een andere locatie op!");
  255. }
  256. }
  257. else
  258. {
  259.  
  260. fileGenerator(newFile);
  261. }
  262. }
  263.  
  264. public static void fileGenerator(File newFile) {
  265. //Dit is ook allemaal om te kijken of het bestand al bestaat, anders komen er errors ondanks eerste if statement!
  266. try
  267. {
  268.  
  269. newFile.createNewFile();
  270. }
  271. catch(Exception e)
  272. {
  273.  
  274. e.printStackTrace();
  275. }
  276.  
  277. //Dit is ook allemaal om te kijken of het bestand al bestaat, anders komen er errors ondanks eerste if statement!
  278. try
  279. {
  280.  
  281. FileWriter FileW = new FileWriter(newFile);
  282. BufferedWriter buffW = new BufferedWriter(FileW);
  283.  
  284.  
  285.  
  286. for ( String key : dagMap.keySet() ) {
  287. double value = dagMap.get(key);
  288. buffW.write(key + ";" + value);
  289. buffW.newLine();
  290. }
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298. //hiermee close ik de buffer! Anders kan het niet in meerdere methods gebruikt worden!
  299. buffW.close();
  300. JOptionPane.showMessageDialog(null, "Gelukt! Het bestand staat klaar!");
  301. }
  302. catch (Exception e)
  303. {
  304.  
  305. e.printStackTrace();
  306. }
  307.  
  308. }
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement