Advertisement
Guest User

DAOcarTextFile

a guest
Oct 20th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.38 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package javaFXapplication.dao;
  7.  
  8. import java.io.BufferedReader;
  9. import java.util.List;
  10. import java.io.BufferedWriter;
  11. import java.io.IOException;
  12. import java.nio.charset.StandardCharsets;
  13. import java.nio.file.Files;
  14. import java.nio.file.Paths;
  15. import java.nio.file.StandardOpenOption;
  16. import java.util.ArrayList;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19.  
  20. /**
  21.  *
  22.  * @author noopi
  23.  */
  24. public class DAOcarTextFile implements DAOcar {
  25.    
  26.     private BufferedWriter writer;
  27.     private List<DTOcar> dtoCars;
  28.     private String path;
  29.  
  30.    
  31.     public void SetPath(String path) {
  32.         path = "bilar.txt";
  33.         this.path = path;
  34.        
  35.     }
  36.    
  37.     @Override
  38.     public void addCar(DTOcar dtoCar) {
  39.         try {
  40.             writer = Files.newBufferedWriter(Paths.get(path), StandardCharsets.UTF_8, StandardOpenOption.APPEND);
  41.             writer.write(dtoCar.regnummer + ";" + dtoCar.marke + ";" + dtoCar.modell + ";" + dtoCar.bildURL + ";" +dtoCar.uthyrd);
  42.             writer.newLine();
  43.             writer.close();
  44.         } catch (IOException ex) {
  45.             System.out.println(DAOcarTextFile.class.getName() + " AddCar(DTOcar) " + ex.getMessage());
  46.         }
  47.     }
  48.  
  49.     @Override
  50.     public void deleteCar(String regnr) {
  51.         try {
  52.             dtoCars = getCars();
  53.             for (DTOcar dtoCar : dtoCars) {
  54.                 if (dtoCar.regnummer.equalsIgnoreCase(regnr)) {
  55.                     dtoCars.remove(dtoCar);
  56.                     break;
  57.                 }
  58.             }
  59.             writer = Files.newBufferedWriter(Paths.get(path), StandardCharsets.UTF_8, StandardOpenOption.TRUNCATE_EXISTING);
  60.             for (DTOcar dtoCar : dtoCars) {
  61.                 writer.write(dtoCar.regnummer + ";" + dtoCar.marke + ";" + dtoCar.modell + ";" + dtoCar.bildURL + ";" + dtoCar.uthyrd);
  62.                 writer.newLine();
  63.             }
  64.  
  65.             writer.close();
  66.  
  67.         } catch (IOException ex) {
  68.             System.out.println(DAOcarTextFile.class.getName() + " deleteCar(DTOcar) " + ex);
  69.         }
  70.     }
  71.  
  72.     @Override
  73.     public void updateCar(DTOcar dtoCar) {
  74.  
  75.         try {
  76.             BufferedWriter writer = null;
  77.  
  78.             List<DTOcar> listan = getCars();
  79.             for (int i = 0; i < listan.size(); i++) {
  80.                 if (listan.get(i).regnummer.equalsIgnoreCase(dtoCar.regnummer)) {
  81.                     DTOcar tempCar = listan.get(i);
  82.                     tempCar.marke = dtoCar.marke;
  83.                     tempCar.modell = dtoCar.modell;
  84.                     tempCar.bildURL = dtoCar.bildURL;
  85.                     tempCar.uthyrd = dtoCar.uthyrd;
  86.                     break;
  87.                 }
  88.             }
  89.  
  90.             writer = Files.newBufferedWriter(Paths.get(path), StandardCharsets.UTF_8, StandardOpenOption.TRUNCATE_EXISTING);
  91.  
  92.             for (DTOcar car : listan) {
  93.                 String bildata = car.regnummer + ";" + car.marke + ";" + car.modell + ";" + car.bildURL + ";" + car.uthyrd;
  94.                 writer.write(bildata);
  95.                 writer.newLine();
  96.             }
  97.             writer.close();
  98.  
  99.         } catch (IOException ex) {
  100.             System.out.println(DAOcarTextFile.class.getName() + " updateCar(DTOcar) " + ex);
  101.         }
  102.        
  103.        
  104.  
  105.     }
  106.        
  107.  
  108.  
  109.     @Override
  110.     public List<DTOcar> getCars() {
  111.         BufferedReader reader = null;
  112.         dtoCars = new ArrayList<DTOcar>();
  113.         try {
  114.            
  115.             reader = Files.newBufferedReader(Paths.get(path));
  116.             String line = null;
  117.             while ((line = reader.readLine()) != null) {
  118.                 String[] tempBil = line.split(";");
  119.                 dtoCars.add(new DTOcar(tempBil[0], tempBil[1], tempBil[2], tempBil[3],Boolean.parseBoolean(tempBil[4]) ));
  120.  
  121.             }//end while
  122.  
  123.             reader.close();
  124.  
  125.         } catch (IOException ex) {
  126.             Logger.getLogger(DAOcarTextFile.class.getName()).log(Level.SEVERE, null, ex);
  127.         } finally {
  128.             try {
  129.                 reader.close();
  130.             } catch (IOException ex) {
  131.                 Logger.getLogger(DAOcarTextFile.class.getName()).log(Level.SEVERE, null, ex);
  132.             }
  133.         }
  134.         return dtoCars;
  135.     }
  136.    
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement