- #1
blah45
- 65
- 0
Java problem ArrayList problem, **URGENT** hard question
Introduction:
You are part of a team working on a software system for a mobile phone company. The team is working on the development of the component that will be used for storing text messages. The initial design has a structure using two classes.
THE MESSAGE CLASS:
The first class is called Message and will be used to store the details of a message.
A message contains the following:
String from the number of the person sending the message.
String message the actual text of the message
long timeReceived the time the message was received. The timeReceived value can be set from the System clock.
Once created messages are immutable.
THE MESSAGELIST CLASS:
The second class is called MessageList. It is used for storing a list of messages (i.e. instances of the Message class). A message list is initially empty. Messages can be added, removed, displayed and moved from one list to another. The contents of a list can also be erased. NOTE, the messages in a MessageList are ALWAYS stored in descending timeReceived order (i.e. most recently received is first).
The class should support/provide the following operations
• Allow new messages to be inserted into the list. Message lists are stored in descending timeReceived order (i.e. most recent first). The header for the add operation is
public void add(Message m).
• Allow messages to be moved from one list to another. When a message is moved it is removed from the original list and added to the receiving list. If the receiving list does not exist then the message should not be removed from the original list.
public void move(int position, MessageList ml)
***************************************
************** MY SOLUTION**************
***************************************
**NOTE**
There is other methods but these to are the only too I need help on. Please help Thanks =D
I don't want to optimize the code, the methods run fine as they are bar the adding messages in time descending order. please help
**/Note**
Introduction:
You are part of a team working on a software system for a mobile phone company. The team is working on the development of the component that will be used for storing text messages. The initial design has a structure using two classes.
THE MESSAGE CLASS:
The first class is called Message and will be used to store the details of a message.
A message contains the following:
String from the number of the person sending the message.
String message the actual text of the message
long timeReceived the time the message was received. The timeReceived value can be set from the System clock.
Once created messages are immutable.
THE MESSAGELIST CLASS:
The second class is called MessageList. It is used for storing a list of messages (i.e. instances of the Message class). A message list is initially empty. Messages can be added, removed, displayed and moved from one list to another. The contents of a list can also be erased. NOTE, the messages in a MessageList are ALWAYS stored in descending timeReceived order (i.e. most recently received is first).
The class should support/provide the following operations
• Allow new messages to be inserted into the list. Message lists are stored in descending timeReceived order (i.e. most recent first). The header for the add operation is
public void add(Message m).
• Allow messages to be moved from one list to another. When a message is moved it is removed from the original list and added to the receiving list. If the receiving list does not exist then the message should not be removed from the original list.
public void move(int position, MessageList ml)
***************************************
************** MY SOLUTION**************
***************************************
**NOTE**
There is other methods but these to are the only too I need help on. Please help Thanks =D
I don't want to optimize the code, the methods run fine as they are bar the adding messages in time descending order. please help
**/Note**
Code:
//This is the Message class//
public class Message
{
private String from ;
private String message ;
private long timeReceived ;
private String readableTime ;
public Message(String Nmbr, String msg)
{
from = Nmbr ;
message = msg ;
readableTime = getTime() ;
timeReceived = System.currentTimeMillis() ;
System.out.println("This message is from " + from + " at " + getTime()) ;
System.out.println(message) ;
}
private String getTime()
{
long hour, min, sec ;
long currentTime = System.currentTimeMillis() ;
String formattedTime ;
sec = currentTime / 1000 ;
sec = sec % (24*60*60);
min = sec / 60 ;
sec = sec % 60 ;
hour = min / 60 ;
min = min % 60 ;
if(hour < 13) {
if(hour == 0) {
hour = 12 ;
}
formattedTime = hour + ":" + min + ":" + sec + "am" ;
}else {
if(hour != 12) {
hour = hour - 12;
}
formattedTime = hour +":"+ min + ":" + sec + "pm" ;
}
return formattedTime ;
}
public String getMessage()
{
return message ;
}
public String getNumber()
{
return from ;
}
public String getMessageTime()
{
return readableTime ;
}
public long getTimePc()
{
return timeReceived ;
}
}
//This is the MessageList class//
import java.util.* ;
public class MessageList
{
private ArrayList<Message> MessageList ;
public MessageList()
{
MessageList = new ArrayList<Message>() ;
}
public int size()
{
return MessageList.size() ;
}
public void display()
{
if(MessageList.size() == 0)
{
System.out.println("There is no messages in the list") ;
}
else
{
int top = MessageList.size() - 1 ;
int j = 1 ;
for(int i = 0 ; i < MessageList.size() ; i++ )
{
Message msg = MessageList.get(top) ;
System.out.println("Message " + j + "is ---> " + msg.getMessage()) ;
j++ ;
top-- ;
}
}
}
public void Display(int message)
{
int top = MessageList.size() - 1 ;
if(MessageList.size() == 0)
{
System.out.println("There is no messages in the list") ;
}
else if(message < MessageList.size())
{
Message msg = MessageList.get(message) ;
System.out.println(msg.getMessage()) ;
top-- ;
}
}
public void addMsg(Message msg)
{
if(MessageList.size() == 0)
{
MessageList.add(msg) ;
}
else
{
int i = 0 ;
int j = 0 ;
while(i <= MessageList.size())
{
int top = MessageList.size() - 1 ;
Message msg1 = MessageList.get(i) ;
if(msg1.getTimePc() > msg.getTimePc())
{
MessageList.add(msg) ;
}
else //(msg.getTimePc() < msg.getTimePc())
{
Message m = MessageList.get(top) ;
MessageList.remove(top);
MessageList.add(msg) ;
}
i++ ;
j++ ;
}
}
}
public Message getMSG(int position)
{
Message msg = null ;
if( position >= MessageList.size())
{
System.out.println("There is no message at " + position) ;
}
else
{
msg = MessageList.get(position) ;
System.out.println("There is a message at index " + position) ;
System.out.println("The Message is : " + msg.getMessage()) ;
}
return msg ;
}
public int find(String searchText)
{
int i = 0 ;
boolean found = false ;
int posMsg = 0 ;
while(i < MessageList.size() && !found)
{
Message msg = MessageList.get(i) ;
String MSG = msg.getMessage() ;
if(MSG.indexOf(searchText) >= 0 )
{
posMsg = i ;
found = true ;
}
else
{
posMsg = -1 ;
}
i++ ;
}
return posMsg ;
}
public void remove( int position)
{
Message msg = MessageList.remove(position) ;
}
public void move(int position, MessageList ml)
{
int i = 0;
Message msg = MessageList.get(position) ;
while(i < MessageList.size())
{
ml.addMsg(msg) ;
remove(position) ;
i++ ;
}
}
public void erase()
{
int i = 0 ;
while( MessageList.size() != 0 )
{
MessageList.remove(i) ;
i++ ;
}
System.out.println("The List has been erased") ;
}
public boolean isEmpty()
{
boolean exists = false ;
if(MessageList.size() == 0)
{
exists = true ;
}
return exists ;
}
}