Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.FlowLayout;
- import java.util.*;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.File;
- import java.io.BufferedWriter;
- import java.io.FileWriter;
- import java.io.IOException;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.*;
- import java.awt.*;
- /*
- * Dit programma heeft als functie het automatisch genereren van een functionele planning.
- *
- */
- public class newFile extends JFrame {
- static Scanner scanner = new Scanner(System.in); //wordt gebruikt voor alle variabelen, anders dan de postcodedata
- private static Scanner x; //wordt gebruikt voor het lezen van de postcodedata
- static ArrayList<String> kentekensList = new ArrayList<String>();
- static ArrayList<Double> kilometersList = new ArrayList<>(Arrays.asList());
- public static void main(String[] args) throws Exception{
- JFrame frame = new JFrame("Planner");
- String username = JOptionPane.showInputDialog(frame, "Username:");
- String password = JOptionPane.showInputDialog("Password:");
- while(true){
- if((username).equals("test") &&
- (password).equals("test")) {
- ReadSearch();
- //TextOutput();
- break;
- }
- else
- {
- JOptionPane.showMessageDialog(null, "Wrong password or username");
- username = JOptionPane.showInputDialog(frame, "Username:");
- password = JOptionPane.showInputDialog("Password:");
- }
- }
- }
- //Deze methode zoekt naar de postcodes en bijbehorende gegevens
- public static void ReadSearch(String kenteken){
- String filepath = "C:/werkruimte/kmuren/input/test.txt";
- String finalKenteken = ""; String date = ""; String begin = ""; String ending = ""; String junk3 = ""; String junk4 = ""; String junk5 = ""; String junk6 = ""; double totalKM = 0; String junk7 = ""; String junk8 = "";
- String junk9 = ""; String junk10 = ""; String junk11 = ""; String junk12 = "";
- String searchTerm = kenteken;
- boolean found = false;
- double kilometers = 0;
- try
- {
- x = new Scanner(new File(filepath));
- x.useDelimiter("[;\n]");
- while(x.hasNext() ||
- x.hasNextDouble())
- {
- finalKenteken = x.next();
- date = x.next();
- begin = x.next();
- ending = x.next();
- junk3 = x.next();
- junk4 = x.next();
- junk5 = x.next();
- junk6 = x.next();
- totalKM = x.nextDouble();
- junk7 = x.next();
- junk8 = x.next();
- junk9 = x.next();
- junk10 = x.next();
- junk11 = x.next();
- junk12 = x.next();
- kentekensList.add(finalKenteken);
- kilometersList.add(kilometers);
- }
- }
- catch(Exception e)
- {
- System.out.println("error");
- }
- TextOutput();
- }
- public static void TextOutput() {
- File newFile = new File("C:/werkruimte/kmuren/output/output.txt");
- //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! planner/sources/output/output");
- }
- }
- 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 (int i = 0; i < kentekensList.size(); i++)
- {
- buffW.write(kentekensList + ", " + kilometersList);
- 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