From 1b26cc8d09cd5da97f80100ca2435ce296838685 Mon Sep 17 00:00:00 2001 From: Ravi Srinivasan <rsriniva@redhat.com> Date: Thu, 06 Sep 2018 13:56:51 +0200 Subject: [PATCH] Code for GE in Chap 1 --- labs/todojse/src/main/java/com/redhat/training/TodoMap.java | 88 +++++++++++++++++ labs/todojse/.gitignore | 4 labs/todojse/pom.xml | 63 ++++++++++++ labs/todojse/src/main/java/com/redhat/training/TestTodoMap.java | 73 ++++++++++++++ labs/todojse/src/main/java/com/redhat/training/TodoItem.java | 35 +++++++ labs/todojse/src/main/java/com/redhat/training/Status.java | 5 + solutions/.gitkeep | 0 7 files changed, 268 insertions(+), 0 deletions(-) diff --git a/labs/todojse/.gitignore b/labs/todojse/.gitignore new file mode 100644 index 0000000..4d7b35b --- /dev/null +++ b/labs/todojse/.gitignore @@ -0,0 +1,4 @@ +.project +.classpath +.settings +target diff --git a/labs/todojse/pom.xml b/labs/todojse/pom.xml new file mode 100644 index 0000000..0268117 --- /dev/null +++ b/labs/todojse/pom.xml @@ -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> diff --git a/labs/todojse/src/main/java/com/redhat/training/Status.java b/labs/todojse/src/main/java/com/redhat/training/Status.java new file mode 100644 index 0000000..f78de89 --- /dev/null +++ b/labs/todojse/src/main/java/com/redhat/training/Status.java @@ -0,0 +1,5 @@ +package com.redhat.training; + +public enum Status { + PENDING, COMPLETED +} diff --git a/labs/todojse/src/main/java/com/redhat/training/TestTodoMap.java b/labs/todojse/src/main/java/com/redhat/training/TestTodoMap.java new file mode 100644 index 0000000..f96ab4a --- /dev/null +++ b/labs/todojse/src/main/java/com/redhat/training/TestTodoMap.java @@ -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; + } + + } diff --git a/labs/todojse/src/main/java/com/redhat/training/TodoItem.java b/labs/todojse/src/main/java/com/redhat/training/TodoItem.java new file mode 100644 index 0000000..24b6cf5 --- /dev/null +++ b/labs/todojse/src/main/java/com/redhat/training/TodoItem.java @@ -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; + } + + +} diff --git a/labs/todojse/src/main/java/com/redhat/training/TodoMap.java b/labs/todojse/src/main/java/com/redhat/training/TodoMap.java new file mode 100644 index 0000000..9aa2eda --- /dev/null +++ b/labs/todojse/src/main/java/com/redhat/training/TodoMap.java @@ -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()); + + } + +} diff --git a/solutions/.gitkeep b/solutions/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/solutions/.gitkeep -- Gitblit v1.9.3