Throw an exception if an invalid value is passed to

I have a class, right now I'm changing the installer to throw an exception if it includes an invalid value. This requires:

  • A. ( dueDay should be between 1 and 31, and dueMonth should be between 1 and 12.) The exception instance is not processed in setter methods.
  • C. Change the main TodoItem method so that it asks the user for the task, day and month, and saves this information as a new TodoItem .
  • C. Change the constructor to be called your new setter methods. If an exception is selected, it should be handled by the main method. The user should be told that they entered an invalid day or month and asked to indicate the correct one.

My class:

(I already changed the setter to the exception, but that will not work, I think I need to change the main constructor of the functions, but I don’t know how to do this.)

 public class TodoItem { private String task; private int dueMonth; private int dueDay; private boolean isDone; // class variables private static int numItems; private static int numDone; // constructor public TodoItem(String taks,int day,int month) { this.task = task; dueDay = day; dueMonth = month; isDone = false; numItems++; } // second constructor public TodoItem(String task) { this.task = task; isDone = false; numItems++; } public static void WriteToFile(String a){ a="toString.txt"; String task; int dueMonth; int dueDay; boolean isDone; } // toString method public String toString() { return a+task + ", due: " + dueMonth + "/" + dueDay + (isDone?", done":", todo"); } // getters public int getDueDay() { return dueDay; } public int getDueMonth() { return dueMonth; } // setters public void setDueDay(int day) throws Exception { if (day >= 1 && day <=31) { dueDay = day; } else { throw new Exception ("Error: invalid due day"); } } public void setDueMonth(int month) throws Exception{ if (month >= 1 && month <= 12) { dueMonth = month; } else { throw new Exception("Error: invalid due month"); } } // Checks off an item as being done. // If the item was already marked as done, don't increase the counter // (this was not specified in the problem set instructions). public void markAsDone() { if (!isDone) { isDone = true; numDone++; } } // returns the percentage of to-do list items completed public static double percentDone() { return (double) numDone/numItems*100; } /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { // constructor 1 TodoItemDone item1 = new TodoItemDone("Walk dog",12,3); TodoItemDone item2 = new TodoItemDone("Do 326 project",16,3); TodoItemDone item3 = new TodoItemDone("Put away winter boots",21,3); // constructor 2 TodoItemDone item4 = new TodoItemDone("Buy groceries"); TodoItemDone item5 = new TodoItemDone("Clean bathroom"); TodoItemDone item6 = new TodoItemDone("Study for myself"); // toString (and verify constructors) System.out.println("The 6 items are:"); System.out.println(item1); System.out.println(item2); System.out.println(item3); System.out.println(item4); System.out.println(item5); System.out.println(item6); System.out.println(); System.out.println("Setting due dates and months on the last 3:"); // setDueDay item4.setDueDay(1); item5.setDueDay(5); item6.setDueDay(52); // setDueMonth item4.setDueMonth(12); item5.setDueMonth(6); item6.setDueMonth(0); System.out.println("The last 3 items are now:"); System.out.println(item4); System.out.println(item5); System.out.println(item6); // Test percentDone() and markAsDone() System.out.println(); System.out.println("About to complete some items:"); System.out.println("percent done: " + percentDone()); item1.markAsDone(); System.out.println("Item 1 is now: " + item1); System.out.println("percent done: " + percentDone()); item1.markAsDone(); System.out.println("Item 1 is now: " + item1); System.out.println("percent done: " + percentDone()); item2.markAsDone(); System.out.println("Item 2 is now: " + item2); System.out.println("percent done: " + percentDone()); } 
+5
source share
2 answers

however it does not work

You define the TodoItem class, but in main () you create the TodoItemDone. When I change TodoItem to TodoItemDone, I have the results:

 The 6 items are: null, due: 3/12, todo null, due: 3/16, todo null, due: 3/21, todo Buy groceries, due: 0/0, todo Clean bathroom, due: 0/0, todo Study for myself, due: 0/0, todo Setting due dates and months on the last 3: Exception in thread "main" java.lang.Exception: Error: invalid due day at com.github.vedenin.TodoItemDone.setDueDay(TodoItemDone.java:61) at com.github.vedenin.TodoItemDone.main(TodoItemDone.java:120) 

Exclude the throw correctly

+1
source

change this part:

 public TodoItem(String taks,int day,int month) { this.task = task; dueDay = day; dueMonth = month; isDone = false; numItems++; } 

:

 public TodoItem(String task,int day,int month) { this.task = task; dueDay = day; dueMonth = month; isDone = false; numItems++; } 

you do not set this.task to fix the taks input argument. I am changing the name of the argument. perhaps this was not the main problem, but try to change it.

+1
source

Source: https://habr.com/ru/post/1237479/


All Articles