Ravi Srinivasan
2018-09-06 1b26cc8d09cd5da97f80100ca2435ce296838685
commit | author | age
1b26cc 1 package com.redhat.training;
RS 2
3 import java.util.HashMap;
4 import java.util.Map;
5 import java.util.Scanner;
6
7 public class TodoMap {
8
9     Map<Integer, TodoItem> todoMap;
10     static Integer count = 0;
11     TodoItem item;
12     Scanner scn;
13
14     public TodoMap() {
15         this.todoMap = new HashMap<Integer,TodoItem>();
16     }
17
18     public void addTodo() {
19         String ans = "Y";
20         do {
21
22             item = new TodoItem();
23             count++;
24             scn = new Scanner(System.in);
25             System.out.println("Enter item description:");
26             item.setItem(scn.nextLine());
27             System.out.println("Is todo item completed? [Y/N]:");
28             String state = scn.nextLine();
29             String status = "";
30
31             if (state.toUpperCase().charAt(0) == 'Y')
32                 status = Status.COMPLETED.toString();
33             else
34                 status = Status.PENDING.toString();
35
36             item.setStatus(status);
37             todoMap.put(new Integer(count), item);
38             System.out.println("Do you want to add more items[Y/N]");
39             ans = scn.next().toUpperCase();
40
41         } while(ans.equalsIgnoreCase("Y"));
42
43     }
44
45     public void printTodo() {
46         System.out.println("---- There are " + todoMap.size() + " items in the list ----");
47         for (Integer key : todoMap.keySet()) {
48             System.out
49                     .println(key + " - " + todoMap.get(key).getItem() + " - " + todoMap.get(key).getStatus());
50         }
51
52     }
53
54     public void completeTodo(int key) {
55
56         if (todoMap.get(key).getStatus().equals(Status.COMPLETED.toString()))
57         {
58             System.out.println("This item is already COMPLETED.\n");
59         }
60         else
61         {
62             item = new TodoItem();
63             String i = todoMap.get(key).getItem();
64             item.setItem(i);
65             item.setStatus(Status.COMPLETED.toString());
66             todoMap.replace(key, item);
67             System.out.println("Marked item #" +key+ " as COMPLETE.\n");
68             printTodo();
69         }
70     }
71
72
73     public void deleteTodo(int id) {
74
75         System.out.println("Enter id of item to be deleted");
76         scn = new Scanner(System.in);
77         todoMap.remove(id);
78         System.out.println("Item removed\n");
79
80     }
81
82     public void findItemTodo(int id) {
83         System.out.println("\n");
84         System.out.println(id + " - " + todoMap.get(id).getItem() + " - " + todoMap.get(id).getStatus());
85
86     }
87
88 }