import java.util.*;
import java.io.*;
class demo_hashtable
{ public static void main(String args[]) throws IOException
{ BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
int opt;
String name;
double per , maxp;
Enumeration n;
Hashtable HT = new Hashtable();
do
{ System.out.println("\n1. Add student ");
System.out.println("2. Display students ");
System.out.println("3. Search student ");
System.out.println("4. Find Highest percentage ");
System.out.println("5. Exit ");
System.out.print("Enter your option : ");
opt = Integer.parseInt(br.readLine());
switch(opt)
{ case 1: System.out.print("Enter Name : ");
name = br.readLine();
System.out.print("Enter percentage : ");
per = Double.parseDouble(br.readLine());
HT.put(name , per);
break;
case 2: System.out.println(HT);
break;
case 3: System.out.print("Enter Name : ");
name = br.readLine();
if(HT.containsKey(name))
System.out.println("Percentage : " + HT.get(name));
else
System.out.println(name + " not present in hashtable");
break;
case 4: n = HT.keys();
per = maxp =0.0;
name = "";
while(n.hasMoreElements())
{ String str =(String)n.nextElement();
per = (Double) HT.get(str);
if( per > maxp)
{ maxp = per;
name = str;
}
}
System.out.println("Student : " + name +" Highest Percentage : "+maxp);
break;
case 5 : System.exit(0);
default : System.out.println("Wrong Option");
}//switch
}while(opt != 5);
}
}
No comments:
Post a Comment