-
-
Save warriordog/74561c1f1f0f4563fcc9 to your computer and use it in GitHub Desktop.
Todo list class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Todo { | |
private final Map<String, Set<TodoItem>> tasks; | |
private final Map<String, TodoItem> allTasks; | |
public Todo() { | |
tasks = new HashMap<>(); | |
allTasks = new HashMap<>(); | |
} | |
public Todo(File f) { | |
this(); | |
load(f); | |
} | |
public void load(File f) { | |
Scanner scanner = null; | |
try { | |
scanner = new Scanner(f); | |
Set<TodoItem> cat = null; | |
while (scanner.hasNext()) { | |
String line = scanner.nextLine(); | |
if (line.beginsWith(':')) { | |
cat = getCategory(line.substring(1)); | |
} else { | |
cat.add(new TodoItem(line)); | |
} | |
} | |
} catch (IOException e) { | |
if (scanner != null) { | |
try { | |
scanner.close(); | |
} catch (IOException ignored) {} | |
} | |
throw new RuntimeException("Exception loading state!", e); | |
} | |
} | |
public void save(File f) { | |
Writer writer = null; | |
try { | |
writer = new FileWriter(f); | |
for (String catName : tasks.keySet()) { | |
writer.write(":" + catName + "\n"); | |
Set<TodoItem> cat = tasks.get(catName); | |
for (TodoItem task : cat) { | |
writer.write(task.item.trim() + "\n"); | |
} | |
} | |
writer.close(); | |
} catch (IOException e) { | |
if (writer != null) { | |
try { | |
writer.close(); | |
} catch (IOException ignored) {} | |
} | |
throw new RuntimeException("Exception saving list!", e); | |
} | |
} | |
private Set<TodoItem> getCategory(String name) { | |
Set<TodoItem> cat = tasks.get(name); | |
if (cat == null) { | |
tasks.put(name, cat = new HashSet<>()); | |
} | |
return cat; | |
} | |
public void addItem(String task, File f, String ... categories) { | |
addItem(task, categories); | |
save(f); | |
} | |
public void addItem(String task, String ... categories) { | |
TodoItem todo = new TodoItem(task); | |
allTasks.put(task, todo); | |
for (String cat : categories) { | |
getCategory(cat).add(todo); | |
} | |
} | |
public void deleteItem(String task, File f, String ... categories) { | |
deleteItem(task, categories); | |
save(f); | |
} | |
public void deleteItem(String task, String ... categories) { | |
TodoItem todo = new TodoItem(task); | |
allTasks.remove(todo); | |
for (String cat : categories) { | |
getCategory(cat).remove(todo); | |
} | |
} | |
public void updateItem(String task, String newTask) { | |
TodoItem todo = allTasks.get(task); | |
if (todo != null) { | |
todo.item = newTask; | |
} | |
} | |
public void viewList() { | |
for (String catName : tasks.keySet()) { | |
viewCategory(catName); | |
} | |
} | |
public void viewCategory(String catName) { | |
System.out.println("Tasks in category \"" + catName + "\":\n"); | |
Set<TodoItem> cat = tasks.get(catName); | |
for (TodoItem task : cat) { | |
System.out.println(" " + task.item + "\n"); | |
} | |
} | |
public static void main(String[] args) { | |
Todo todo = new Todo(); | |
todo.addItem("Work on easy DP challenge", "programming"); | |
todo.addItem("Work on intermediate DP challenge", "programming"); | |
todo.save("./tasks.lst"); | |
todo = new Todo("./tasks.lst"); | |
todo.viewList(); | |
} | |
private static class TodoItem { | |
private String item; | |
public TodoItem(item) { | |
this.item = item; | |
} | |
public int hashCode() { | |
return item.hashCode(); | |
} | |
public boolean equals(Object other) { | |
if (other == this) return true; | |
if (!(other instanceof TodoItem)) return false; | |
return item.equals(((TodoItem)other).item); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment