Practice SWYK check

Set up

You can perform the following commands to download the template and test harness for this SWYK check:


For this problem, you are to write two classes, one called Phone and the other called Network. These are illustrated below:

 
Cell phone   Cellular network

How cell phones work

Every cell phone has its own unique number, which we will model as a String. (Not an integer, note.) When a Phone object is instantiated, it must be given a number, which it will use to connect to the network (see below).

Phones can do three main things:

However, phones have limited memory: they can only store up to 5 texts before their mailbox overflows. If the Network ever tries to send a phone a sixth text when that phone already has five queued up that its owner has not yet viewed, it will throw a MailboxFullException back at the Network.

Similarly, if a user tries to send a text to a friend who already has a full mailbox (i.e., 5 other unread texts), the .sendTextToFriend() will throw a MailboxFullException back at them.

How cellular networks work

Although there may be thousands of phones, there is only one Network in our world. It serves as a dispatcher and central staging point to deliver messages.

As explained above, whenever a new Phone is instantiated, it should register with the one-and-only Network by passing itself to the Network's .register() method. This method will take note of that new phone, and its phone number, so that texts can be delivered to it. The .register() method will barf with a NumberInUseException, however, if a second Phone ever tries to register with the same phone number that a previously-registered Phone has.

Finally, in order to send messages, Phone objects will call the .deliverText() method on the Network. This method will bounce back at them with the appropriate type of exception, of course, if they provide a non-working phone number (i.e. a number not registered with the network) or if they try to send a text to a recipient with a full mailbox.


The requirements

Instance variables

Your Phone and Network classes should probably each have instance variables. It's up to you to decide what they are.

Methods

Phone

In addition to a constructor, your Phone class must have an accessor method so that clients can get a Phone's phone number. It must also implement the three methods described above — .sendTextToFriend(), .acceptTextFromNetwork(), and .viewNextText().

Network

Your Network class must have the two methods described above (.register() and .deliverText()), as well as provide accessibility to its one and only instance.

Example code and output

try {
    Phone a = new Phone("5406541317");
    Phone b = new Phone("5402267901");
    a.sendTextToFriend("5402267901", "Hey, handsome!");
    a.sendTextToFriend("5402267901", "been a long time!");
    System.out.println(b.viewNextText());    <-- prints "Hey, handsome!"
    b.sendTextToFriend("5402267901", "get lost you're dead to me");
    System.out.println(b.viewNextText());    <-- prints "been a long time!"
    System.out.println(a.viewNextText());    <-- prints "get lost you're dead to me"
    System.out.println(a.viewNextText());    <-- prints nothing ("")
    System.out.println(b.viewNextText());    <-- prints nothing ("")
} catch (NumberInUseException e) {
    System.out.println("Duplicate cell numbers!");
} catch (NonWorkingNumberException e2) {
    System.out.println("Tried to send to a non-existent number!");
} catch (MailboxFullException e) {
    System.out.println("Recipient's mailbox is full!");
}

Side note

Notice that the above sample code doesn't even mention the Network class explicitly anywhere. The Network is certainly involved behind-the-scenes, though, in making all of that work properly!