SRS – Server Side Code Part 2

We posted code in four different articles. You can read and obtain code from links given below :-
SRS – Server Side Code 1
SRS- Server Side Code Part2
SRS – Server Side Code Part 3
SRS – Android Client Code Part 4

Reading and Writing response in a file – Server side

package services;

import java.io.*;
/**
  *@Author Gulraiz Iqbal 
  */

public class FileService {

	String filePath;
	Services services;
	public FileService(String path,Services serv){
		this.filePath = path;
		this.services = serv;
	}
	
	public boolean isFileAvailable(){
		 File f = new File(this.filePath+"/services.ser");
		 if(f.exists())
			 return true;
		 return false;
	}
	
	public Services serializeServices(){
		try
	      {
	         FileOutputStream fileOut =
	         new FileOutputStream(this.filePath+"services.ser");
	         ObjectOutputStream out = new ObjectOutputStream(fileOut);
	         out.writeObject(this.services);
	         out.close();
	         fileOut.close();
	         System.out.printf("Serialized data is saved in "+this.filePath+"services.ser");
	      }catch(IOException i)
	      {
	          i.printStackTrace();
	      }
		return this.services;
	   }
	
	public Services deserializeServices(){
		 try
	      {
	         FileInputStream fileIn = new FileInputStream(this.filePath+"services.ser");
	         ObjectInputStream in = new ObjectInputStream(fileIn);
	         this.services = (Services) in.readObject();
	         in.close();
	         fileIn.close();
	      }catch(IOException i)
	      {
	         i.printStackTrace();
	      }catch(ClassNotFoundException c)
	      {
	         System.out.println("Services class not found");
	         c.printStackTrace();
	      }
		 return this.services;
	}
}

Service Class – Handles most of functionality

package services;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import questions.*;

/**
  *@Author Gulraiz Iqbal 
  */

public class Services implements Serializable {
	private static final long serialVersionUID = 1L;
	Hashtable> questions;
	Hashtable> answers;
	ArrayList users;
	Question curr=null;
	String session=null;
	int count=1;
	
	public Services() {
		this.questions = new Hashtable>();
		this.users = new ArrayList();
		this.answers = new Hashtable>();
	}

	public Object[] getQuestionKeys(){
		return	this.questions.keySet().toArray();
	}
	
	public Object[] getAnswerKeys(){
		return	this.answers.keySet().toArray();
	}
	
//sKey is a session key.. every session results are stored seperately
	public void addAnswer(String sKey,AnswerSheet ans) {
		if (this.answers.containsKey(sKey)){
			ArrayList list = this.answers.get(sKey);
			list.add(ans);
		}
		else {
			ArrayList list = new ArrayList();
			list.add(ans);
			this.answers.put(sKey, list);
		}
	}

	//Get all answers using session keys
	public ArrayList getAnswers(String sKey) {
		return this.answers.get(sKey);
	}
	
	public void addQuestion(Question ques, String key) {
		if (this.questions.containsKey(key)){
			ArrayList list = this.questions.get(key);
			list.add(ques);
		}
		else {
			ArrayList list = new ArrayList();
			list.add(ques);
			this.questions.put(key, list);
		}
		this.curr = ques;
	}

	
	public void setCurrQuestion(Question q) {
		this.curr=q;
	}
	
	public Question getCurrQuestion() {
		return this.curr;
	}
	
	
	public ArrayList getQuestions(String key) {
		return this.questions.get(key);
	}

	public void addUser(User user) {
		this.users.add(user);
	}

	public User getUser(int id) {
		for(int i=0; i

Maintaining User record

Here is User class - Student Response System

package services;

import java.io.Serializable;
/**
 * @author Gulraiz
 */

public class User implements Serializable {
	
	private static final long serialVersionUID = 1L;
	String id;
	String ip;
	String name;

	public User(String id, String ip) {
		this.id = id;
		this.ip = ip;
	}

	public User(String id, String ip, String name) {
		this.id = id;
		this.ip = ip;
		this.name = name;
	}

	public String getId() {
		return this.id;
	}

	public String toString() {
		return this.id + ":" + this.ip + ":" + this.name;
	}
}

jFree Chart is used to visualize results

Here is Student Response System VisualizResults class

package visualize;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel; 
import org.jfree.chart.JFreeChart; 
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset; 
import org.jfree.data.category.DefaultCategoryDataset;  

import questions.AnswerSheet;
import services.Services;

public class VisualizResults 
{
   public VisualizResults( String applicationTitle , Services serv)
   {
      
	   JFrame f = new JFrame(applicationTitle);
       f.setTitle(applicationTitle);
       
       f.setLayout(new BorderLayout(0, 5));
       
       String s = (String)JOptionPane.showInputDialog(
               f,
               "Select a session to visualize:\n"
               + "\"for example:  J06-01-2015:7\"",
               "Customized Dialog",
               JOptionPane.PLAIN_MESSAGE,
               null,
               serv.getAnswerKeys(),
               null);
       
       if(s!=null){
	   
      JFreeChart barChart = ChartFactory.createBarChart(
    		  applicationTitle,           
         "Question Choices",            
         "% student response",            
         createDataset(serv,s),          
         PlotOrientation.VERTICAL,          
         true, true, false);
         
      ChartPanel chartPanel = new ChartPanel( barChart );        
      chartPanel.setPreferredSize(new Dimension( 560 , 367 ) ); 
      
      
      f.add(chartPanel, BorderLayout.CENTER);
      

     
      f.pack();
      f.setLocationRelativeTo(null);
      f.setVisible(true);
   }
   }
   private CategoryDataset createDataset(Services serv, String session )
   {
              
	   ArrayList ans = serv.getAnswers(session);
	   
	   double totalAns = ans.size();
	   double correct =0;
	   double wrong =0;
	   double totalTimeSpent=0;
	   double timeOnCorrectAns =0;
	   double timeOnWrongAns=0;
	   
	   
	   for(int i=0; i < ans.size(); i++){
		   totalTimeSpent+=ans.get(i).timeSpent();
		   if(ans.get(i).isCorrectAns()){
			   correct++;
			   timeOnCorrectAns+=ans.get(i).timeSpent();
		   }
		   else{
			   wrong++;
			   timeOnWrongAns+=ans.get(i).timeSpent();
		   }
			   
	   }
	   
	   
	   
      final DefaultCategoryDataset dataset = 
      new DefaultCategoryDataset( );  
      
      System.out.println(ans.size());
      double percCorrect =(correct/totalAns)*100;
      double percWrong = (wrong/totalAns)*100;
      
	 
      dataset.addValue(  percCorrect, "Answer" , "Correct" );        
      dataset.addValue( percWrong, "Answer" , "Wrong" );
      
     // dataset.addValue( (timeOnCorrectAns/totalTimeSpent)*100 , "Time" , "Correct" );        
     // dataset.addValue( (timeOnWrongAns/totalTimeSpent)*100, "Time" , "Wrong" );
                

                   

      return dataset; 
   }
}

No Responses