Ravi Srinivasan
2018-09-06 1b26cc8d09cd5da97f80100ca2435ce296838685
Code for GE in Chap 1
7 files added
268 ■■■■■ changed files
labs/todojse/.gitignore 4 ●●●● patch | view | raw | blame | history
labs/todojse/pom.xml 63 ●●●●● patch | view | raw | blame | history
labs/todojse/src/main/java/com/redhat/training/Status.java 5 ●●●●● patch | view | raw | blame | history
labs/todojse/src/main/java/com/redhat/training/TestTodoMap.java 73 ●●●●● patch | view | raw | blame | history
labs/todojse/src/main/java/com/redhat/training/TodoItem.java 35 ●●●●● patch | view | raw | blame | history
labs/todojse/src/main/java/com/redhat/training/TodoMap.java 88 ●●●●● patch | view | raw | blame | history
solutions/.gitkeep patch | view | raw | blame | history
labs/todojse/.gitignore
New file
@@ -0,0 +1,4 @@
.project
.classpath
.settings
target
labs/todojse/pom.xml
New file
@@ -0,0 +1,63 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
        <groupId>com.redhat.training</groupId>
        <artifactId>todojse</artifactId>
        <version>1.0</version>
        <packaging>jar</packaging>
        <properties>
        <!-- Explicitly declaring the source encoding eliminates the following
            message: -->
        <!-- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered
            resources, i.e. build is platform dependent! -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- maven-compiler-plugin -->
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.5.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>com.redhat.training.TestTodoMap</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <id>package-jar-with-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <appendAssemblyId>false</appendAssemblyId>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                            <archive>
                                <manifest>
                                    <mainClass>com.redhat.training.TestTodoMap</mainClass>
                                </manifest>
                            </archive>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
labs/todojse/src/main/java/com/redhat/training/Status.java
New file
@@ -0,0 +1,5 @@
package com.redhat.training;
public enum Status {
    PENDING, COMPLETED
}
labs/todojse/src/main/java/com/redhat/training/TestTodoMap.java
New file
@@ -0,0 +1,73 @@
package com.redhat.training;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class TestTodoMap {
    public static void main(String[] args) {
        TodoMap testMap=new TodoMap();
        boolean timeToQuit = false;
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        do {
            try {
                timeToQuit = executeMenu(in, testMap);
            } catch (IOException e) {
                System.out.println("Could not read the input");
            }
        } while (!timeToQuit);
    }
    public static boolean executeMenu(BufferedReader in, TodoMap todo) throws IOException {
        String action;
        int id;
        System.out.println("\n\n[N]ew | [C]omplete | [R]ead | [D]elete | [L]ist | [Q]uit: ");
        action = in.readLine();
        if ((action.length() == 0) || action.toUpperCase().charAt(0) == 'Q') {
            return true;
        }
        switch (action.toUpperCase().charAt(0)) {
        // Create a new todo record
        case 'N':
            todo.addTodo();
            System.out.println("Successfully added new todo item: ");
            break;
        // Display an todo record
        case 'R':
            System.out.println("Enter int value for item id: ");
            id = new Integer(in.readLine().trim());
            todo.findItemTodo(id);
            break;
        // Mark an existing task as completed
        case 'C':
            System.out.println("Enter int value for item id: ");
            id = new Integer(in.readLine().trim());
            todo.completeTodo(id);
            break;
        // Delete an todo record
        case 'D':
            System.out.println("Enter int value for item id: ");
            id = new Integer(in.readLine().trim());
            todo.deleteTodo(id);
            System.out.println("Deleted item " + id);
            break;
        // Display a list (Read the records) of todo
        case 'L':
            todo.printTodo();
            break;
        }
        return false;
    }
    }
labs/todojse/src/main/java/com/redhat/training/TodoItem.java
New file
@@ -0,0 +1,35 @@
package com.redhat.training;
public class TodoItem {
    private String item;
    private String status;
    public TodoItem(String item,String status)
    {
        this.item=item;
        this.status=status;
    }
    public TodoItem()
    {
    }
    public String getItem() {
        return item;
    }
    public void setItem(String item) {
        this.item = item;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
}
labs/todojse/src/main/java/com/redhat/training/TodoMap.java
New file
@@ -0,0 +1,88 @@
package com.redhat.training;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class TodoMap {
    Map<Integer, TodoItem> todoMap;
    static Integer count = 0;
    TodoItem item;
    Scanner scn;
    public TodoMap() {
        this.todoMap = new HashMap<Integer,TodoItem>();
    }
    public void addTodo() {
        String ans = "Y";
        do {
            item = new TodoItem();
            count++;
            scn = new Scanner(System.in);
            System.out.println("Enter item description:");
            item.setItem(scn.nextLine());
            System.out.println("Is todo item completed? [Y/N]:");
            String state = scn.nextLine();
            String status = "";
            if (state.toUpperCase().charAt(0) == 'Y')
                status = Status.COMPLETED.toString();
            else
                status = Status.PENDING.toString();
            item.setStatus(status);
            todoMap.put(new Integer(count), item);
            System.out.println("Do you want to add more items[Y/N]");
            ans = scn.next().toUpperCase();
        } while(ans.equalsIgnoreCase("Y"));
    }
    public void printTodo() {
        System.out.println("---- There are " + todoMap.size() + " items in the list ----");
        for (Integer key : todoMap.keySet()) {
            System.out
                    .println(key + " - " + todoMap.get(key).getItem() + " - " + todoMap.get(key).getStatus());
        }
    }
    public void completeTodo(int key) {
        if (todoMap.get(key).getStatus().equals(Status.COMPLETED.toString()))
        {
            System.out.println("This item is already COMPLETED.\n");
        }
        else
        {
            item = new TodoItem();
            String i = todoMap.get(key).getItem();
            item.setItem(i);
            item.setStatus(Status.COMPLETED.toString());
            todoMap.replace(key, item);
            System.out.println("Marked item #" +key+ " as COMPLETE.\n");
            printTodo();
        }
    }
    public void deleteTodo(int id) {
        System.out.println("Enter id of item to be deleted");
        scn = new Scanner(System.in);
        todoMap.remove(id);
        System.out.println("Item removed\n");
    }
    public void findItemTodo(int id) {
        System.out.println("\n");
        System.out.println(id + " - " + todoMap.get(id).getItem() + " - " + todoMap.get(id).getStatus());
    }
}
solutions/.gitkeep