Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package javaFXapplication.dao;
- import java.io.BufferedReader;
- import java.util.List;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.nio.charset.StandardCharsets;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.nio.file.StandardOpenOption;
- import java.util.ArrayList;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author noopi
- */
- public class DAOcarTextFile implements DAOcar {
- private BufferedWriter writer;
- private List<DTOcar> dtoCars;
- private String path;
- public void SetPath(String path) {
- path = "bilar.txt";
- this.path = path;
- }
- @Override
- public void addCar(DTOcar dtoCar) {
- try {
- writer = Files.newBufferedWriter(Paths.get(path), StandardCharsets.UTF_8, StandardOpenOption.APPEND);
- writer.write(dtoCar.regnummer + ";" + dtoCar.marke + ";" + dtoCar.modell + ";" + dtoCar.bildURL + ";" +dtoCar.uthyrd);
- writer.newLine();
- writer.close();
- } catch (IOException ex) {
- System.out.println(DAOcarTextFile.class.getName() + " AddCar(DTOcar) " + ex.getMessage());
- }
- }
- @Override
- public void deleteCar(String regnr) {
- try {
- dtoCars = getCars();
- for (DTOcar dtoCar : dtoCars) {
- if (dtoCar.regnummer.equalsIgnoreCase(regnr)) {
- dtoCars.remove(dtoCar);
- break;
- }
- }
- writer = Files.newBufferedWriter(Paths.get(path), StandardCharsets.UTF_8, StandardOpenOption.TRUNCATE_EXISTING);
- for (DTOcar dtoCar : dtoCars) {
- writer.write(dtoCar.regnummer + ";" + dtoCar.marke + ";" + dtoCar.modell + ";" + dtoCar.bildURL + ";" + dtoCar.uthyrd);
- writer.newLine();
- }
- writer.close();
- } catch (IOException ex) {
- System.out.println(DAOcarTextFile.class.getName() + " deleteCar(DTOcar) " + ex);
- }
- }
- @Override
- public void updateCar(DTOcar dtoCar) {
- try {
- BufferedWriter writer = null;
- List<DTOcar> listan = getCars();
- for (int i = 0; i < listan.size(); i++) {
- if (listan.get(i).regnummer.equalsIgnoreCase(dtoCar.regnummer)) {
- DTOcar tempCar = listan.get(i);
- tempCar.marke = dtoCar.marke;
- tempCar.modell = dtoCar.modell;
- tempCar.bildURL = dtoCar.bildURL;
- tempCar.uthyrd = dtoCar.uthyrd;
- break;
- }
- }
- writer = Files.newBufferedWriter(Paths.get(path), StandardCharsets.UTF_8, StandardOpenOption.TRUNCATE_EXISTING);
- for (DTOcar car : listan) {
- String bildata = car.regnummer + ";" + car.marke + ";" + car.modell + ";" + car.bildURL + ";" + car.uthyrd;
- writer.write(bildata);
- writer.newLine();
- }
- writer.close();
- } catch (IOException ex) {
- System.out.println(DAOcarTextFile.class.getName() + " updateCar(DTOcar) " + ex);
- }
- }
- @Override
- public List<DTOcar> getCars() {
- BufferedReader reader = null;
- dtoCars = new ArrayList<DTOcar>();
- try {
- reader = Files.newBufferedReader(Paths.get(path));
- String line = null;
- while ((line = reader.readLine()) != null) {
- String[] tempBil = line.split(";");
- dtoCars.add(new DTOcar(tempBil[0], tempBil[1], tempBil[2], tempBil[3],Boolean.parseBoolean(tempBil[4]) ));
- }//end while
- reader.close();
- } catch (IOException ex) {
- Logger.getLogger(DAOcarTextFile.class.getName()).log(Level.SEVERE, null, ex);
- } finally {
- try {
- reader.close();
- } catch (IOException ex) {
- Logger.getLogger(DAOcarTextFile.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- return dtoCars;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement