public class RoomReports
2: import java.util.Scanner;
4: /**
5: * A program to distribute milk and cookies, but cognizant of the fact that
6: * under exceptional circumstances, there may be no milk (or cookies) to
7: * distribute.
8: *
9: * @author Mark Young (A00000000)
10: */
11: public class RoomReports {
13: /** The number of rooms in this facility. */
14: public static final int NUM_ROOMS = 5;
16: public static void main(String[] args) {
17: // introduce yourself
18: System.out.print("\n\n"
19: + "Room Reports\n"
20: + "------------\n"
21: + "\n"
22: + "Every room should have enuf milk and cookies "
23: + "for every child.\n"
24: + "But things aren't always the way they should be!"
25: + "\n\n");
27: // report on each room
28: for (int i = 1; i <= NUM_ROOMS; i++) {
29: try {
30: reportFromRoom(i);
31: } catch (NotEnufMilkException e) {
32: System.out.println("Oh no! Not enuf milk!");
33: System.out.println("Children's services has shut us down!");
34: System.exit(1);
35: } catch (NotEnufCookiesException e) {
36: System.out.println("Oh no! Not enuf cookies!");
37: System.out.println("Do not distribute cookies in room " + i
38: + " today -- we'll do doubles tomorrow!");
39: }
40: System.out.println();
41: }
43: System.out.println("And we're done!\n");
44: }
46: /**
47: * Collect room data from the user and report on conditions.
48: *
49: * @throws NotEnufMilkException if there's not enuf milk for each child to
50: * get their own carton.
51: * @throws NotEnufCookiesException if there aren't enuf cookies for each
52: * child to have one.
53: */
54: public static void reportFromRoom(int n) {
55: Scanner kbd = new Scanner(System.in);
57: // get number of children
58: System.out.println("Report from room #" + n);
59: System.out.print("Enter number of children: ");
60: int numChildren = kbd.nextInt();
61: kbd.nextLine();
63: // get number of cookies
64: System.out.print("Enter number of cookies: ");
65: int numCookies = kbd.nextInt();
66: kbd.nextLine();
68: // get number of milk cartons
69: System.out.print("Enter number of milk cartons: ");
70: int numMilks = kbd.nextInt();
71: kbd.nextLine();
73: // throw a (suitable kind of) fit if conditions warrant
74: if (numMilks < numChildren) {
75: throw new NotEnufMilkException();
76: }
77: if (numCookies < numChildren) {
78: throw new NotEnufCookiesException();
79: }
81: // report all well
82: System.out.println("Room #" + n + " is doing OK.");
83: }
84: }