public class MilkAndCookies
2: import java.util.Scanner;
4: /**
5: * A program that tries to divvy up milk and cookies between classes in a
6: * daycare.
7: *
8: * @author Mark Young (A00000000)
9: */
10: public class MilkAndCookies {
12: public static void main(String[] args) {
13: // introduce yourself
14: System.out.print("\n\n"
15: + "Milk And Cookies Reports\n"
16: + "------------------------\n"
17: + "\n"
18: + "Every room should have some milk -- "
19: + "\nbut things aren't always the way they should be!"
20: + "\n\n");
22: // room #1 report
23: try {
24: System.out.println("Report from room #1");
25: reportOnMilkAndCookies();
26: System.out.println("Room #1 is doing fine!");
27: } catch (RuntimeException e) {
28: System.out.println(e.getMessage());
29: System.out.println("I suspect there was no milk!");
30: }
31: System.out.println();
33: // room #2 report
34: try {
35: System.out.println("Report from room #2");
36: reportOnMilkAndCookies();
37: System.out.println("Room #2 is doing fine!");
38: } catch (RuntimeException e) {
39: System.out.println(e.getMessage());
40: System.out.println("Something's wrong in room 2???");
41: System.out.println("Oh, well!");
42: }
43: System.out.println();
45: // room #3 report
46: try {
47: System.out.println("Report from room #3");
48: reportOnMilkAndCookies();
49: System.out.println("Room #3 is doing fine!");
50: } catch (RuntimeException e) {
51: System.out.println("Something's wrong in room #3");
52: System.out.println("I'm quitting!");
53: System.exit(1);
54: }
55: System.out.println();
57: System.out.println("And we're done!\n");
58: }
60: /**
61: * Get data from user and report satisfaction of constraints.
62: * Throw an exception if something goes wrong.
63: *
64: * @throws NotEnufMilkException if there's no milk
65: * @throws NotEnufCookiesException if there're no cookies
66: */
67: public static void reportOnMilkAndCookies() {
68: Scanner kbd = new Scanner(System.in);
70: System.out.print("Enter number of cookies: ");
71: int numCookies = kbd.nextInt();
72: kbd.nextLine();
74: System.out.print("Enter number of milk cartons: ");
75: int numMilks = kbd.nextInt();
76: kbd.nextLine();
78: if (numMilks < 1) {
79: throw new RuntimeException("Something is not right!");
80: } else if (numCookies < 1) {
81: throw new RuntimeException("Something else is not right!");
82: }
84: double cookiesPerMilk = (double)numCookies / (double)numMilks;
85: System.out.println("\n"
86: + "You have " + cookiesPerMilk
87: + " cookies for each carton of milk.");
88: }
89: }