Advertisement
Guest User

Untitled

a guest
Apr 30th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1.  
  2. import java.awt.FlowLayout;
  3. import java.util.*;
  4. import java.io.BufferedReader;
  5. import java.io.FileReader;
  6. import java.io.File;
  7. import java.io.BufferedWriter;
  8. import java.io.FileWriter;
  9. import java.io.IOException;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.*;
  13. import java.awt.*;
  14.  
  15.  
  16. /*
  17. * Dit programma heeft als functie het automatisch genereren van een functionele planning.
  18. *
  19. */
  20.  
  21. public class newFile extends JFrame {
  22.  
  23.  
  24. static Scanner scanner = new Scanner(System.in); //wordt gebruikt voor alle variabelen, anders dan de postcodedata
  25. private static Scanner x; //wordt gebruikt voor het lezen van de postcodedata
  26. static ArrayList<String> kentekensList = new ArrayList<String>();
  27. static ArrayList<Double> kilometersList = new ArrayList<>(Arrays.asList());
  28.  
  29. public static void main(String[] args) throws Exception{
  30.  
  31.  
  32. JFrame frame = new JFrame("Planner");
  33. String username = JOptionPane.showInputDialog(frame, "Username:");
  34. String password = JOptionPane.showInputDialog("Password:");
  35. while(true){
  36. if((username).equals("test") &&
  37. (password).equals("test")) {
  38. ReadSearch();
  39. //TextOutput();
  40. break;
  41. }
  42. else
  43. {
  44. JOptionPane.showMessageDialog(null, "Wrong password or username");
  45. username = JOptionPane.showInputDialog(frame, "Username:");
  46. password = JOptionPane.showInputDialog("Password:");
  47. }
  48. }
  49. }
  50.  
  51.  
  52.  
  53. //Deze methode zoekt naar de postcodes en bijbehorende gegevens
  54. public static void ReadSearch(String kenteken){
  55.  
  56. String filepath = "C:/werkruimte/kmuren/input/test.txt";
  57. String finalKenteken = ""; String date = ""; String begin = ""; String ending = ""; String junk3 = ""; String junk4 = ""; String junk5 = ""; String junk6 = ""; double totalKM = 0; String junk7 = ""; String junk8 = "";
  58. String junk9 = ""; String junk10 = ""; String junk11 = ""; String junk12 = "";
  59.  
  60. String searchTerm = kenteken;
  61. boolean found = false;
  62. double kilometers = 0;
  63.  
  64. try
  65. {
  66.  
  67. x = new Scanner(new File(filepath));
  68. x.useDelimiter("[;\n]");
  69.  
  70.  
  71. while(x.hasNext() ||
  72. x.hasNextDouble())
  73. {
  74.  
  75. finalKenteken = x.next();
  76. date = x.next();
  77. begin = x.next();
  78. ending = x.next();
  79. junk3 = x.next();
  80. junk4 = x.next();
  81. junk5 = x.next();
  82. junk6 = x.next();
  83. totalKM = x.nextDouble();
  84. junk7 = x.next();
  85. junk8 = x.next();
  86. junk9 = x.next();
  87. junk10 = x.next();
  88. junk11 = x.next();
  89. junk12 = x.next();
  90.  
  91. kentekensList.add(finalKenteken);
  92. kilometersList.add(kilometers);
  93.  
  94. }
  95.  
  96. }
  97. catch(Exception e)
  98. {
  99.  
  100. System.out.println("error");
  101.  
  102. }
  103. TextOutput();
  104.  
  105. }
  106.  
  107.  
  108. public static void TextOutput() {
  109. File newFile = new File("C:/werkruimte/kmuren/output/output.txt");
  110. //Bestaat het bestand al?
  111. if (newFile.exists()) {
  112.  
  113. JOptionPane.showMessageDialog(null, "Het bestand bestaat al. Wil je deze verwijderen? ja/nee");
  114. String antwoord = scanner.nextLine();
  115.  
  116. if ((antwoord).equals("ja") ||
  117. (antwoord).equals("Ja") ||
  118. (antwoord).equals("JA"))
  119. {
  120.  
  121. try {
  122.  
  123. if(newFile.delete()) {
  124.  
  125. System.out.println(newFile.getName() + " is verwijderd!");
  126.  
  127. }
  128. else {
  129.  
  130. System.out.println("Het bestand kon niet verwijderd worden. Doe dit handmatig.");
  131.  
  132. }
  133.  
  134. }catch(Exception e){
  135.  
  136. e.printStackTrace();
  137. }
  138. fileGenerator(newFile);
  139. }
  140. else
  141. {
  142.  
  143. JOptionPane.showMessageDialog(null, "Sla het dan eerst op een andere locatie op! planner/sources/output/output");
  144. }
  145. }
  146. else
  147. {
  148.  
  149. fileGenerator(newFile);
  150. }
  151. }
  152.  
  153.  
  154. public static void fileGenerator(File newFile) {
  155. //Dit is ook allemaal om te kijken of het bestand al bestaat, anders komen er errors ondanks eerste if statement!
  156. try
  157. {
  158.  
  159. newFile.createNewFile();
  160. }
  161. catch(Exception e)
  162. {
  163.  
  164. e.printStackTrace();
  165. }
  166.  
  167. //Dit is ook allemaal om te kijken of het bestand al bestaat, anders komen er errors ondanks eerste if statement!
  168. try
  169. {
  170.  
  171. FileWriter FileW = new FileWriter(newFile);
  172. BufferedWriter buffW = new BufferedWriter(FileW);
  173.  
  174.  
  175. for (int i = 0; i < kentekensList.size(); i++)
  176. {
  177.  
  178. buffW.write(kentekensList + ", " + kilometersList);
  179. buffW.newLine();
  180. }
  181.  
  182.  
  183. //hiermee close ik de buffer! Anders kan het niet in meerdere methods gebruikt worden!
  184. buffW.close();
  185. JOptionPane.showMessageDialog(null, "Gelukt! Het bestand staat klaar!");
  186. }
  187. catch (Exception e)
  188. {
  189.  
  190. e.printStackTrace();
  191. }
  192.  
  193. }
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement