Em co bai Test Java kha kho, su huynh nao gioi ve Java giup em voi, khan cap lam roi!
Anh nao gioi Java xin giup em giai bai test nay voi, khan cap lam em phai nop vao 7/4/2011 do. Giup em voi!!
Your task in this assignment is to build a program that can store a user’s e-mail data in a file and
print it to the screen. The e-mail is not sent. This will be the task of assignment 2.
1. To build a simple Mail Client (MC) that prompts the user for typical email fields and then
writes SMTP protocol messages to an output file.
2. To develop a simple Java console user interface that prompts for and accepts user typed
input and handles errors.
3. To handle Java command line arguments.
4. To understand and construct SMTP protocol messages.
5. To understand how to use Java Streams to read and write data.
6. To build a simple object-oriented program.
7. To be able to write a simple description of how to use your program.
A detailed marking scheme is part of this document (see below). You will be asked to answer
questions about your solution in the lab after the submission date. If you cannot explain all parts
of your program, it cannot be considered your own work and you cannot claim marks for it.
Your program must consist of at least three files (= classes), Test.java, Message.java and
1. Test.java has includes the main method which starts the program. The program reads the
first argument. The first argument can be ‘read’ or ‘print’. If it is anything else, display an
error message that tells the user to state either ‘read’ or ‘print’. If the argument is correct
(‘read’ or ‘print’), one of two auxiliary methods in Test.java is called.
a. If the argument is ‘read’, the program calls a method which asks the user to type all
parts of the message to the console. The user has to be prompted for each part
separately, for example “Type message receiver: “ tells the user to type the
receiver’s e-mail address. Once the user has typed the input, it is stored in an
object of type Message.
b. When all parts of the message have been recorded, the message is written to a file
called smtpstorage.txt. The parts of the message are stored on separate lines.
c. If the first argument to the program is ‘print’, the appropriate method in Test.java
is called. This method reads the parts of the message from the file. The parts are
stored in the Message object. When all part of the message are complete, the e-mail
is printed to the console in the format:
MAIL FROM: abc.hutech.edu.vn
RCPT TO: def.hutech.edu.vn, xyz.hutech.edu.vn
Subject: Meeting at 1pm
I am afraid I will have to reschedule. Does 3pm suit you?
d. Test.java has an auxiliary method which validates e-mail addresses using a regular
expression. This method is called whenever an e-mail address is recorded from user input.
The program must display an error message on receiving an invalid e-mail address. It
must then ask the user for a valid address.
2. Message.java has variables for all parts of the SMTP message content. These variables
must be private and not be accessed from Test.java, except through the appropriate
a. sender, recipient, subject, data, mailDomain
b. E-mails can have more than one recipient. Users can type several e-mail addresses
separated by commas. All addresses must be validated and stored.
c. The SMTP commands (such as ‘HELO’, ‘MAIL FROM:’ are stored in constants.
d. Message.java has a toString() method which returns the complete message in the
format displayed under 1.c.
3. FileStorage.java has at least three methods, one to open the file smtpstorage.txt, one to
close it and one to write the message to it. For that purpose, the FileStorage needs two
objects, a file reader and a file writer. The best way to go is to have a FileStorage
constructor that takes a Boolean value for “openForReading”. If it is true, initialise the file
reader, if it is false, initialise the file writer.
The file “smtpstorage.txt” must be created in the directory of the Test class.
You must submit the following as part of the assignment
1. Complete source code of all java classes your mail client needs. The classes must be
named as described above. You can submit additional classes. It must be possible to
invoke your program using:
java Test read
java Test print