Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.InputMismatchException;
- import java.util.Scanner;
- public class Todo
- {
- private static final int INT_FLAG = -5;
- private static final String STRING_FLAG = "";
- private static final int PROGRAM_WIDTH = 20;
- private static final int ADD_ITEM = 1;
- private static final int CHANGE_ITEM_STATUS = 2;
- private static final int REMOVE_ITEM = 3;
- private static final int EXIT_PROGRAM = 4;
- private static final String[] MENU_OPTIONS = {"Add item", "Change item status", "Remove item", "Exit"};
- private static ArrayList<TodoItem> tasks = new ArrayList<TodoItem>();
- public static void main(String[] args)
- {
- int userInput = INT_FLAG;
- while(userInput != EXIT_PROGRAM)
- {
- displayTasksWithStatus(tasks);
- displayMenu();
- userInput = intInput("Enter option's number: ");
- executeChoosenOption(userInput);
- }
- }
- private static void displayTasksWithStatus(ArrayList<TodoItem> tasks)
- /**
- * Displays all tasks enumerated, everyone in new line
- */
- {
- displayBar();
- if (tasks.isEmpty())
- System.out.println("Your tasks list is empty!");
- else
- {
- System.out.println("TASKS");
- System.out.println();
- for ( TodoItem element : tasks)
- {
- System.out.print(tasks.indexOf(element) + 1);
- System.out.print(". ");
- element.showNameAndStatus();
- }
- }
- System.out.println();
- displayBar();
- }
- private static void displayBar()
- /**
- * Displays horizontal bar
- */
- {
- for (int i = PROGRAM_WIDTH; i>=0; i--)
- {
- System.out.print("_");
- }
- System.out.println();
- }
- private static void displayMenu()
- /**
- * Displays possible menu options in program
- */
- {
- ArrayList<String> options = new ArrayList<String>(Arrays.asList(MENU_OPTIONS));
- for ( String choice : options)
- {
- System.out.print(options.indexOf(choice) + 1);
- System.out.print(". ");
- System.out.println(choice);
- }
- displayBar();
- }
- private static void executeChoosenOption(int option)
- /**
- * Changes tasks list state depending on option choosen
- */
- {
- if (option == ADD_ITEM)
- {
- String taskName;
- taskName = strInput("Enter task name: ");
- TodoItem task = new TodoItem(taskName);
- tasks.add(task);
- }
- else if (option == REMOVE_ITEM)
- {
- int taskIndex;
- taskIndex = intInput("Enter task's number to remove: ");
- tasks.remove(taskIndex - 1);
- }
- else if (option == CHANGE_ITEM_STATUS)
- {
- int taskIndex;
- TodoItem task;
- taskIndex = intInput("Enter task's number to change status: ");
- task = tasks.get(taskIndex - 1);
- task.toggleDone();
- }
- }
- private static int intInput(String question)
- /**
- * Asks question to user and returns his answer in int
- */
- {
- Scanner in = new Scanner(System.in);
- int userInput = INT_FLAG;
- System.out.print(question);
- try
- {
- userInput = in.nextInt();
- }
- catch( InputMismatchException e)
- {
- System.out.println("Enter number!");
- }
- return userInput;
- }
- private static String strInput(String question)
- /**
- * Asks question to user and return his answer in String
- */
- {
- Scanner in = new Scanner(System.in);
- String userInput = STRING_FLAG;
- System.out.println(question);
- userInput = in.next();
- return userInput;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement