You are on page 1of 4

002 BULDING SERVICE STUB

1. Now we will create a messageService to add , update, delete the messages


But before that let us create a basic profile class which is very similar to that Message class

package com.module.model;
//this class is created for 003 docs...message service stub
import java.util.Date;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Profile {

private long id;


private String profileName;
private String firstName;
private String lastName;
private Date created;

public Profile(){

public Profile(long id,String profileName, String


firstName,String lastName){
this.id=id;
this.profileName=profileName;
this.firstName=firstName;
this.lastName=lastName;
this.created=new Date();
}

public long getId() {


return id;
}
public void setId(long id) {
this.id = id;
}
public String getProfileName() {
return profileName;
}
public void setProfileName(String profileName) {
this.profileName = profileName;
}

public String getFirstName() {


return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}

2. Let us create a sample database class, we are just creating a hashmap and not really inserting
into database here

package com.module.database;

import java.util.HashMap;
import java.util.Map;

import com.module.model.Message;
import com.module.model.Profile;

public class SampleDatabase {

//we are not going to insert in database here...


//but for sample we are writing this sample class as our dtabase
class
//message map and profile map which maps id to message and id to
profiles respectively
private static Map<Long,Message> messages=new HashMap<>();
private static Map<Long,Profile> profiles=new HashMap<>();

public static Map<Long, Message> getMessages(){


return messages;
}

public static Map<Long, Profile> getProfiles(){


return profiles;
}

//now what we are going to do with this...we are going to create


, update, delete to this messages map..
//so go and create new methods in MessageService class
}

3. Now update message service class with new methods as follows

package com.module.service;

import java.sql.DatabaseMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.module.database.SampleDatabase;
import com.module.model.Message;

public class MessageService {

//we created a local member varibale which is map of long and


message and getting messages from
SampleDatabase..SampleDatabase.getMessages();

private static Map<Long,Message>


messages=SampleDatabase.getMessages();

public MessageService(){
//creating a new messages with ID=> 1L for HashMap<Long,
Message>
messages.put(1L,new Message(1,"GOOD MORNING","GITESH MORE"));
messages.put(2L,new Message(2, "GOOD AFTERNOON","SHAILESH"));
messages.put(3L,new Message(3,"GOOD EVENING","KIRAN"));
}
// now next is we need a service that returns list of messages
public List<Message> getAllMessages(){
//deleted previous stuff
//we are going to have list of all messages...and this we
want as list so done as follows
return new ArrayList<Message>(messages.values());
//passing a collection to the arraylist constructor
initialized List with those elements
}

//to get only one message


public Message getMessage(long id){
return messages.get(id);
}

//to add message


public Message addmessage(Message message){
message.setId(messages.size()+1);
messages.put(message.getId(), message);
return message;
}

//to update message


public Message updateMessage(Message message){
if(message.getId()<=0){
return null;
}
messages.put(message.getId(), message);
return message;
}

//to remove message


public Message removeMessage(long id){
return messages.remove(id);
}
}

You might also like