first commit
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
#BlueJ class context
|
||||
comment0.params=server\ user
|
||||
comment0.target=MailClient(MailServer,\ java.lang.String)
|
||||
comment0.text=\n\ Create\ a\ mail\ client\ run\ by\ user\ and\ attached\ to\ the\ given\ server.\n
|
||||
comment1.params=
|
||||
comment1.target=MailItem\ getNextMailItem()
|
||||
comment1.text=\n\ Return\ the\ next\ mail\ item\ (if\ any)\ for\ this\ user.\n
|
||||
comment2.params=
|
||||
comment2.target=void\ printNextMailItem()
|
||||
comment2.text=\n\ Print\ the\ next\ mail\ item\ (if\ any)\ for\ this\ user\ to\ the\ text\ \n\ terminal.\n
|
||||
comment3.params=to\ message
|
||||
comment3.target=void\ sendMailItem(java.lang.String,\ java.lang.String)
|
||||
comment3.text=\n\ Send\ the\ given\ message\ to\ the\ given\ recipient\ via\n\ the\ attached\ mail\ server.\n\ @param\ to\ The\ intended\ recipient.\n\ @param\ message\ The\ text\ of\ the\ message\ to\ be\ sent.\n
|
||||
numComments=4
|
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* A class to model a simple email client. The client is run by a
|
||||
* particular user, and sends and retrieves mail via a particular server.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class MailClient
|
||||
{
|
||||
// The server used for sending and receiving.
|
||||
private MailServer server;
|
||||
// The user running this client.
|
||||
private String user;
|
||||
|
||||
/**
|
||||
* Create a mail client run by user and attached to the given server.
|
||||
*/
|
||||
public MailClient(MailServer server, String user)
|
||||
{
|
||||
this.server = server;
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the next mail item (if any) for this user.
|
||||
*/
|
||||
public MailItem getNextMailItem()
|
||||
{
|
||||
return server.getNextMailItem(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the next mail item (if any) for this user to the text
|
||||
* terminal.
|
||||
*/
|
||||
public void printNextMailItem()
|
||||
{
|
||||
MailItem item = server.getNextMailItem(user);
|
||||
if(item == null) {
|
||||
System.out.println("No new mail.");
|
||||
}
|
||||
else {
|
||||
item.print();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the given message to the given recipient via
|
||||
* the attached mail server.
|
||||
* @param to The intended recipient.
|
||||
* @param message The text of the message to be sent.
|
||||
*/
|
||||
public void sendMailItem(String to, String message)
|
||||
{
|
||||
MailItem item = new MailItem(user, to, message);
|
||||
server.post(item);
|
||||
}
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
#BlueJ class context
|
||||
comment0.params=from\ to\ message
|
||||
comment0.target=MailItem(java.lang.String,\ java.lang.String,\ java.lang.String)
|
||||
comment0.text=\n\ Create\ a\ mail\ item\ from\ sender\ to\ the\ given\ recipient,\n\ containing\ the\ given\ message.\n\ @param\ from\ The\ sender\ of\ this\ item.\n\ @param\ to\ The\ intended\ recipient\ of\ this\ item.\n\ @param\ message\ The\ text\ of\ the\ message\ to\ be\ sent.\n
|
||||
comment1.params=
|
||||
comment1.target=java.lang.String\ getFrom()
|
||||
comment1.text=\n\ @return\ The\ sender\ of\ this\ message.\n
|
||||
comment2.params=
|
||||
comment2.target=java.lang.String\ getTo()
|
||||
comment2.text=\n\ @return\ The\ intended\ recipient\ of\ this\ message.\n
|
||||
comment3.params=
|
||||
comment3.target=java.lang.String\ getMessage()
|
||||
comment3.text=\n\ @return\ The\ text\ of\ the\ message.\n
|
||||
comment4.params=
|
||||
comment4.target=void\ print()
|
||||
comment4.text=\n\ Print\ this\ mail\ message\ to\ the\ text\ terminal.\n
|
||||
numComments=5
|
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* A class to model a simple mail item. The item has sender and recipient
|
||||
* addresses and a message string.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class MailItem
|
||||
{
|
||||
// The sender of the item.
|
||||
private String from;
|
||||
// The intended recipient.
|
||||
private String to;
|
||||
// The text of the message.
|
||||
private String message;
|
||||
|
||||
/**
|
||||
* Create a mail item from sender to the given recipient,
|
||||
* containing the given message.
|
||||
* @param from The sender of this item.
|
||||
* @param to The intended recipient of this item.
|
||||
* @param message The text of the message to be sent.
|
||||
*/
|
||||
public MailItem(String from, String to, String message)
|
||||
{
|
||||
this.from = from;
|
||||
this.to = to;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The sender of this message.
|
||||
*/
|
||||
public String getFrom()
|
||||
{
|
||||
return from;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The intended recipient of this message.
|
||||
*/
|
||||
public String getTo()
|
||||
{
|
||||
return to;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The text of the message.
|
||||
*/
|
||||
public String getMessage()
|
||||
{
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print this mail message to the text terminal.
|
||||
*/
|
||||
public void print()
|
||||
{
|
||||
System.out.println("From: " + from);
|
||||
System.out.println("To: " + to);
|
||||
System.out.println("Message: " + message);
|
||||
}
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=MailServer()
|
||||
comment0.text=\n\ Construct\ a\ mail\ server.\n
|
||||
comment1.params=who
|
||||
comment1.target=int\ howManyMailItems(java.lang.String)
|
||||
comment1.text=\n\ Return\ how\ many\ mail\ items\ are\ waiting\ for\ a\ user.\n\ @param\ who\ The\ user\ to\ check\ for.\n\ @return\ How\ many\ items\ are\ waiting.\n
|
||||
comment2.params=who
|
||||
comment2.target=MailItem\ getNextMailItem(java.lang.String)
|
||||
comment2.text=\n\ Return\ the\ next\ mail\ item\ for\ a\ user\ or\ null\ if\ there\n\ are\ none.\n\ @param\ who\ The\ user\ requesting\ their\ next\ item.\n\ @return\ The\ user's\ next\ item.\n
|
||||
comment3.params=item
|
||||
comment3.target=void\ post(MailItem)
|
||||
comment3.text=\n\ Add\ the\ given\ mail\ item\ to\ the\ message\ list.\n\ @param\ item\ The\ mail\ item\ to\ be\ stored\ on\ the\ server.\n
|
||||
numComments=4
|
@@ -0,0 +1,69 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* A simple model of a mail server. The server is able to receive
|
||||
* mail items for storage, and deliver them to clients on demand.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class MailServer
|
||||
{
|
||||
// Storage for the arbitrary number of mail items to be stored
|
||||
// on the server.
|
||||
private List<MailItem> items;
|
||||
|
||||
/**
|
||||
* Construct a mail server.
|
||||
*/
|
||||
public MailServer()
|
||||
{
|
||||
items = new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return how many mail items are waiting for a user.
|
||||
* @param who The user to check for.
|
||||
* @return How many items are waiting.
|
||||
*/
|
||||
public int howManyMailItems(String who)
|
||||
{
|
||||
int count = 0;
|
||||
for(MailItem item : items) {
|
||||
if(item.getTo().equals(who)) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the next mail item for a user or null if there
|
||||
* are none.
|
||||
* @param who The user requesting their next item.
|
||||
* @return The user's next item.
|
||||
*/
|
||||
public MailItem getNextMailItem(String who)
|
||||
{
|
||||
Iterator<MailItem> it = items.iterator();
|
||||
while(it.hasNext()) {
|
||||
MailItem item = it.next();
|
||||
if(item.getTo().equals(who)) {
|
||||
it.remove();
|
||||
return item;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the given mail item to the message list.
|
||||
* @param item The mail item to be stored on the server.
|
||||
*/
|
||||
public void post(MailItem item)
|
||||
{
|
||||
items.add(item);
|
||||
}
|
||||
}
|
@@ -0,0 +1,33 @@
|
||||
Project: mail-system
|
||||
Authors: David Barnes and Michael Kölling
|
||||
|
||||
This project is part of the material for the book
|
||||
|
||||
Objects First with Java - A Practical Introduction using BlueJ
|
||||
Sixth edition
|
||||
David J. Barnes and Michael Kölling
|
||||
Pearson Education, 2016
|
||||
|
||||
It is discussed in chapter 3.
|
||||
|
||||
This project simulates a simple email system. Mail clients simulate email programs
|
||||
of different users. If you create two or more email clients, they can send messages
|
||||
to each other. (Note this this is a simulation: you cannot really send email. All
|
||||
messages are exchanged only between the email client objects in this project.)
|
||||
|
||||
To use this project,
|
||||
|
||||
- create a MailServer instance.
|
||||
- create a MailClient instance. Here you have to pass the mail server as a
|
||||
parameter, and you have to make up a name for this user.
|
||||
- create a second MailClient object with the same mail server and a different user
|
||||
name.
|
||||
- Use the "sendMessage" method of a MailClient object to send a message to the
|
||||
other mail client.
|
||||
- Use the "printNextMessage" method of the second mail client to receive the message.
|
||||
|
||||
The purpose of this project is to demonstrate object interaction. It includes external
|
||||
method calls (one object calling methods of another object) and object creation
|
||||
statements.
|
||||
|
||||
The debugger can be used to investigate these object interactions.
|
@@ -0,0 +1,64 @@
|
||||
#BlueJ package file
|
||||
dependency1.from=MailClient
|
||||
dependency1.to=MailServer
|
||||
dependency1.type=UsesDependency
|
||||
dependency2.from=MailClient
|
||||
dependency2.to=MailItem
|
||||
dependency2.type=UsesDependency
|
||||
dependency3.from=MailServer
|
||||
dependency3.to=MailItem
|
||||
dependency3.type=UsesDependency
|
||||
objectbench.height=89
|
||||
objectbench.width=766
|
||||
package.editor.height=365
|
||||
package.editor.width=658
|
||||
package.editor.x=70
|
||||
package.editor.y=80
|
||||
package.numDependencies=3
|
||||
package.numTargets=3
|
||||
package.showExtends=true
|
||||
package.showUses=true
|
||||
project.charset=UTF-8
|
||||
readme.editor.height=776
|
||||
readme.editor.width=876
|
||||
readme.editor.x=53
|
||||
readme.editor.y=24
|
||||
target1.editor.height=749
|
||||
target1.editor.width=844
|
||||
target1.editor.x=113
|
||||
target1.editor.y=40
|
||||
target1.height=60
|
||||
target1.name=MailItem
|
||||
target1.naviview.expanded=true
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.typeParameters=
|
||||
target1.width=100
|
||||
target1.x=400
|
||||
target1.y=220
|
||||
target2.editor.height=767
|
||||
target2.editor.width=863
|
||||
target2.editor.x=53
|
||||
target2.editor.y=60
|
||||
target2.height=60
|
||||
target2.name=MailServer
|
||||
target2.naviview.expanded=true
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.typeParameters=
|
||||
target2.width=100
|
||||
target2.x=90
|
||||
target2.y=160
|
||||
target3.editor.height=527
|
||||
target3.editor.width=712
|
||||
target3.editor.x=132
|
||||
target3.editor.y=79
|
||||
target3.height=60
|
||||
target3.name=MailClient
|
||||
target3.naviview.expanded=false
|
||||
target3.showInterface=false
|
||||
target3.type=ClassTarget
|
||||
target3.typeParameters=
|
||||
target3.width=100
|
||||
target3.x=250
|
||||
target3.y=80
|
Reference in New Issue
Block a user