THINK FIRST·CODE LATER

← All labs

One message, three channels: programming to an interface

Problem

Write an interface Notifier with String send(String message), int cost() (in fen) and String channel(), and three classes that implement it:

  • EmailNotifier(address) — To lina@wku.edu.cn: message, free;
  • SmsNotifier(phone) — SMS 13800001111: message, 10 fen; a text longer than LIMIT = 20 characters is cut to 17 characters plus ...;
  • PushNotifier(device) — [phone-1] MESSAGE in upper case, 1 fen.

Notifications.main reads n, then n subscribers (email ADDRESS, sms PHONE or push DEVICE) into an ArrayList<Notifier>, then sends every following non-empty line to all of them and prints the number of notifications and the total cost in yuan.

For the input

3
email lina@wku.edu.cn
sms 13800001111
push phone-1
Lab moved to room B204
Quiz on Friday

the program prints

To lina@wku.edu.cn: Lab moved to room B204
SMS 13800001111: Lab moved to room...
[phone-1] LAB MOVED TO ROOM B204
To lina@wku.edu.cn: Quiz on Friday
SMS 13800001111: Quiz on Friday
[phone-1] QUIZ ON FRIDAY
6 notifications sent
Cost: 0.22 yuan

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l15-5-data-1.txt

3
email lina@wku.edu.cn
sms 13800001111
push phone-1
Lab moved to room B204
Quiz on Friday
What your program must print for l15-5-data-1.txt
To lina@wku.edu.cn: Lab moved to room B204
SMS 13800001111: Lab moved to room...
[phone-1] LAB MOVED TO ROOM B204
To lina@wku.edu.cn: Quiz on Friday
SMS 13800001111: Quiz on Friday
[phone-1] QUIZ ON FRIDAY
6 notifications sent
Cost: 0.22 yuan

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l15-5-data-1.txt

Windows PowerShell does not understand <:

Get-Content l15-5-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.