diff --git a/src/tse/lr4/NotePad.java b/src/tse/lr4/NotePad.java index fecf119..346e8aa 100644 --- a/src/tse/lr4/NotePad.java +++ b/src/tse/lr4/NotePad.java @@ -1,5 +1,6 @@ package tse.lr4; +import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; @@ -54,6 +55,18 @@ public class NotePad { this.important = important; } + public static NotePad readFromCsvLine(String line) throws ParseException { + if (line.isEmpty()) throw new RuntimeException("Пустая строка"); + String[] params = line.split(","); + if (params.length != 4) throw new RuntimeException("Неверное количество параметров"); + + String name = params[0]; + String description = params[1]; + Date data = new SimpleDateFormat(DATE_PATTERN).parse(params[2]); + boolean important = Boolean.parseBoolean(params[3]); + + return new NotePad(name, description, data, important); + } public String storeToCsvLine() { final char SEPARATOR = ','; diff --git a/src/tse/lr4/NotePadManager.java b/src/tse/lr4/NotePadManager.java new file mode 100644 index 0000000..84a66db --- /dev/null +++ b/src/tse/lr4/NotePadManager.java @@ -0,0 +1,107 @@ +package tse.lr4; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +/** + * Менеджер записей ежедневника. + * @author aNNiMON + */ +public class NotePadManager { + + private static final String FILENAME = "dailypad.csv"; + + private static NotePadManager instance; + + public static NotePadManager getInstance() { + if (instance == null) { + instance = new NotePadManager(); + } + return instance; + } + + private ArrayList notepads; + + public NotePadManager() { + notepads = (ArrayList) readFromCSV(FILENAME); + if (notepads == null) notepads = new ArrayList<>(); + } + + public ArrayList getNotepads() { + return notepads; + } + + public boolean isNotePadExists(String name) { + for (NotePad pad : notepads) { + if (name.equals(pad.getName())) return true; + } + return false; + } + + public void createNewEntry(String name, String desc, Date date, boolean important) { + notepads.add(new NotePad(name, desc, date, important)); + saveToCSV(FILENAME, notepads); + } + + public void updateEntry(String name, String desc, Date date, boolean important) { + for (NotePad pad : notepads) { + if (name.equals(pad.getName())) { + pad.setDescription(desc); + pad.setDate(date); + pad.setImportant(important); + } + } + saveToCSV(FILENAME, notepads); + } + + private void saveToCSV(String filename, ArrayList list) { + try { + BufferedWriter writer = new BufferedWriter( + new OutputStreamWriter(new FileOutputStream(filename), "UTF-8") + ); + writer.write("Имя,Описание,Дата,Важное"); + for (NotePad pad : list) { + writer.newLine(); + writer.write(pad.storeToCsvLine()); + } + writer.flush(); + writer.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } + + private Object readFromCSV(String filename) { + try { + List list = new ArrayList<>(); + BufferedReader reader = new BufferedReader( + new InputStreamReader( new FileInputStream(filename), "UTF-8" ) + ); + reader.readLine(); // Имя,Описание,Дата,Важное + String line; + while ( (line = reader.readLine()) != null ) { + NotePad pad = null; + try { + pad = NotePad.readFromCsvLine(line); + } catch (RuntimeException | ParseException ex) { + System.out.println(ex.toString()); + } + if (pad != null) list.add(pad); + } + reader.close(); + return list; + } catch (IOException ex) { + ex.printStackTrace(); + } + return null; + } +}