-2

Here are my instructions:

This assignment is already past due and my professor said he would comment on it to help me understand but I think he is too busy & it's irking my soul that I can't get it right so.... here i am... Any help would be wonderful.

ToDo List

  • You will be handing in 2 classes. ToDoItem.java (defines the data type of a To Do Item) and MyList.java (the driver that allows the user to enter data, create ToDoItems, and manage ToDoItems).
  • Design a class for a ToDoItem. A ToDoItem keeps track of an item you need to do (as a string), the date it is due (hmmm, what is a good way to do this?), the priority of that item (1 is high, 2 is medium, 3 is low), and if that item is or is not completed.
  • Sketch-out a rough UML diagram of what this class would look like and what methods you would need for this class. You must include this with your assignment. You may use a CASE tool like ArgoUML or you can simply hand draw the diagram and submit a picture with your assignment.
  • You must provide a constructor that accepts arguments for this class to set the object's default state.
  • Write an overloaded method so that a user can set priority by int (1, 2, 3) or String ("high", "medium", "low").
  • Then, write a simple menu-driven program that allows a user to create ToDo Items, delete them, view them all unsorted, and mark them as completed. Please see the attached file MyList.java for a small framework to help make your menu system more organized.

ToDo Notes & Hints

The attached file MyList.java is a basic framework for a simple command-line driven ToDo list. Use this as the basis for your ToDo assignment. There is a lot missing from this starter file but it will help you get up and running quicker.

import java.util.ArrayList;
import java.util.Scanner;

public class MyList {

   public static ArrayList todoItems = new ArrayList();

   public static void main(String[] args) {

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

   }

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

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

      if(input.equals("a")) {
         //addToDoItem();
      }
      else if(input.equals("p")) {
         //printAll();
      }
      else if(input.equals("q")) {
         System.exit(0);
      }
   }

   // implement rest of processInput methods here

}
  • Each feature such as create, view, delete, and mark read should be defined as a method so that your code can be easily read.
  • Your ToDoItem class should have NO code related to user interface in it. Keep your user interface code separate from your data (this advice is based on a pattern called Model-View-Controller or MVC). This means your ToDoItem class might be pretty basic and that your "driver" file with your main method is doing most of the work.
  • Use an Array or ArrayList to store your ToDoItems. Reference individual ToDoItems by their index in that data structure (print the index of each item when printing all ToDoItems).
  • Once a ToDoItem is created it can NOT be edited beyond being marked as completed. If the user mistypes a date or incorrectly spells the title that item can only be deleted and then recreated to be fixed. This is to simplify the assignment. Again, marking an item as complete/incomplete is the only aspect of a ToDoItem that can be edited. Objects that cannot be edited are called immutable.

ToDo Program Thoughts

Here are some things to think about that we will address in a few weeks in this course. I included these items now so you can start to think about them.

  • What if we wanted to sort our items by date due or by priority or by both? - -Don't write this, just think about how we might do this.

  • What makes one ToDo -item less than, equal to, or greater than another?

  • We are writing a lot of code to manage our array of ToDoItems. What, if anyways, might we simplify this?

I have a video of a demo of the program: https://youtu.be/9eWkn7uOLs0

Here are the codes I have written so far. I am stuck and having trouble parsing my date and getting it to print out.

MyList.java

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

public class MyList {

   public static ArrayList<ToDoItem> toDoItems = new ArrayList<>();
   public static Scanner k = new Scanner(System.in);
   private static String description;
   private static String dueDate;
   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"); 
   }

   public 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);
      }      
   }

   public static ToDoItem addToDoItem() throws ParseException {
      ToDoItem newItem;
      newItem = null;

      System.out.print("Enter an item to add to list: ");
      String desc = k.nextLine();
      if(desc.trim().length()==0) return newItem;
      System.out.print("Enter Date (MM/dd/YYYY): ");
      String dueDate = k.nextLine();

      System.out.print("Enter priority between 1 and 3 (3 being the highest): ");
      String prior = k.nextLine();
      int p = Integer.parseInt(prior);

      if(dueDate.trim().length()==0){
         newItem = new ToDoItem(desc);
      }
      else { 
         newItem = new ToDoItem(desc, dueDate);
      }
      newItem.setPriority(p);

      return newItem;         

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

   public static void printAll() throws ParseException {  
      ToDoItem item = new ToDoItem();
      System.out.println(item);

      //ToDoItem task = newItem.get(i);         
      // ***************
      // You should not need to create ToDoItems here
      // This method should loop through your array and print out each item
      // Since you have a toString() method you can print the objects by passing
      //   them into like so inside of a loop System.out.println( item.get(i) ) 
      //for(int i = 0; i < newItem.size(); i++) {
       //  System.out.println(toDoItems.get(i));
     // }
        // for(ToDoItem myItem : toDoItems) {
         //ToDoItem myItem = toDoItems.get(i);
         //System.out.println(myItem);
        // System.out.println(myItem.getDescription()+" -"+myItem.getPriority()+"- ("+myItem.getDueDate()+")");
      }
   }   

   //public static void deleteToDoItem() {

   // **********
   // You won't need a loop here, you can directly 
   //  delete the item at the given index.
   // Prompt for an int, read in the int, then call item.remove(i);

         //System.out.print("Enter index of item to delete: ");
         //int delete = k.nextInt();
         //toDoItems.remove(i);  
  // } 

  // public static void toggleComplete() {
      ///// 
  // }  
//}

ToDoItem.java

import java.text.*;
import java.util.Date;
//import java.lang.NullPointerException;

public class ToDoItem {

   private String description;
   private Date dueDate;
   private int priority;
   DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);   

   public ToDoItem() {
   }
   public ToDoItem(String desc) {
      description = desc;
      dueDate = null;
      priority = 0;
   }
   public ToDoItem(String desccription, String dDate) throws ParseException {
      this.description = description;
      dueDate = df.parse(dDate.trim());
   }
   public String toString() {
      if(dueDate != null) {
         return( description + " -"+priority+"- " + "Date not Set");
      }
      else {
         return( description + " -"+priority+"- " + df.format(dueDate));
      }
   }
   public void setPriority( int prio) {
      if(prio<0) this.priority = 0;
      else if(prio > 3) this.priority = 3;
      else this.priority = prio; 
   }
   public int getPriority() {
      return this.priority;
   } 
   public void setDueDate(String date) throws ParseException {
      Date d = df.parse(date.trim());
      this.dueDate = d;
   }
   public String getDescription() {
      return description;
   }     
   public String getDueDate() {
      if(dueDate == null) return "";
      return df.format(dueDate);
   }            
}
pyuntae
  • 742
  • 3
  • 10
  • 25
  • 2
    Is this assignment for you or for us? Please provide what it required and state your problem precisely? Have a look at this [MCVE](http://stackoverflow.com/help/mcve). – YoungHobbit Oct 16 '15 at 07:08
  • I provided everything that I have been provided with... this is all the info i got from my professor. And below are the codes that I have written out of the information he provided. And I have stated that I can't parse the date correctly. Guess i didn't make it clear enough. now i have bolded it so people can see it now. sorry. – pyuntae Oct 16 '15 at 07:14

2 Answers2

0

If you have a problem with parsing and printing out your Date-type maybe this could help you:

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    String dateString = sdf.format(new Date());
    Date date = null;
    try {
        date = sdf.parse("10/16/2015");
    } catch (ParseException objcPException) {
        // TODO Auto-generated catch block
        objcPException.printStackTrace();
    }
    System.out.println(date);
    System.out.println(dateString);

You have to change this method:

 public ToDoItem(String desccription, String dDate) throws ParseException {
  this.description = description;
  dueDate = df.parse(dDate.trim());

} to this:

 public ToDoItem(String desccription, String dDate) throws ParseException {
  this.description = description;
  dueDate = sdf.parse(dDate);

} and switch your DateFormat to SimpleDateFormat like this in my example. Maybe you have to validate the form of the input first (MM/dd/yyyy) before parsing.

Winusch
  • 38
  • 4
  • i have tried that method but it always tells me that Date cannot be converted to String when I compile it. – pyuntae Oct 16 '15 at 07:28
  • I've tried it in my project and i didn't get any errors. Did you copy the whole example and run it? – Winusch Oct 16 '15 at 07:38
  • Yes the problem is if i run it just like how you wrote it, then it is correct. But for this i have to implement it under a method so that it can parse an input that is given from the prompt. I can't hard code it. Which is what gets me stuck because everything i google only gives me a hard code for it and not how to implement it in my two classes to use. – pyuntae Oct 16 '15 at 07:46
  • Got it! Thanks so much. – pyuntae Oct 17 '15 at 05:24
0

Replace the following code

DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);

with statement below and try

SimpleDateFormat df = new SimpleDateFormat("MM/dd/YYYY");

So, the constructor will use this dateformat to parse the input.

dueDate = df.parse(dDate.trim());

When user presents date in a format (MM/dd/YYYY) and is read as a string, best way is to parse it using dateformat. Check the following similar example from (stackoverflow). You can run it and check.

String string = "January 2, 2010";
DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date);

Hope it helps.

Community
  • 1
  • 1
a3.14_Infinity
  • 5,653
  • 7
  • 42
  • 66