Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.01 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.InputMismatchException;
  4. import java.util.Scanner;
  5.  
  6. public class Todo
  7. {
  8.     private static final int INT_FLAG = -5;
  9.     private static final String STRING_FLAG = "";
  10.     private static final int PROGRAM_WIDTH = 20;
  11.  
  12.     private static final int ADD_ITEM = 1;
  13.     private static final int CHANGE_ITEM_STATUS = 2;
  14.     private static final int REMOVE_ITEM = 3;
  15.     private static final int EXIT_PROGRAM = 4;
  16.  
  17.     private static final String[] MENU_OPTIONS = {"Add item", "Change item status", "Remove item", "Exit"};
  18.     private static ArrayList<TodoItem> tasks = new ArrayList<TodoItem>();
  19.  
  20.  
  21.     public static void main(String[] args)
  22.     {  
  23.         int userInput = INT_FLAG;
  24.  
  25.         while(userInput != EXIT_PROGRAM)
  26.         {
  27.             displayTasksWithStatus(tasks);
  28.             displayMenu();
  29.  
  30.             userInput = intInput("Enter option's number: ");
  31.             executeChoosenOption(userInput);
  32.         }
  33.     }
  34.  
  35.     private static void displayTasksWithStatus(ArrayList<TodoItem> tasks)
  36.     /**
  37.      * Displays all tasks enumerated, everyone in new line
  38.      */
  39.     {
  40.         displayBar();
  41.         if (tasks.isEmpty())
  42.             System.out.println("Your tasks list is empty!");
  43.  
  44.         else
  45.         {
  46.             System.out.println("TASKS");
  47.             System.out.println();
  48.  
  49.             for ( TodoItem element : tasks)
  50.                 {
  51.                     System.out.print(tasks.indexOf(element) + 1);
  52.                     System.out.print(". ");
  53.                     element.showNameAndStatus();
  54.                 }
  55.         }
  56.         System.out.println();
  57.         displayBar();
  58.     }
  59.  
  60.     private static void displayBar()
  61.     /**
  62.      * Displays horizontal bar
  63.      */
  64.     {
  65.         for (int i = PROGRAM_WIDTH; i>=0; i--)
  66.         {
  67.             System.out.print("_");
  68.         }
  69.         System.out.println();
  70.     }
  71.    
  72.     private static void displayMenu()
  73.     /**
  74.      * Displays possible menu options in program
  75.      */
  76.     {
  77.         ArrayList<String> options = new ArrayList<String>(Arrays.asList(MENU_OPTIONS));
  78.  
  79.         for ( String choice : options)
  80.         {
  81.             System.out.print(options.indexOf(choice) + 1);
  82.             System.out.print(". ");
  83.             System.out.println(choice);
  84.         }
  85.  
  86.         displayBar();
  87.     }
  88.  
  89.     private static void executeChoosenOption(int option)
  90.     /**
  91.      * Changes tasks list state depending on option choosen
  92.      */
  93.     {
  94.        
  95.         if (option == ADD_ITEM)
  96.         {
  97.             String taskName;
  98.             taskName = strInput("Enter task name: ");
  99.             TodoItem task = new TodoItem(taskName);
  100.             tasks.add(task);
  101.         }
  102.         else if (option == REMOVE_ITEM)
  103.         {
  104.             int taskIndex;
  105.             taskIndex = intInput("Enter task's number to remove: ");
  106.             tasks.remove(taskIndex - 1);
  107.         }
  108.         else if (option == CHANGE_ITEM_STATUS)
  109.         {
  110.             int taskIndex;
  111.             TodoItem task;
  112.             taskIndex = intInput("Enter task's number to change status: ");
  113.  
  114.             task = tasks.get(taskIndex - 1);
  115.             task.toggleDone();
  116.         }
  117.     }
  118.  
  119.     private static int intInput(String question)
  120.     /**
  121.      * Asks question to user and returns his answer in int
  122.      */
  123.     {
  124.         Scanner in = new Scanner(System.in);
  125.         int userInput = INT_FLAG;
  126.        
  127.         System.out.print(question);
  128.  
  129.         try
  130.         {
  131.             userInput = in.nextInt();
  132.         }
  133.         catch( InputMismatchException e)
  134.         {
  135.             System.out.println("Enter number!");
  136.         }
  137.  
  138.         return userInput;
  139.     }
  140.  
  141.     private static String strInput(String question)
  142.     /**
  143.      * Asks question to user and return his answer in String
  144.      */
  145.     {
  146.         Scanner in = new Scanner(System.in);
  147.         String userInput = STRING_FLAG;
  148.  
  149.         System.out.println(question);
  150.         userInput = in.next();
  151.  
  152.         return userInput;
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement