add io.ToDoListFromToJSON,

ToDo-List-Editor now can save and load ToDo-lists from&to JSON
This commit is contained in:
Dejan
2021-08-23 22:09:23 +02:00
parent 4ecddf0b65
commit 14c5a9503d
6 changed files with 98 additions and 21 deletions

View File

@@ -1,7 +1,8 @@
Funktionen:
-ToDo deadline#
-ToDo-Liste nach Datum/Uhrzeit sortieren#
-ToDo-Liste abspeichern
-ToDo-Liste als JSON abspeichern#
-ToDo-Liste von JSON laden#
-ToDo hinzufügen#
-ToDo abhaken#
-ToDo löschen#

8
theList.json Normal file
View File

@@ -0,0 +1,8 @@
[
{
"id": 0,
"content": "asdfgh",
"done": false,
"deadline": "Feb 2, 2022, 12:00:00 AM"
}
]

View File

@@ -26,6 +26,11 @@
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.7</version>
</dependency>
</dependencies>
<build>

View File

@@ -4,6 +4,8 @@ import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import de.dejan.todolist.io.ToDoListFromToJSON;
public class Main{
@@ -12,12 +14,23 @@ public class Main{
public static void main( String[] args ){
//ArrayList<ToDo> tasks_ = ToDoListFromToJSON.loadToDoList("theList.json");
for (int i = 0; i < ToDoListFromToJSON.loadToDoList("theList.json").size(); i++) {
tasks.add(ToDoListFromToJSON.loadToDoList("theList.json").get(i));
}
while (true) {
menu();
}
}
public static void exit(){
ToDoListFromToJSON.saveToDoList(tasks, "theList.json");
System.exit(0);
}
public static void menu(){
System.out.println("\n[1]Neues ToDo hinzufügen");
@@ -36,7 +49,7 @@ public class Main{
case 3: done(); break;
case 4: delTask(); break;
case 5: sortByDeadline(); break;
case 6: System.exit(0);
case 6: exit();
}
}

View File

@@ -4,59 +4,59 @@ import java.util.*;
public class ToDo{
private int theID;
private String theContent;
private boolean theDone;
private int id;
private String content;
private boolean done;
// https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
// https://stackoverflow.com/questions/5927109/sort-objects-in-arraylist-by-date
private Date theDeadline;
private Date deadline;
public ToDo(int id, String content, boolean done, Date deadline){
theID=id;
theContent=content;
theDone=done;
theDeadline=deadline;
this.id=id;
this.content=content;
this.done=done;
this.deadline=deadline;
}
public ToDo(int id, String content, boolean done){
theID=id;
theContent=content;
theDone=done;
this.id=id;
this.content=content;
this.done=done;
}
//get-& set-methods
public void setID(int id){
theID=id;
this.id=id;
}
public int getID(){
return theID;
return id;
}
public void setContent(String content){
theContent=content;
this.content=content;
}
public String getContent(){
return theContent;
return this.content;
}
public void setDone(boolean done){
theDone=done;
this.done=done;
}
public boolean getDone(){
return theDone;
return this.done;
}
public void setDeadline(Date d){
theDeadline=d;
this.deadline=d;
}
public Date getDeadline(){
return theDeadline;
return this.deadline;
}
}

View File

@@ -0,0 +1,50 @@
package de.dejan.todolist.io;
import de.dejan.todolist.*;
//import jdk.internal.org.jline.utils.InputStreamReader;
import java.io.*;
import java.lang.reflect.Type;
import java.util.*;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
public class ToDoListFromToJSON {
public static void saveToDoList(ArrayList<ToDo> tasks, String listname){
Gson gson=new GsonBuilder().setPrettyPrinting().create();
try {
FileWriter fw=new FileWriter(listname);
gson.toJson(tasks, fw);
fw.close();
} catch (JsonIOException e) {
//e.printStackTrace();
} catch (IOException e) {
//e.printStackTrace();
}
}
public static ArrayList<ToDo> loadToDoList(String listname){
ArrayList<ToDo> tasks=new ArrayList<ToDo>();
Gson gson=new Gson();
try {
BufferedReader br =new BufferedReader(new InputStreamReader(new FileInputStream(listname)));
Type list=new TypeToken<ArrayList<ToDo>>(){}.getType();
tasks=gson.fromJson(br, list);
br.close();
} catch (Exception e) {
}
return tasks;
}
}