码迷,mamicode.com
首页 > 其他好文 > 详细

HashSet ——学生姓名&成绩

时间:2014-08-11 21:03:22      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   java   os   io   for   

 Implement HashSet to store ‘n’ records of students ( Name, Percentage). Write a menu driven program to :

1. Add student

2. Display details of all students

3. Search student

4. Find out highest marks

 

  1 package Aug11.Collection;
  2 
  3 import java.util.*;
  4 
  5 public class Test4 {
  6     public static Set set = new HashSet();
  7 
  8     public static void main(String[] args) {
  9         Scanner reader = new Scanner(System.in);
 10 
 11         while (true) {
 12             System.out.println("---------------------");
 13             System.out.println("1. Add student");
 14             System.out.println("2. Display details of all students");
 15             System.out.println("3. Search student");
 16             System.out.println("4. Find out highest marks");
 17             System.out.println("5. Exit");
 18             System.out.println("---------------------");
 19             int n = reader.nextInt();
 20             switch (n) {
 21             case 1:
 22                 addStu();
 23                 break;
 24             case 2:
 25                 displayAllStu();
 26                 break;
 27             case 3:
 28                 searchStu();
 29                 break;
 30             case 4:
 31                 findHighestMarks();
 32                 break;
 33             case 5:
 34                 Exit();
 35                 break;
 36             }
 37         }
 38 
 39     }
 40 
 41     private static void Exit() {
 42         System.out.println("Exit Success!");
 43         System.exit(0);
 44 
 45     }
 46 
 47     private static void findHighestMarks() {
 48         Student stu;
 49         int max = 0;
 50         Iterator itr = set.iterator();
 51         while (itr.hasNext()) {
 52             stu = (Student) itr.next();
 53             if (stu.getPercentage() > max) {
 54                 max = stu.getPercentage();
 55             }
 56 
 57         }
 58         System.out.println("The  highest mark is " + max);
 59 
 60     }
 61 
 62     private static void searchStu() {
 63         Scanner reader = new Scanner(System.in);
 64         boolean flag = false;
 65         System.out.println("Enter the student‘s name:");
 66         String name = reader.nextLine();
 67         Iterator itr = set.iterator();
 68         while (itr.hasNext()) {
 69             Student stu = (Student) itr.next();
 70             if (stu.getName().equals(name)) {
 71                 System.out.println(stu.getName() + ":" + stu.getPercentage());
 72                 return;
 73             } else{
 74                 flag=false;
 75             }
 76         }
 77         if(!flag)
 78         System.out.println("There has no such person");
 79     }
 80 
 81     private static void displayAllStu() {
 82         System.out.println("The information of all students:");
 83         Iterator itr = set.iterator();
 84         while (itr.hasNext()) {
 85             Student stu = (Student) itr.next();
 86             System.out.println(stu.getName() + ":" + stu.getPercentage());
 87         }
 88     }
 89 
 90     private static void addStu() {
 91         Scanner reader = new Scanner(System.in);
 92         System.out.println("Enter student‘s name:");
 93         String name = reader.nextLine();
 94         System.out.println("Enter student‘s percentage:");
 95         int percentage = reader.nextInt();
 96         Student stu = new Student(name, percentage);
 97         set.add(stu);
 98     }
 99 
100 }
101 
102 // Student class
103 
104 class Student {
105     String name;
106     int percentage;
107     public Student(){
108         
109     }
110     public Student(String name, int percentage) {
111         this.name = name;
112         this.percentage = percentage;
113 
114     }
115 
116     public String getName() {
117         return name;
118     }
119 
120     public void setName(String name) {
121         this.name = name;
122     }
123 
124     public int getPercentage() {
125         return percentage;
126     }
127 
128     public void setPercentage(int percentage) {
129         this.percentage = percentage;
130     }
131 
132 }

 

bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣

 

HashSet ——学生姓名&成绩,布布扣,bubuko.com

HashSet ——学生姓名&成绩

标签:des   style   blog   color   java   os   io   for   

原文地址:http://www.cnblogs.com/thrive/p/3905395.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!