Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throw an exception if an invalid value is passed in

I have a class, right now I am changing the setter to throw an exception if an invalid value pass in. It requires:

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

My class is:

( I already changed setter to throw an exception, however, it doesn't work, I think I should change the main function's constructor, however I don't know how to do it.)

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());
    }
like image 891
Xiaolong Avatar asked Oct 30 '22 14:10

Xiaolong


1 Answers

however, it doesn't work,

You define TodoItem class, but in main() you create TodoItemDone. When I change TodoItem to TodoItemDone, I have 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)

Exception throw correctly

like image 157
Slava Vedenin Avatar answered Nov 15 '22 05:11

Slava Vedenin