2

I have to create a toggleComplete() method that adds a checkbox next to each arrayList object and prompt user to enter the index of the arrayList object they want to delete.

  • If it is completed, the program will print all the arrayList objects with an "x" mark in each of the boxes. Like so [x]
  • If it is not completed, you want to able to toggle it so that you can unmark each of the boxes; therefore leaving a blank checkbox again, like so [ ]

For example:

Add item: Run
Set due date: 11/27/2015
Enter priority: High

Print All items:
0. [ ] Run -1- (11/27/1993)

Add item: Jump
Set due date: 11/28/1993
Enter priority: Low

Add item: Walk
Set due date: 11/19/1993
Enter priority: Medium 

Print All items:
0. [ ] Run -1- (11/27/1993)
1. [ ] Walk -2- (11/19/1993)
2. [ ] Jump -3- (11/27/1993)

Toggle Complete: 1

Print All items:
0. [ ] Run -1- (11/27/1993)
1. [x] Walk -2- (11/19/1993)
2. [ ] Jump -3- (11/27/1993)

Toggle Complete: 1

Print All items:
0. [ ] Run -1- (11/27/1993)
1. [ ] Walk -2- (11/19/1993)
2. [ ] Jump -3- (11/27/1993)

UPDATE //Sharing full code

ToDoItem class:

import java.text.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class ToDoItem {

   private String description;
   private static Date dueDate;
   private Priority priority;

   private static DateFormat df = new SimpleDateFormat("MM/dd/yyyy");

   public ToDoItem() {
   }
   public ToDoItem(String desc) {
      description = desc;
      dueDate = null;
      priority = priority.HIGH;
   }
   public ToDoItem(String desccription, String d) throws ParseException{
      this.description = description;
      dueDate = df.parse(d);
   }
   public ToDoItem(String description, String p, String d) throws ParseException{
      this.description = description;
      this.priority = Priority.valueOf(p.toUpperCase());
      dueDate = df.parse(d);
   }   
   public String toString() {
      return description + " -"+priority.getValue()+"- (" + df.format(dueDate) + ")";
   }

   public static void setDueDate(String s) {
      try {
         dueDate = df.parse(s);
      } catch(Exception ex) {
         System.out.println(ex);
      }      
   }
   public String getDescription() {
      return description;
   }     
   public String getDueDate() {
      return df.format(dueDate);
   }   
   public Priority getPriority() {
      return priority;
   }
}
enum Priority {
      HIGH(1), MEDIUM(2), LOW(3);

      private int value;
      Priority(int value) {
         this.value = value;
      }
      public int getValue() {
         return value;
      }      
   }

MyList class:

import java.util.Scanner;
import java.util.ArrayList;
import java.util.*;
import java.text.*;
import java.util.Collections;

public class MyList {

   public static ArrayList<ToDoItem> toDoItems = new ArrayList<>();
   private static Scanner k = new Scanner(System.in);

   public static void main(String[] args) throws ParseException {

      while(true) {
         printMenu();
         processInput();
      } 
   }

   public static void printMenu() {
      System.out.println("[a]dd an item"); 
      System.out.println("[d]elete an item");
      System.out.println("[t]oggle complete");  
      System.out.println("[p]rint all");  
      System.out.println("[q]uit"); 
   }

   private static void processInput() throws ParseException {
      Scanner s = new Scanner(System.in);
      String input = s.next();

      if(input.equals("a")) {
         addToDoItem();
      }   
      else if(input.equals("d")) {
         deleteToDoItem();
      }
      else if(input.equals("t")) {
         toggleComplete();
      }      
      else if(input.equals("p")) {
         printAll();
      }
      else if(input.equals("q")) {
         System.exit(0);
      }      
   }

   private static void addToDoItem() throws ParseException {

      System.out.print("Enter an item to add to list: ");
      String desc = k.nextLine();

      System.out.print("Enter Date (MM/dd/YYYY): ");
      String dueDate = k.nextLine();
      ToDoItem.setDueDate(dueDate);

      System.out.print("Enter priority (Low/Medium/High): ");
      String prior = k.nextLine();

      toDoItems.add(new ToDoItem(desc, prior, dueDate));
   }

   public static void printAll() {  
      Collections.sort(toDoItems, new Comparator<ToDoItem>() {
         @Override
         public int compare(ToDoItem o1, ToDoItem o2) {
            return o1.getPriority().getValue() - o2.getPriority().getValue();
         }
      });      
      for (int index = 0; index < toDoItems.size(); index++)
         System.out.println(index + ". [ ] " + toDoItems.get(index));
      }  

   public static void deleteToDoItem() {
      int index = 0;
      System.out.print("Enter index of item to delete: ");
      int delete = k.nextInt();
      toDoItems.remove(index);  
   } 

   public static void toggleComplete() {

   }  
}
pyuntae
  • 742
  • 3
  • 10
  • 25
  • And what exactly is your question? Most of the things you need are already there, the only concept that seems to be missing: if you want that you no deletion takes places until a later "confirmation" (that is what unchecking a crossed checkbox translates to) ... then you might prefer to not immediately delete an item from the list. Instead consider to somehow "remember" the deletion selections the user has made. – GhostCat Oct 17 '15 at 10:14
  • I do not know how to start the `public static void toggleComplete() { }` method and how to add the checkboxes so that it can toggle the x mark for complete or incomplete.... – pyuntae Oct 17 '15 at 10:23
  • You can't toggle text that was printed via println! If you are really asking about building some kind of "form" where you can make such changes, you will either have to create a graphical application, for example using Swing or JavaFx; or you need a curses like library that allows for screen manipulation, see http://stackoverflow.com/questions/439799/whats-a-good-java-curses-like-library-for-terminal-applications for examples. – GhostCat Oct 17 '15 at 11:09
  • No, i cannot create a "form" because i have not learned those yet. I need to do this old fashioned way thru simple compilers. I have to make it look like this [DEMO VIDEO](https://www.youtube.com/watch?v=9eWkn7uOLs0&feature=youtu.be) – pyuntae Oct 19 '15 at 08:48
  • So, for the "checkboxes", there video is so clear. You do not "toggle" them "graphically". You turn to the "menu"; select "toggle"; and select the number of an existing item. Then, when you print again, the box should now look differently. – GhostCat Oct 19 '15 at 10:01

1 Answers1

1

Lets try to give an answer that helps you, but that doesn't take away the things you have to do: obviously you are dealing with a list of objects that represent the "todo items".

But now ask your self: "what other information is required". It looks like each item can be "completed", or "not completed". So, maybe you need something to remember this for each of the work items. And when you "toggle" the state of any work item, that means updating that data structure.

And that data structure also determines if you print

[ ] whatever

or

[X] whatever

Please note: the assignment does say that the only way to "toggle" is via menu; it is not like you can click on an X to make it disappear or so.

You print out the data; then you may change it; then you print out again; resulting in a different output. That is the whole secret.

GhostCat
  • 137,827
  • 25
  • 176
  • 248