
import java.util.Scanner;

/**
 * A program to demo basics of exception handling
 * by offering simple nutritional advice.
 * 
 * @author Lisa C. Kaczmarczyk, modified by Matt Boutell.
 *		Created Sep 5, 2006.
 *		Modified by David Mutchler, Sep 15, 2008.
 *		Modified for csse375 by Steve Chenoweth, Apr 12, 2014
 */
public class Exceptions {
	/**
	 * A basic MVC design, with no "model" to speak of. Here, main is an example of a very simple 
	 * UI "controller". It persists in getting a day of the week from "getWeekday", which is its "view",
	 * until either "getWeekday" returns "true" to say it has a day of the week, or else it provides an
	 * "exception" saying the user elected to do something different, namely, go to the prior week or
	 * to the next week.  This is all the program does.
	 * 
	 * The program doesn't have particularly good MVC structure, because the "nutritional advice" resulting
	 * from user input is provided directly by the "view"!
	 * 
	 * There are a few other sorts of Exceptions shown, just to exemplify how these work in Java.  
	 * The program was originally a project for CSSE221. 
	 * 
	 * @param args Command-line arguments (unused)
	 */
	public static void main(String[] args) {
		try {
			boolean gotIt = false; // no day specified yet
			while (!gotIt) {
				gotIt = Exceptions.getWeekday();
				System.out.println("returned "+gotIt);
			}
		} catch (MyException myEx) {
			System.out.println(myEx.getMessage());
		}
	}

	/*
	 * Tell the user what they get to eat based upon how well they follow
	 * directions.
	 */
	private static boolean getWeekday() throws MyException {
		// find out if it is a weekday
		// and reward the user accordingly.
		Scanner scanner = new Scanner(System.in);
		boolean returnVal = true; // default return value = ok
		System.out.println("Use a number to tell me what weekday it is.");
		System.out.print("1-Monday, 2-Tuesday, 3-Wednesday, 4-Thursday, 5-Friday, old-Last wk, new-Next wk: ");
		int day = 0;
		String myLine = scanner.nextLine();

		try {
     		day = Integer.parseInt(myLine);
		} catch (NumberFormatException e1){
        	if (myLine.equals("new")) {
        		throw new MyException("Next wk");
        	} else if (myLine.equals("old")) {
        		throw new MyException("Last wk");
        	} else {
        		System.out.println("Error -- input a number -- Try again");
        		returnVal = false;
        	}
		}
		try {
			if (day == 6 || day == 7) {
				throw new Exception("Are you trying to say it is the weekend???");
			} else if (day > 7) {
				throw new Exception("There aren't that many days in a week");
			} else if (day < 1) {
				throw new Exception("Um...That makes no sense");
			}

			System.out.println("Because it is a weekday, you get to eat gummy bears. Good Job!");

			Exceptions.bugs(day); // results of your choice!

		} catch (Exception exception) {
			String problem = exception.getMessage();
			System.out.println(problem + " - you have to eat brussel sprouts");
			returnVal = false;
		
		} finally {
			System.out.println("Weekdays are suddenly looking much more appealing, aren't they?");
		}
		return returnVal;
	}

	/**
	 * This method simulates some buggy code.  Leave it as it is.
	 * 
	 * @param day Day that the user inputs.
	 */
	private static void bugs(int day) {
		int x = 500000000 * day; // Purposeful errors, leave them here.
		System.out.println("Five hundred million times 'day' is " + x);
		if (day == 3) {
			System.out.println(1 / 0);
		}
	}
}
