Im new to using HashMap. Im assigned a project to retrieve records from HashMap and display it as a Pie Chart on a JSP. However despite searching and look thru many different Hashmap examples. Im still stuck in my project which is about to due in 3 weeks time :( Kindly please give me some advice and i apologize if my code is in a mess.
Basically i have a Class which contain my records in HASHMAP.
public static void main(String[] args) throws IOException {
HashMap<String, ArrayList<String>> UserCatergory = new HashMap<String,ArrayList<String>>();
ArrayList<String> studentList = new ArrayList<String>();
ArrayList<String> parentList = new ArrayList<String>();
// get some user id and add them to appropriate group
studentList.add("abc");
studentList.add("cdf");
parentList.add("efg");
parentList.add("hij");
//add all into overallList for overallCount
overallList.addAll(studentList);
overallList.addAll(parentList);
// adding value into HashMap
UserCatergory.put( "Student", studentList );
UserCatergory.put( "Parent", parentList );
System.out.println("Enumerate all the keys in the HashMap");
//To get all keys stored in HashMap use keySet method.Set keySet()
for (String key : UserCatergory.keySet()){
System.out.println("Type of Group: " + key);
}
System.out.println();
System.out.println("Enumerate all the key/value entries in the HashMap");
//Extract keys into arrays To get all values stored in HashMap use entrySet() method.
//Set entrySet()
for (Entry<String, ArrayList<String>> entry : UserCatergory.entrySet()){
System.out.println(entry);
}
PieBean pb = new PieBean();
pb.setStudentListCount(studentList.size());
pb.setParentListCount(parentList.size());
for (int i=1; i <UserCatergory.size();i++)
{
System.out.println();
System.out.println("Total Groups: "+ UserCatergory.size());
System.out.println("Total Search For Student :" + pb.getStudentListCount());
System.out.println("Total Search For Parent :" + pb.getParentListCount());
System.out.println();
}
//start writing to the XML file
File fleExample = new File("PieChart.xml");
// Create that file and prepare to write some values to it
PrintWriter pwInput;
try {
pwInput = new PrintWriter(fleExample);
pwInput.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
pwInput.println("<pie>");
for (int i =1 ; i< UserCatergory.size(); i++){
pwInput.print("<slice color=\"#FF0000\" title=\"Student\" value =\"0\">" + pb.getStudentListCount() + "</slice>");
System.out.println("PieChart.. student");
}
for (int i =1 ; i< UserCatergory.size(); i++){
pwInput.print("<slice color=\"#FF0550\" title=\"Parent\" value =\"0\">" + pb.getParentListCount()+ "</slice>");
System.out.println("PieChart..parent");
}
}
pwInput.println("</pie>");
// After using the PrintWriter object, de-allocated its memory
pwInput.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I have a PieBean that is used to retrieve the Counts for the PieChart.
public class PieBean {
int parentListCount;
int studentListCount;
public int getParentListCount() {
return parentListCount;
}
public void setParentListCount(int parentListCount) {
this.parentListCount = parentListCount;
}
public int getStudentListCount() {
return studentListCount;
}
public void setStudentListCount(int studentListCount) {
this.studentListCount = studentListCount;
}
}
I have this servlet to search for the catergory.
public class SearchUserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String search = request.getParameter("search");
HashMap<String, ArrayList<String>> UserCatergory = new HashMap<String,ArrayList<String>>();
HashMap<String, ArrayList<String>> studentList = new HashMap<String, ArrayList<String>>();
HashMap<String, ArrayList<String>> parentList = new HashMap<String, ArrayList<String>>();
UserPieChart upc = new UserPieChart(); ***Im not sure if this is required.
studentList= upc.UserCatergory;
parentList = upc.UserCatergory;
request.setAttribute("search", search);
request.setAttribute("studentList", studentList);
request.setAttribute("parentList", parentList);
PieBean pb = new PieBean();
pb.setStudentListCount(studentList.size());
pb.setParentListCount(parentList.size());
// start writing to the XML file
File fleExample = new File("C:/Workspace - FYPJ/FYPJ_Period2/WebContent/PieChart.xml");
// Create that file and prepare to write some values to it
PrintWriter pwInput;
try {
pwInput = new PrintWriter(fleExample);
pwInput.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
pwInput.println("<pie>");
for (int i =1 ; i< UserCatergory.size(); i++){
pwInput.print("<slice color=\"#FF0000\" title=\"Student\" value =\"0\">" + pb.getStudentListCount() + "</slice>");
//System.out.println("PieChart.. student");
}
for (int i =1 ; i< UserCatergory.size(); i++){
pwInput.print("<slice color=\"#FF0550\" title=\"Parent\" value =\"0\">" + pb.getParentListCount()+ "</slice>");
//System.out.println("PieChart..parent");
}
pwInput.println("</pie>");
// After using the PrintWriter object, de-allocated its memory
pwInput.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String inputFile = "C:/Workspace - FYPJ/FYPJ_Period2/WebContent/PieChart.xml";
String outputFile = "C:/Workspace - FYPJ/FYPJ_Period2/WebContent/PieChart.xml";
Document doc = null;
try {
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(inputFile));
} catch (SAXException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (ParserConfigurationException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodes = null;
try {
nodes = (NodeList)xpath.evaluate("//slice[@color='#FF0000'][@title='Student'][@value='0']", doc, XPathConstants.NODESET);
} catch (XPathExpressionException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
for (int value = 0; value<nodes.getLength(); value++){
nodes.item(value).setTextContent("" + pb.getStudentListCount());
}
Transformer xformer = null;
try {
xformer = TransformerFactory.newInstance().newTransformer();
} catch (TransformerConfigurationException e1){
//TODO Auto-Generated Catch Block
e1.printStackTrace();
} catch(TransformerFactoryConfigurationError e1){
//TODO Auto-Generated Catch Block
e1.printStackTrace();
}
try{
xformer.transform(new DOMSource(doc), new StreamResult(new File(outputFile)));
} catch (TransformerException e){
//TODO Auto-Generated Catch Block
e.printStackTrace();
}
try{
Thread.sleep(9000);
} catch (InterruptedException e){
e.printStackTrace();
}
RequestDispatcher view = request.getRequestDispatcher("Pie&Table.jsp");
view.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub{
doGet(request,response);
}
}
My PieChart doesnt show any thing. So does my XML file. It's not updated. So i suppose my Servlet didnt read my class. Hoping any java expert will give me some advice. Thanks.